如何将不同用户级别的登录名放入角度

时间:2019-02-10 16:19:36

标签: node.js angular mongodb

我如何以这样的不同用户级别登录? 如果将其1级用户作为其超级管理员,如果将2级用户作为其管理员, 如果其3名雇员 我使用Angular 7 Node.js和MongoDB 这是我的项目https://github.com/Kammikazy/project

的链接

这是我的代码

身份验证文件        getUserDetails(用户名,密码){     返回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
      })

    }
    })

1 个答案:

答案 0 :(得分:1)

  // 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'])
    }

  })
}