[英]Getting data in an Angular Component (Profile page)
I'm working on a MEAN Stack application.我正在开发一个 MEAN Stack 应用程序。 I have made authentication and authorization using jWt and it works like a charm.
我已经使用 jWt 进行了身份验证和授权,它就像一个魅力。 The problem that I'm facing is how to get the user data in the Profile page component.
我面临的问题是如何在 Profile 页面组件中获取用户数据。 I'm thinking about several options:
我正在考虑几个选项:
This is how I created the jwt token:这就是我创建 jwt 令牌的方式:
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:这就是我用来处理 controller 中的请求的存储库中的方法:
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登录服务的响应需要是配置文件信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.