AWS lambda serverless website session maintaining

时间:2021-08-03 18:48:48

I developed a website using node.js as back-end. Recently I am trying to make it serverless and deploy to lambda. I will re-write most of my code but just haven't figured out how to maintain the session after user logged in. I was using "express-session" module and the session data is all recorded in the database.

我开发了一个使用node.js作为后端的网站。最近我试图使它无服务器并部署到lambda。我将重新编写我的大部分代码,但是在用户登录后还没弄清楚如何维护会话。我使用的是“express-session”模块,会话数据全部记录在数据库中。

To be honest I don't have a very deep understanding on sessions. I searched on google and did not find what I need. Does anyone have some sample code on maintaining sessions using lambda? or any resources. Thanks a lot!

说实话,我对会议没有非常深刻的理解。我在谷歌搜索,但没有找到我需要的东西。有没有人在使用lambda维护会话时有一些示例代码?或任何资源。非常感谢!

2 个解决方案

#1


3  

There are multiple mechanisms available in HTTP to maintain session state within web applications, such as cookies (standard HTTP header), URL parameters, URL arguments on GET requests, body arguments on POST requests, such as hidden form fields (HTML forms), or proprietary HTTP headers.

HTTP中有多种机制可用于维护Web应用程序中的会话状态,例如cookie(标准HTTP标头),URL参数,GET请求上的URL参数,POST请求的主体参数,例如隐藏的表单字段(HTML表单),或者专有HTTP标头。

Source: Session Management Cheat Sheet

资料来源:会议管理备忘单

AWS Lambda has nothing to do with session management unless you want to re-invent the wheel and write Lambda functions that store/retrieve session variables from the database, in which case I'd recommend that you use Amazon Cognito for session management. See Amazon Cognito Identity SDK for JavaScript.

AWS Lambda与会话管理无关,除非您想重新发明*并编写从数据库中存储/检索会话变量的Lambda函数,在这种情况下,我建议您使用Amazon Cognito进行会话管理。请参阅适用于JavaScript的Amazon Cognito Identity SDK。

#2


2  

In the Amazon Cognito Identity SDK for Javascript, check in particular the use case 16, it shows how to retrieve the Cognito current user. You can use this function to pass from page to page the current user attributes.

在用于Javascript的Amazon Cognito Identity SDK中,特别检查用例16,它显示了如何检索Cognito当前用户。您可以使用此功能从页面传递当前用户属性。

    var poolData = {
        UserPoolId : '...', // Your user pool id here
        ClientId : '...' // Your client id here
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var cognitoUser = userPool.getCurrentUser();

    if (cognitoUser != null) {
        cognitoUser.getSession(function(err, session) {
            if (err) {
                alert(err);
                return;
            }
            console.log('session validity: ' + session.isValid());
            // other AWS actions ...
        });
    }

#1


3  

There are multiple mechanisms available in HTTP to maintain session state within web applications, such as cookies (standard HTTP header), URL parameters, URL arguments on GET requests, body arguments on POST requests, such as hidden form fields (HTML forms), or proprietary HTTP headers.

HTTP中有多种机制可用于维护Web应用程序中的会话状态,例如cookie(标准HTTP标头),URL参数,GET请求上的URL参数,POST请求的主体参数,例如隐藏的表单字段(HTML表单),或者专有HTTP标头。

Source: Session Management Cheat Sheet

资料来源:会议管理备忘单

AWS Lambda has nothing to do with session management unless you want to re-invent the wheel and write Lambda functions that store/retrieve session variables from the database, in which case I'd recommend that you use Amazon Cognito for session management. See Amazon Cognito Identity SDK for JavaScript.

AWS Lambda与会话管理无关,除非您想重新发明*并编写从数据库中存储/检索会话变量的Lambda函数,在这种情况下,我建议您使用Amazon Cognito进行会话管理。请参阅适用于JavaScript的Amazon Cognito Identity SDK。

#2


2  

In the Amazon Cognito Identity SDK for Javascript, check in particular the use case 16, it shows how to retrieve the Cognito current user. You can use this function to pass from page to page the current user attributes.

在用于Javascript的Amazon Cognito Identity SDK中,特别检查用例16,它显示了如何检索Cognito当前用户。您可以使用此功能从页面传递当前用户属性。

    var poolData = {
        UserPoolId : '...', // Your user pool id here
        ClientId : '...' // Your client id here
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var cognitoUser = userPool.getCurrentUser();

    if (cognitoUser != null) {
        cognitoUser.getSession(function(err, session) {
            if (err) {
                alert(err);
                return;
            }
            console.log('session validity: ' + session.isValid());
            // other AWS actions ...
        });
    }