Advertisement
MIAlDK

login

Feb 18th, 2020
1,765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Login component
  2. login()
  3.   {
  4.     let promise;
  5.     let email = this.loginForm.get("email").value
  6.     let password = this.loginForm.get("password").value
  7.     this.http.login(email, password)
  8.     .then( () =>
  9.     {
  10.       this.router.navigate(['/dashboard']);
  11.     },
  12.     codError =>
  13.     {
  14.       this.errorCode = codError;
  15.     }
  16.     );
  17.  
  18.     console.log(this.errorCode);
  19.  
  20.   }
  21.  
  22. //Http Service
  23.  login(email: string, password: string)
  24.   {
  25.     let codError;
  26.     this.url = "https://api.kyps.tk/user/login";
  27.     this.body =
  28.     {
  29.       "email": email,
  30.       "password": password
  31.     }
  32.     let promise = new Promise((resolve, reject) =>
  33.     {
  34.       this.http.post(this.url, this.body, {headers: this.header}).subscribe((risposta: LoginResponse) =>
  35.     {
  36.       console.log(risposta);
  37.       if (risposta.error)
  38.       {
  39.         if (risposta.error === "Email not registered yet")
  40.           codError = 1;
  41.         else
  42.           codError = 2;
  43.  
  44.         reject(codError);
  45.       }
  46.       else
  47.       {
  48.         resolve();
  49.       }
  50.      // console.log(codError);
  51.     })
  52.     });
  53.  
  54.     return promise;  
  55.         //La risposta è composta da token + user oppure da stringa errore
  56.         //Errore 1: Email non registrata
  57.         //Errore 2: Password errata
  58.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement