login with multiple angular access

Asked

Viewed 125 times

-3

my problem is the following I am trying to put my login with various levels of access as if it is level 1 enters as super administrator if it is 2 enters as administrator and if it is 3 enters as employee I’m using angular 7 nodejs and mongodb the code is followed auth.service.ts

`getUserDetails(username,password){ return this.http.post('api/login',{ username, password }`) }

login Component

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

    }
    })

what I have to change the complete code is on this link https://github.com/Kammikazy/project

  • what the problem or error?

  • the problem is I don’t know what I have to change in my code

  • In this case your question is very generic pro stackoverflow, has a thousand ways to have this behavior. One of them would be to return a user object and there vc do the logics of access

1 answer

0


this was my solution to solve my problem

    login() {
      let username = this.loginForm.value.username;
         let password = this.loginForm.value.password;
      this.Auth.getUserDetails(username,password).subscribe(data =>{
        if(data.success){

      if(data.nivel=='1'){
      this.router.navigate(['homepage'])
      this.Auth.setLoggedIn(true)


      }
    else if(data.nivel=='2'){
      this.router.navigate(['homepage'])
     this.Auth.setLoggedIn(true)
      }
      else if(data.nivel=='3'){
        this.router.navigate(['homepage'])
      this.Auth.setLoggedIn(true)


        }
      //sdsadad
        }else{
        document.getElementById("erro").style.visibility = "visible";
        window.setTimeout("location.href='login'",3000);

        }
      })

      }
node index
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
  const resp = await User.findOne({ username: username}, function(err, result) {
      const {username}=req.body
    if (err) throw err;
    var obj= result
  req.session.user=username
  req.session.nivel=obj.nivel
  req.session.save()


  res.json({
    success:true,
    nivel:obj.nivel
  })
});
}

})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.