mongodb 如果用户的状态为“活动”,我如何允许用户登录?

toiithl6  于 2022-11-03  发布在  Go
关注(0)|答案(1)|浏览(186)

我正在创建管理 Jmeter 板,其中帐户的status: 'active '必须在登录前首先处于活动状态。
这是我的数据库,状态为"active"

但当我运行到后端时

router.post('/login', async (req,res) =>{
    try {
        const studentId = await User.findOne({studentId: req.body.studentId})
        if(!studentId) return res.status(404).json("User is not yet accepted")

        const studentStatus = await User.findOne({status: 'active'})

        if(studentStatus === 'active'){
            const isPasswordCorrect = CryptoJS.AES.decrypt(
                studentId.password,
                process.env.PASSWORD_SEC
                )

            const originalPassowrd = isPasswordCorrect.toString(CryptoJS.enc.Utf8)
                if(originalPassowrd !== req.body.password) 
                 return res.status(400).json("Incorrect password")

                 const acessToken = jwt.sign({
                    id:studentId._id, isAdmin: studentId.isAdmin
                 }, process.env.JWT_KEY, {expiresIn: "3d"})

            const {password, ...others} = studentId._doc
            res.status(200).json({...others, acessToken})
        }else{
           return res.status(400).json("Still Pending Account")
        }   
        } catch (error) {
        res.status(400).json({message:error.message})
    }
})

我收到错误
“仍挂起帐户”
用户方案

const UserSchema = new mongoose.Schema(
    {
        firstname: {type: String, required: true},
        middlename: {type: String},
        lastname: {type: String, required: true},
        email: {type: String, required: true, unique: true},
        department: {type: String, required: true},
        password: {type:String, required: true},
        studentId: {type:String,required:true,unique:true},
        img: {type: String},
        isAdmin: {type: Boolean,default: false},
        status: {type:String, default: "active"},
    }
, {timestamps: true}
)

编辑

我想我已经解决了它,我抓取others.status的信息并检查它是否等于'active'

router.post('/login', async (req,res) =>{
    try {
        const studentId = await User.findOne({studentId: req.body.studentId})
        if(!studentId) return res.status(404).json("User is not yet accepted")

            const isPasswordCorrect = CryptoJS.AES.decrypt(
                studentId.password,
                process.env.PASSWORD_SEC
                )

            const originalPassowrd = isPasswordCorrect.toString(CryptoJS.enc.Utf8)
                if(originalPassowrd !== req.body.password) 
                 return res.status(400).json("Incorrect password")

                 const acessToken = jwt.sign({
                    id:studentId._id, isAdmin: studentId.isAdmin
                 }, process.env.JWT_KEY, {expiresIn: "3d"})

            const {password, ...others} = studentId._doc

            if(others.status === 'active'){
                res.status(200).json({...others, acessToken})
            }
            else{
                res.status(404).json("Account still Pending")
            }        

        } catch (error) {
        res.status(400).json({message:error.message})
    }
})
qlfbtfca

qlfbtfca1#

User.findOne({status: 'active'})返回学生的整条记录,而不是单个学生状态字段
您可以通过change if语句来检查状态字段

if(studentStatus.status === 'active'){
...

相关问题