Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.1 kB
2
Indexable
Never
import { Component, OnInit, Output,EventEmitter } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../auth.service';
import { SharedServiceService } from '../shared/shared-service.service';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  loginUserData:any={};
  loginTypes=[{type:'admin'},{type:'manager'},{type:'user'}]
  constructor( private router:Router ,private sharedObj:SharedServiceService, private _auth:AuthService) { }

  ngOnInit(): void {
  }
   
  loginUser(){
    console.log(this.loginUserData)
    if(this.loginUserData.type=='admin'){
      this._auth.loginAdmin(this.loginUserData).subscribe(
        res=>{
          console.log(res);
          localStorage.setItem('token',res.data.accessToken)
          sessionStorage.setItem('type','admin');
          if (res.data.accessToken!=="null" || res.data.accessToken!=="undefined"){
             this.router.navigate(['/admin'])
          }
          
        },
        err=>{console.log(err)}
                
      )
      
    }
   else if (this.loginUserData.type=='manager') {
      
    this._auth.loginManager(this.loginUserData).subscribe(
      res=>{
        console.log(res);
        localStorage.setItem('token',res.data.accessToken)
        sessionStorage.setItem('type','manager');
        if (res.data.accessToken!=="null" || res.data.accessToken!=="undefined"){
           this.router.navigate(['/manager'])
        }
        
      },
      err=>{console.log(err)}          
    )
    } 
    else if (this.loginUserData.type=='user') {
      
      this._auth.loginUser(this.loginUserData).subscribe(
        res=>{
          console.log(res);
          localStorage.setItem('token',res.data.accessToken)
          sessionStorage.setItem('type','user');
          if (res.data.accessToken!=="null" || res.data.accessToken!=="undefined"){
             this.router.navigate(['/user'])
          }
          
        },
        err=>{console.log(err)}          
      )
    } 
  }
}