MIT Weblab笔记 - Authentication
Introduction
Setting up account and authentication involves achieving two goals:
- Initial login
- Staying login when making requests or refreshing pages
Initial Login
User send username and password to the server, however, it’s not safe to directly pass them as JSON. Using Google Authentication can help us manage the account.
Session
- When a user logs in, the server creates a session and assigns a unique session ID to the user.
- The session ID is stored on the client-side in a cookie, while the actual session data corresponding to the session ID is stored on the server
- On each subsequent request, the server uses the session ID from the cookie to retrieve the session data and identify the user.
JWT (Json Web Token)
JWTs do not require server-side storage of session data.
- When a user logs in, the server generates a JWT containing the user’s information and sends it to the client.
- Browser puts JWT in local storage. The client includes the JWT for subsequent requests.
- The server verifies the token’s signature to authenticate the user.
How to Manage Login?

Login (Frontend)
At Frontend:
1 |
|
- When user clicks the login button, it triggers google login process. (GoogleLogin component from the react-google-login package).
onSuccess={handleLogin}
: Triggers handleLogin function when loggin in successgully, passing response from google as the parameter.

- Inside
handleLogin
, It extracts the id_token from the response byuserToken = res.tokenObj.id_token
and sends it to the server via a POST request to the/api/login
endpoint.
Login (Backend)
Define user schema for MongoDB:
1 |
|
The frontend passes the userToken to backend through post request.
Handling /api/login
Post request by calling login
function in the following:
1 |
|
- The
login
function calls theverify
function to validate the token with Google. - The
login
function then callsgetOrCreateUser
. This function checks if the user already exists in the database using the googleid. If the user exists, it returns the existing user; if not, it creates a new user in the database. - Then, the login function stores the user information in the session by
req.session.user = user
. - sends the user data back to the client.
req.session and authenticating following requests
1 |
|
app.use(session({...}))
:- This middleware sets up and manages sessions in an Express application.
- It uses a server-side storage mechanism to store session data.
- It creates a session ID, which is stored in a cookie on the client side.
Previously, we do
req.session.user = user
, the following happens:- The
express-session
middleware stores the user object in the session data associated with the current session on server side. - The session middleware ensures that a cookie containing the
session ID
is sent to the client’s browser. - For every subsequent request, the browser automatically includes the session ID cookie in the request headers. The session middleware parses the session ID from the client’s cookie, retrieves the session data, and attaches it to req.session. As a result, req.session is automatically filled by the
user
document that we previously stored in session.
- The
populateCurrentUser
middleware: The populateCurrentUser middleware sets req.user to the user data from req.session.user for each following request.
Stay Logged In At Frontend
Now, since the session ID is in my browser’s cookie, everytime I make a request, as long as the cookies exist, my req.user
will be set as the user that I previously logged in.
To let frontend know that I am currently logged in, we can send a get request to backend, then backend checks req.user
and sends back.
1 |
|