简体   繁体   English

在 Angular 组件中获取数据(配置文件页面)

[英]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:我正在考虑几个选项:

  • First, sending the email from the Login component to the Dashboard and then pass it to Profile .首先,将 email 从Login组件发送到Dashboard ,然后将其传递给Profile It will be easy from then to send a get request to get the user with the email.从那时起,发送获取请求以使用 email 获取用户将很容易。
  • Second, I don't know if it possible but I'm thinking of using the jwt I'm returning to the user to get his data since I created it with the provided email in Login其次,我不知道这是否可能,但我正在考虑使用jwt我返回给用户以获取他的数据,因为我在登录时使用提供的email创建了它

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM