簡體   English   中英

在 Angular 組件中獲取數據(配置文件頁面)

[英]Getting data in an Angular Component (Profile page)

我正在開發一個 MEAN Stack 應用程序。 我已經使用 jWt 進行了身份驗證和授權,它就像一個魅力。 我面臨的問題是如何在 Profile 頁面組件中獲取用戶數據。 我正在考慮幾個選項:

  • 首先,將 email 從Login組件發送到Dashboard ,然后將其傳遞給Profile 從那時起,發送獲取請求以使用 email 獲取用戶將很容易。
  • 其次,我不知道這是否可能,但我正在考慮使用jwt我返回給用戶以獲取他的數據,因為我在登錄時使用提供的email創建了它

這就是我創建 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
    }
}

這就是我用來處理 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 })
    }

}

這些是我正在使用的庫:

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

登錄服務的響應需要是配置文件信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM