File

src/app/auth/authentication.service.ts

Description

Responsible for saving user name, id and jwt token in localstorage and exposing the name and id through currentUserSubject

Index

Properties
Methods
Accessors

Constructor

constructor(http: HttpClient, router: Router)
Parameters :
Name Type Optional
http HttpClient No
router Router No

Methods

fetchUserFromStorage
fetchUserFromStorage()
Returns : User
login
login(username: string, password: string)

If login is sucessfull save the user name, id and jwt token to local storage and expose the name and id through currentUserSubject. If the login fails the returned observable errors.

Parameters :
Name Type Optional Description
username string No

provided by user to LoginComponent

password string No

provided by user to LoginComponent

Returns : Observable<User>

the logged in user, errors if login is not successful

logout
logout()

remove user from local storage and set subject holding the current user to null

Returns : void
saveUserToStorage
saveUserToStorage(user: User)
Parameters :
Name Type Optional
user User No
Returns : void

Properties

Public currentUser
Type : Observable<User>
Private currentUserSubject
Type : BehaviorSubject<User>
Readonly userStorageKey
Type : string
Default value : 'currentUser'

Accessors

currentUserValue
getcurrentUserValue()
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {BehaviorSubject, Observable} from 'rxjs';
import {environment} from '@environments/environment';
import {Router} from '@angular/router';
import {map} from 'rxjs/operators';

/**
 * Responsible for saving user name, id and jwt token in localstorage
 * and exposing the name and id through currentUserSubject
 */
@Injectable({providedIn: 'root'})
export class AuthenticationService {
  readonly userStorageKey = 'currentUser';
  private currentUserSubject: BehaviorSubject<User>;
  public currentUser: Observable<User>;

  constructor(private http: HttpClient, private router: Router) {
    this.currentUserSubject = new BehaviorSubject<User>(this.fetchUserFromStorage());
    this.currentUser = this.currentUserSubject.asObservable();
  }

  public get currentUserValue(): User {
    return this.currentUserSubject.value;
  }

  fetchUserFromStorage(): User {
    return JSON.parse(localStorage.getItem(this.userStorageKey));
  }

  saveUserToStorage(user: User): void {
    localStorage.setItem(this.userStorageKey, JSON.stringify(user));
  }

  /**
   * If login is sucessfull save the user name, id and jwt token to local storage and expose the name and id
   * through currentUserSubject. If the login fails the returned observable errors.
   * @param username provided by user to LoginComponent
   * @param password provided by user to LoginComponent
   * @returns the logged in user, errors if login is not successful
   */
  login(username: string, password: string): Observable<User> {
    return this.http.post<any>(environment.loginUrl, {username, password}).pipe(
      map((response) => {
        // store user details and jwt token in local storage to keep user logged in between page refreshes
        localStorage.setItem('token', response.token);
        const tokenPayload = JSON.parse(atob(response.token.split('.')[1]));
        const user = {name: tokenPayload.name, id: tokenPayload.sub};
        this.saveUserToStorage(user);
        this.currentUserSubject.next(user);
        return user;
      })
    );
  }

  /**
   * remove user from local storage and set subject holding the current user to null
   */
  logout(): void {
    localStorage.removeItem('currentUser');
    localStorage.removeItem('token');
    this.router.navigate(['login']);
    this.currentUserSubject.next(null);
  }
}

interface User {
  name: string;
  id: string;
}

results matching ""

    No results matching ""