File

projects/web-mev/src/app/core/authentication/authentication.service.ts

Description

Authentication service

Used for user registration and authentication

Index

Properties
Methods
Accessors

Constructor

constructor(http: HttpClient)
Parameters :
Name Type Optional
http HttpClient No

Methods

confirmPasswordReset
confirmPasswordReset(body)

Confirm password reset

Parameters :
Name Optional
body No
Returns : Observable<any>
getJwtToken
getJwtToken()

Get user token from storage

Returns : string
Private getRefreshToken
getRefreshToken()

Get user refresh token from storage

Returns : any
googleSignInExternal
googleSignInExternal(googleTokenId: string)

Google Login

Parameters :
Name Type Optional
googleTokenId string No
Returns : Observable<any>
isLoggedIn
isLoggedIn()

Check if user is logged in

Returns : boolean
login
login(username: string, password: string)

User login

Parameters :
Name Type Optional
username string No
password string No
Returns : any
logout
logout()

User logout

Returns : void
newPassword
newPassword(body)

Update password

Parameters :
Name Optional
body No
Returns : Observable<any>
refreshToken
refreshToken()

Refresh token

Returns : any
requestPasswordReset
requestPasswordReset(body)

Request password reset

Parameters :
Name Optional
body No
Returns : Observable<any>
Private storeJwtToken
storeJwtToken(jwt: string)
Parameters :
Name Type Optional
jwt string No
Returns : void
Private storeRefreshToken
storeRefreshToken(token: string)

Update refresh token in storage

Parameters :
Name Type Optional
token string No
Returns : void
ValidPasswordToken
ValidPasswordToken(body)

Verify token after password reset

Parameters :
Name Optional
body No
Returns : Observable<any>

Properties

Private Readonly API_NAME
Default value : environment.appName
Private Readonly API_URL
Default value : environment.apiUrl
Public currentUser
Type : Observable<User>
Private currentUserSubject
Type : BehaviorSubject<User>
Private Readonly JWT_TOKEN
Default value : this.API_NAME + 'JWT_TOKEN'
Private Readonly REFRESH_TOKEN
Default value : this.API_NAME + 'REFRESH_TOKEN'

Accessors

currentUserValue
getcurrentUserValue()
import { Injectable } from '@angular/core';
import { environment } from '@environments/environment';
import { BehaviorSubject, Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { User } from '@app/_models/user';
import { HttpClient } from '@angular/common/http';

/**
 * Authentication service
 *
 * Used for user registration and authentication
 */
@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {
  private currentUserSubject: BehaviorSubject<User>;
  public currentUser: Observable<User>;
  private readonly API_URL = environment.apiUrl;
  private readonly API_NAME = environment.appName;

  private readonly JWT_TOKEN = this.API_NAME + 'JWT_TOKEN';
  private readonly REFRESH_TOKEN = this.API_NAME + 'REFRESH_TOKEN';

  constructor(private http: HttpClient) {
    this.currentUserSubject = new BehaviorSubject<User>(
      JSON.parse(sessionStorage.getItem(this.JWT_TOKEN))
    );
    this.currentUser = this.currentUserSubject.asObservable();
  }

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

  /**
   * User login
   *
   */
  login(username: string, password: string) {
    return this.http
      .post<any>(`${this.API_URL}/token/`, {
        email: username,
        password: password
      })
      .pipe(
        map(user => {
          // login successful if there's a token in the response: {'refresh': '<REFRESH TOKEN>', 'access': '<ACCESS_TOKEN>'}
          if (user && user.access) {
            this.storeJwtToken(JSON.stringify(user.access));
            this.storeRefreshToken(JSON.stringify(user.refresh));
            this.currentUserSubject.next(user);
          }
          return user;
        })
      );
  }

  // store user details and token in local storage to keep user logged in between page refreshes
  private storeJwtToken(jwt: string) {
    sessionStorage.setItem(this.JWT_TOKEN, jwt);
  }

  /**
   * Update refresh token in storage
   *
   */
  private storeRefreshToken(token: string) {
    sessionStorage.setItem(this.REFRESH_TOKEN, token);
  }

  /**
   * User logout
   *
   */
  logout() {
    // remove user from local storage to log user out
    sessionStorage.removeItem(this.JWT_TOKEN);
    this.currentUserSubject.next(null);
  }

  /**
   * Get user token from storage
   *
   */
  getJwtToken(): string {
    return JSON.parse(sessionStorage.getItem(this.JWT_TOKEN));
  }

  /**
   * Get user refresh token from storage
   *
   */
  private getRefreshToken() {
    return JSON.parse(sessionStorage.getItem(this.REFRESH_TOKEN));
  }

  /**
   * Check if user is logged in
   *
   */
  isLoggedIn() {
    return this.getJwtToken() !== null;
  }

  /**
   * Refresh token
   *
   */

  refreshToken() {
    return this.http
      .post<any>(`${this.API_URL}/token/refresh/`, {
        refresh: this.getRefreshToken()
      })
      .pipe(
        tap(tokens => {
          this.storeJwtToken(JSON.stringify(tokens.access));
        })
      );
  }

  /**
   * Google Login
   *
   */

  googleSignInExternal(googleTokenId: string): Observable<any> {
    return this.http
      .post<any>(`${this.API_URL}/users/social/google/`, {
        provider_token: googleTokenId
      })
      .pipe(
        map(token => {
          // login successful if there's a token in the response: {'refresh': '<REFRESH TOKEN>', 'access': '<ACCESS_TOKEN>'}
          if (token && token.access) {
            this.storeJwtToken(JSON.stringify(token.access));
            this.storeRefreshToken(JSON.stringify(token.refresh));
            this.currentUserSubject.next(token);
          }
          return token;
        })
      );
  }

  /**
   * Request password reset
   *
   */

  requestPasswordReset(body): Observable<any> {
    return this.http.post<any>(`${this.API_URL}/users/reset-password/`, body);
  }

  /**
   * Confirm password reset
   *
   */
  confirmPasswordReset(body): Observable<any> {
    // user has clicked on a reset link and is sending a UID (encoded), a token, a new password, and a re-typed confirmation of that password
    return this.http.post<any>(
      `${this.API_URL}/users/reset-password/confirm/`,
      body
    );
  }

  /**
   * Update password
   *
   */
  newPassword(body): Observable<any> {
    return this.http.post<any>(`${this.API_URL}/users/change-password/`, body);
  }

  /**
   * Verify token after password reset
   *
   */
  ValidPasswordToken(body): Observable<any> {
    return this.http.post<any>(`${this.API_URL}/users/activate/`, body);
  }
}

result-matching ""

    No results matching ""