【问题标题】:Getting data in an Angular Component (Profile page)在 Angular 组件中获取数据(配置文件页面)
【发布时间】:2021-05-26 04:06:18
【问题描述】:

我正在开发一个 MEAN Stack 应用程序。我已经使用 jWt 进行了身份验证和授权,它就像一个魅力。 我面临的问题是如何在 Profile 页面组件中获取用户数据。我正在考虑几个选项:

  • 首先,将邮件从Login组件发送到Dashboard,然后传递给个人资料。从那时起,发送获取请求以获取用户的电子邮件将很容易。
  • 其次,我不知道是否可能,但我正在考虑使用 jwt 我返回给用户以获取他的数据,因为我使用提供的 创建了它登录中的电子邮件

这就是我创建 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
    }
}

这是我用来在控​​制器中处理请求的存储库中的方法:

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 })
    }

}

这些是我正在使用的库:

const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");

【问题讨论】:

  • 能否包含代码段,包括您正在使用的 jwt 库?有一个服务保存登录操作返回的配置文件数据是很常见的。然后像仪表板或配置文件这样的组件从那里获取它,而不需要查询参数。我正在使用基于 JWT 的 auth0 身份验证服务。我的身份验证服务使用他们的 js SDK,它会返回一些基本的个人资料信息,包括电子邮件。
  • 大功告成!如您所见,我使用了 jsonwebtoken 包

标签: javascript node.js angular rest jwt


【解决方案1】:

登录服务的响应需要是个人资料信息

【讨论】:

  • 然后将返回值传递给profile组件?
  • 你需要将值保存在本地存储中,而不是在特定组件中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
相关资源
最近更新 更多