gpt4 book ai didi

node.js - 如何以不同的用户级别登录我的 Angular

转载 作者:太空宇宙 更新时间:2023-11-03 23:16:41 25 4
gpt4 key购买 nike

我如何像这样使用不同的用户级别登录如果级别为 1,则用户为 super 管理员 如果级别为 2,则用户为管理员如果它的 3 个采用我使用 Angular 7 Nodejs 和 mongodb这是我的项目的链接 https://github.com/Kammikazy/project

这是我的代码

授权文件 获取用户详细信息(用户名,密码){ 返回 this.http.post('api/login',{ 用户名, 密码 }) }

登录组件

login() {
let username = this.loginForm.value.username;
let password = this.loginForm.value.password;
this.Auth.getUserDetails(username,password).subscribe(data =>{
if(data.success){
this.router.navigate(['homepage'])
this.Auth.setLoggedIn(true)
//sdsadad
}else{
document.getElementById("erro").style.visibility = "visible";
window.setTimeout("location.href='login'",3000);

}
})

}

nodejs

app.post('/api/login', async (req,res)=>{
const {username,password}=req.body
const resp = await User.findOne({ username: username}).select('+password')
if(password==null){
res.json({success:false,
message:" password can not be empty"
})
}else if(username==null){
res.json({success:false,
message:" username can not be empty "
})
}
else if(!resp){
res.json({success:false,
message:"incorrect password"
})

}
else if(!await bcrypt.compare(password, resp.password)){
//if(!resp){

res.json({success:false,
message:"incorrect password"
})
}else {
///sessions
req.session.user=username
req.session.save()

res.json({
success:true
})

}
})

最佳答案

  // declare golbal variable
responce: any;
login() {
let username = this.loginForm.value.username;
let password = this.loginForm.value.password;
this.Auth.getUserDetails(username, password).subscribe(data => {
this.responce = data
if (this.responce.user == 'level1') {
this.router.navigate(['homepage'])
this.Auth.setLoggedIn(true)
//sdsadad

} else if (this.responce.user == 'level2') {

this.router.navigate(['secondpage'])
}
else if (this.responce.user == 'level3') {

this.router.navigate(['thirdpage'])
}

})
}

关于node.js - 如何以不同的用户级别登录我的 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54618417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com