本文介绍了颤抖的Web Cookie /令牌会话和身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NodeJS和Flutter For Web 开发一个全栈应用程序,此刻,我还不知道如何进行安全的cookie /令牌会话。 br>
我需要的答案是如何使用Flutter For Web创建身份验证系统,就像其他社交网络或Stackoverflow本身一样。

I am working on a full stack app using NodeJS and Flutter For Web, at the moment i don't understand how to make safe cookie/token sessions.
The answer i need is how to make an authentication system with Flutter For Web like other Social Networks or Stackoverflow itself.

推荐答案

从flutter 1.9中不直接导入 dart.html

Importing dart.html directly doesn't support from flutter 1.9 : Reference

我碰到了包裹,同时寻找解决方案,它对我来说很好用。以下是我的帮助程序类,用于在网络上本地存储键值对:

I came across the package universal_html while digging in for the solution, and its working fine for me. Below is my helper class to store key-value pair locally on web:

import 'package:universal_html/prefer_universal/html.dart';

class WebStorage {

  //Singleton
  WebStorage._internal();
  static final WebStorage instance = WebStorage._internal();
  factory WebStorage() {
    return instance;
  }

  String get sessionId => window.localStorage['SessionId'];
  set sessionId(String sid) => (sid == null) ? window.localStorage.remove('SessionId') : window.localStorage['SessionId'] = sid;
}

要阅读,

WebStorage.instance.sessionId;

要编写,

示例:

fetchPost(params, "CMD_USERREGISTRATION").then((result) {
        ...
        APIResponse response = APIResponse(xmlString: result.body);

        if (!response.isSuccess()) {
          ...
          return;
        }
        var sid = response.getSessionId();

        if (kIsWeb) {
          WebStorage.instance.sessionId = sid;
        }
}

main.dart:

@override
  Widget build(BuildContext context) {
    if (kIsWeb) {
      isLogin = WebStorage.instance.sessionId != null;
    } else {
      isLogin = //check from SharedPreferences;
    }
    return isLogin ? dashboardPage() : loginPage();
  }

更新:

现在支持0.5版以上的网络.6。另请参见

shared_preferences now support web from the version 0.5.6. See also shared_preferences_web

这篇关于颤抖的Web Cookie /令牌会话和身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:29