I'm working on a MEAN Stack application. I have made authentication and authorization using jWt and it works like a charm. The problem that I'm facing is how to get the user data in the Profile page component. I'm thinking about several options:
This is how I created the jwt token:
login: async (data, model) => {
try {
/**
* Fetch the admin from the Database
*/
const adminData = await baseRepository.findOne({ email: data.email }, model);
/**
* Check if an admin with that email exists
*/
if (!adminData) {
return (400, { message: "ADMIN NOT FOUND" })
} else {
/**
* Compare the input password with the hashed password in the database
*/
const admin = { email: adminData.email }
if (await bcrypt.compare(data.password, adminData.password)) {
/**
* Create a jwt Token and send it back to the client
*/
const accessToken = jwt.sign(admin, process.env.ACCESS_TOKEN_SECRET)
return ({ status: 200, accessToken: accessToken })
}
return ({ status: 401, accessToken: null })
}
}
catch (err) {
throw err
}
}
That's the method from the repository that I'm using to handle the request in the controller this way:
login: async (req, res) => {
try {
console.log("yo")
const { status, accessToken } = await authRepository.login(req.body, Admin)
if (status == 400) {
res.status(400).json({ message: "ADMIN NOT FOUND" })
} else if (status == 401) {
res.status(401).json({ message: "WRONG PASSWORD" })
}
res.status(200).json({ accessToken: accessToken })
}
catch (e) {
res.status(400).send({ error: e })
}
}
And these are the libraries I'm using:
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
The response of the login service need to be the profile info
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.