Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
  3. import { throwError, Observable } from 'rxjs';
  4. import { map, catchError } from 'rxjs/operators';
  5.  
  6. import { Restaurant } from '../models/Restaurant';
  7. import { LocalStorage } from '@ngx-pwa/local-storage';
  8.  
  9. @Injectable({
  10. providedIn: 'root'
  11. })
  12. export class AuthService {
  13.  
  14. loginUrl = 'xxxxxxxxxx';
  15. errorData: {};
  16.  
  17.  
  18. constructor(private http: HttpClient) { }
  19.  
  20. redirectUrl: string;
  21.  
  22. login(email: string, password: string) {
  23. var postData = {email: email, password: password};
  24. return this.http.post<Restaurant>(this.loginUrl, postData)
  25. .pipe(map(restaurant => {
  26. if (restaurant) {
  27. localStorage.setItem('currentRestaurant', JSON.stringify(restaurant));
  28. return restaurant;
  29. }
  30. }),
  31. catchError(this.handleError)
  32. );
  33. }
  34.  
  35. isLoggedIn() {
  36. if (localStorage.getItem('currentRestaurant')) {
  37. return true;
  38. }
  39. return false;
  40. }
  41.  
  42. getAuthorizationToken() {
  43. const currentRestaurant = JSON.parse(localStorage.getItem('currentRestaurant'));
  44. return currentRestaurant.token;
  45. }
  46.  
  47. logout() {
  48. localStorage.removeItem('currentRestaurant');
  49. }
  50.  
  51. private handleError(error: HttpErrorResponse) {
  52. if (error.error instanceof ErrorEvent) {
  53.  
  54. // A client-side or network error occurred. Handle it accordingly.
  55. console.error('An error occurred:', error.error.message);
  56. } else {
  57.  
  58. // The backend returned an unsuccessful response code.
  59. // The response body may contain clues as to what went wrong.
  60. console.error(`Backend returned code ${error.status}, ` + `body was: ${error.error}`);
  61. }
  62.  
  63. // return an observable with a user-facing error message
  64. this.errorData = {
  65. errorTitle: 'Oops! Request for document failed',
  66. errorDesc: 'Something bad happened. Please try again later.'
  67. };
  68. return throwError(this.errorData);
  69. }
  70.  
  71. currRestaurant: Restaurant = JSON.parse(localStorage.getItem('currentRestaurant'));
  72. currID = this. currRestaurant.id;
  73. }
  74.  
  75. import { Component, OnInit } from '@angular/core';
  76. import { FormBuilder, Validators, FormGroup } from '@angular/forms';
  77. import { Router } from '@angular/router';
  78. import { AuthService } from '../services/auth.service';
  79.  
  80. @Component({
  81. selector: 'app-login',
  82. templateUrl: './login.component.html',
  83. styleUrls: ['./login.component.scss']
  84. })
  85. export class LoginComponent implements OnInit {
  86.  
  87. loginForm: FormGroup;
  88. submitted = false;
  89. returnUrl: string;
  90. error: {};
  91. loginError: string;
  92.  
  93. constructor(
  94. private fb: FormBuilder,
  95. private router: Router,
  96. private authService: AuthService
  97. ) { }
  98.  
  99. ngOnInit() {
  100. this.loginForm = this.fb.group({
  101. email: ['', Validators.required],
  102. password: ['', Validators.required]
  103. });
  104.  
  105. this.authService.logout();
  106. }
  107.  
  108. get email() { return this.loginForm.get('email'); }
  109. get password() { return this.loginForm.get('password'); }
  110.  
  111. onSubmit() {
  112. this.submitted = true;
  113. this.authService.login( this.email.value, this.password.value).subscribe((data) => {
  114.  
  115. if (this.authService.isLoggedIn) {
  116. const redirect = this.authService.redirectUrl ? this.authService.redirectUrl : '/';
  117. this.router.navigate([redirect]);
  118. } else {
  119. this.loginError = 'email or password is incorrect.';
  120. }
  121. },
  122. error => this.error = error
  123. );
  124.  
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement