Reputation: 948
I implement user registration. I have two routes.
import express from 'express';
import User from '../models/user';
const router = express.Router();
router.post('/signup', async (req, res) => {
try {
...
const user = new User(
red.body.name,
red.body.username,
red.body.user_email,
red.body.user_password,
red.body.user_phone,
);
...
} catch (err) {
return res.status(500).json({
err: err
});
}
});
router.post('/verify', async (req, res) => {
try {
...
console.log(user);
...
} catch (err) {
return res.status(500).json({
err: err
});
}
});
I need to send data from the route, user registration, to the route, user confirmation.
How to send user data to the second route for confirmation?
Upvotes: 1
Views: 748
Reputation: 97
The best way to approach this would be to store the user data as a browser cookie. Storing user data in local storage is not recommended as it can be accessed by JavaScript and hence poses a security threat.
Upvotes: 0
Reputation: 16226
There are 2 strategies to implement this feature (maybe more):
Strategy 1: Store user
in server.
You can store user
object and assign it a unique key (e.g. uuid) in server. The user
data can be stored as global variable (in memory of Node.js process), or it can be stored in memory database (e.g. Redis) if you are using multiple Node.js process.
In POST /signup
route handler, the user
data can be written to server, and this newly created user id would be returned to browser. Then, in POST /verify
route handler, server would retrieve corresponding user data via its id.
Strategy 2: Store user
in browser.
Another strategy is returning the whole user
data to browser in POST /signup
route handler, and let browser send it back in the following POST /verify
request. There are 2 ways to implement this design (maybe more):
Return user
data to browser via Set-Cookie
. Browser would send user data as cookie automatically.
Return user
data to browser as plain response body. Browser take it and save it in localStorage or sessionStorage. Then, when sending POST /verify
request, browser would read that data and put it as plain HTTP request body.
Upvotes: 2