0

I'm developing a Dart application that will need authentication and session control. I'm trying shelf_auth to do that, but the examples doesn't seem to work or, more likely, I'm not implementing them the right way.

In short, this is what I want to happen:

  1. An user opens the application on the browser.
  2. The user enters the login information (login and password), which are POSTED to the server.
  3. If the provided information is valid, the application generates a session code that is passed to the client and stored on the DB (server-side). This code will be sent with every transaction to the server-side.

The package shelf_auth has some examples, but I don't know which one to follow. So my question is: how could I do that with shelf_auth? I'm not asking for anyone to code this for me, but just to point me to the right direction.

EDIT: The example that I was trying out was this: example_with_login_and_jwt_session.dart. Seems that it's lacking CORS headers (this question helped me fixing it) and, even providing valid information, it responds "Unauthorized".

This is how I'm POSTING the information:

import "dart:html";

void main() {
    Map _queryParameters = {
        "username": "fred",
        "password": "blah"
    };
    var _button = querySelector("#login_button");
    _button.onClick.listen((MouseEvent e) {
        e.preventDefault();
        var requisition = new HttpRequest();
        Uri uri = new Uri(path: "http://localhost:8080/login", queryParameters: _queryParameters);
        requisition.open("POST", uri.toString());
        requisition.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        requisition.onLoadEnd.listen((_) {
            print(requisition.response.toString());
        });
        requisition.send();
    });
}
Community
  • 1
  • 1
Felipe
  • 376
  • 2
  • 5
  • 15
  • 1
    There is a bug in shelf_route package, causing path to be set with tailing slash: `http://localhost:8080/login/`. Maintainer promises to fix it soon. – Mike May 26 '15 at 21:38

1 Answers1

0

I got it working with this client code

import "dart:html";

void main() {
  Map _queryParameters = {"username": "fred", "password": "blah"};
  var _button = querySelector("#login_button");
  _button.onClick.listen((MouseEvent e) async {
    e.preventDefault();
    var requisition = new HttpRequest();
    Uri uri = new Uri(
        path: "http://localhost:8080/login/");
    requisition.onLoadEnd.listen((_) {
      print(requisition.response.toString());
    });
    HttpRequest request = await HttpRequest.postFormData(
        "http://localhost:8080/login/", _queryParameters
        //,withCredentials: true
        );
    print(request.response);
  });
}

The example server expects the credentials in the body instead of query parameters and I set withCredentials: true so authentication cookies are sent with the request. Worked without withCredentials.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567