Guest User

Untitled

a guest
Aug 17th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2.  
  3. @Injectable({
  4. providedIn: 'root'
  5. })
  6. export class AuthService {
  7.  
  8. constructor() { }
  9. /*
  10. The login method will return true if the provided user/password pair
  11. equals 'user' and 'password', respectively. Also, when it is matched, it’s
  12. going to use localStorage to save the username. This will also serve as a flag
  13. to indicate whether or not there is an active logged user.
  14. */
  15. login(user: string, password: string): boolean {
  16. if (user === 'user' && password === 'password') {
  17. localStorage.setItem('username', user);
  18. return true;
  19. }
  20. return false;
  21. }
  22. // The logout method just clears the username value:
  23. logout(): void {
  24. localStorage.removeItem('username');
  25. }
  26. // getUser returns the username or null
  27. getUser(): any {
  28. return localStorage.getItem('username');
  29. }
  30. // isLoggedIn uses getUser() to return true if we have a user
  31. isLoggedIn(): boolean {
  32. return this.getUser() !== null;
  33. }
  34. }
Add Comment
Please, Sign In to add comment