DeepCode00

Untitled

Feb 2nd, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** LOGIN COMPONENT **/
  2.  
  3.   tryLogin() {
  4.     this.apiService.login(
  5.       this.username,
  6.       this.password
  7.     ).subscribe(
  8.       r => {
  9.         this.apiService.setToken(r);
  10.         this.router.navigate(['members','dashboard']);
  11.       },
  12.       r => {
  13.         this.presentAlert({msg: "Nome utente o password non validi, riprovare.", type: "Login error"});
  14.       });
  15.   }
  16.  
  17.  
  18. /** SERVICE INJECTET **/
  19.  
  20.     constructor(private http: HttpClient, private storage: Storage, private plt: Platform, private router: Router) {
  21.         this.plt.ready().then(() => {
  22.             this.getToken();
  23.             this.loadLocalSettings();
  24.             this.storage.get(TOKEN).then(token => {
  25.                 if (token) {
  26.                     this.token = token;
  27.                 }
  28.             });
  29.         })
  30.     }
  31.  
  32.     /** Login function */
  33.     login(username: string, password: string): Observable<Login>{
  34.         this.loadLocalSettings();
  35.         return this.http.post<Login>(this.localSettings.apiUrl + 'login', {
  36.           username: username,
  37.           password: password
  38.         });
  39.     }
  40.  
  41.     setToken(user: User) {
  42.         this.token = user.token;
  43.         this.storage.set(TOKEN, user.token).then((us) => {
  44.             this.setCurrentUser(user);
  45.         });
  46.         this.loggedIn.next(true);
  47.     }
  48.  
  49.     intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  50.         request = request.clone({
  51.           setHeaders: {
  52.             Authorization: `Bearer ${this.token}`,
  53.           },
  54.           responseType: 'json'
  55.         });
  56.         return next.handle(request);
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment