Dart 语言:身份验证和会话控制 (shelf_auth)

Dart Language: Authentication and session control (shelf_auth)

我正在开发一个需要身份验证和会话控制的 Dart 应用程序。我正在尝试 shelf_auth 这样做,但这些示例似乎不起作用,或者更可能的是,我没有以正确的方式实施它们。

简而言之,这就是我想要发生的事情:

  1. 用户在浏览器上打开应用程序。
  2. 用户输入登录信息(登录名和密码),这些信息被发布到服务器。
  3. 如果提供的信息有效,应用程序将生成一个会话代码,该代码将传递给客户端并存储在数据库(服务器端)上。此代码将随每笔交易一起发送到服务器端。

包shelf_auth有一些例子,但我不知道该看哪一个。所以我的问题是:我如何使用 shelf_auth 做到这一点? 我并不是要任何人为我编写代码,而只是为我指明正确的方向。

编辑: 我尝试的示例是这样的:example_with_login_and_jwt_session.dart. Seems that it's lacking CORS headers ( 帮助我修复它)并且,即使提供有效信息,它也会响应 "Unauthorized".

这是我发布信息的方式:

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();
    });
}

我用这个客户端代码得到了它

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);
  });
}

示例服务器需要主体中的凭据而不是查询参数并且我设置了withCredentials: true以便身份验证 cookie 随请求一起发送。在没有 withCredentials.

的情况下工作