简体   繁体   English

NodeJS express-session 在 Android 模拟器上不起作用

[英]NodeJS express-session not working on Android emulator

I'm creating a Flutter app and want to add login using express-session.我正在创建一个 Flutter 应用程序并希望使用快速会话添加登录。 But I have some problem with the session values not being set/fetched correctly using the Android emulator.但是我对使用 Android 仿真器未正确设置/获取 session 值存在一些问题。

Server服务器

const app = express();
const map = new Map();

const sessionParser = session({
    saveUninitialized: false,
    secret: '$eCuRiTy',
    resave: false
});

app.use(sessionParser);

app.use(express.static('public'));

app.post('/login', function (req: any, res: any) {
    console.log("enter login");

    const id = uuid.v4();

    console.log(`Updating session for user ${id}`);
    req.session.userId = id;
    res.send({ result: 'OK', message: 'Session updated' });
});

app.delete('/logout', function (req: any, response: any) {
    console.log("enter logout");
    console.log("Logout user:", req.session.userId);
    const ws = map.get(req.session.userId);
    console.log('Destroying session');
    req.session.destroy(function () {
        if (ws) ws.close();
        response.send({ result: 'OK', message: 'Session destroyed' });
    });
});

When I call above auth functions from Postman using当我使用 Postman 调用上述身份验证函数时

  • Post: http://localhost:7000/login帖子:http://localhost:7000/login
  • Delete: http://localhost:7000/logout删除:http://localhost:7000/logout

I get the following output in my loggs我在我的日志中得到以下 output

  1. enter login进入登录
  2. Updating session for user 1a9a9e62-e972-4a28-82d3-892d282b6321为用户 1a9a9e62-e972-4a28-82d3-892d282b6321 更新 session
  3. enter logout进入注销
  4. Logout user: 1a9a9e62-e972-4a28-82d3-892d282b6321注销用户:1a9a9e62-e972-4a28-82d3-892d282b6321
  5. Destroying session破坏 session

But when I try to make the same calls from my flutter application with the following code I get req.session.userId as undefined但是当我尝试使用以下代码从我的 flutter 应用程序进行相同的调用时,我得到req.session.userId为 undefined

  static const url = "10.0.2.2:7000";
  login() {
    http.post(Uri.parse('http://$url/login'));
  }

  logout() {
    http.delete(Uri.parse('http://$url/logout'));
  }
  1. enter login进入登录
  2. Updating session for user b0e9768e-b24c-4b1a-9a7c-a3fd58ef2ede为用户 b0e9768e-b24c-4b1a-9a7c-a3fd58ef2ede 更新 session
  3. enter logout进入注销
  4. Logout user: undefined注销用户:未定义
  5. Destroying session破坏 session

Any ideas why I am getting undefined using the emulator?任何想法为什么我使用模拟器变得不确定?

As i see in this question: How to make an http DELETE request with JSON body in Flutter?正如我在这个问题中看到的那样:如何使用 Flutter 中的 JSON 主体发出 http DELETE 请求? the "delete" verb does not send data with Flutter. “删除”动词不使用 Flutter 发送数据。 You can see @Roi Snir's answer in the previous link, a way to send data with "delete" by using the .Request method of the http package.您可以在上一个链接中看到@Roi Snir 的答案,这是一种使用 http package 的.Request方法发送数据的方法。

When using flutter you need to pass the cookies manually,使用 flutter 时,您需要手动通过 cookies,

So i got a little helper class from this POST所以我从这篇文章中得到了一个小帮手class

Now i can just call post,get etc through the Session class现在我可以通过Session class 打电话给 post,get 等

class Session {
  Map<String, String> headers = {};

  Future<Map> get(String url) async {
    http.Response response = await http.get(Uri.parse(url), headers: headers);
    updateCookie(response);
    return json.decode(response.body);
  }

  Future<Map> post(String url, dynamic data) async {
    http.Response response =
        await http.post(Uri.parse(url), body: data, headers: headers);
    updateCookie(response);
    return json.decode(response.body);
  }

  Future<Map> delete(String url) async {
    http.Response response =
        await http.delete(Uri.parse(url), headers: headers);
    updateCookie(response);
    return json.decode(response.body);
  }

  void updateCookie(http.Response response) {
    String? rawCookie = response.headers['set-cookie'];
    if (rawCookie != null) {
      int index = rawCookie.indexOf(';');
      headers['Cookie'] =
          (index == -1) ? rawCookie : rawCookie.substring(0, index);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM