File

modules/auth-service/src/auth.service.ts

Description

Service that provide user data and methods to work with auth

Constructor

constructor(dal: any, sessionFlow: any, zone: any)

Methods

logout
logout()

Logout method

Returns: void
register
register(registerForm: any)

Register user

Parameters :
  • registerForm
Returns: void

userData

login
login(email: any, password: any)

Login with email and password

Parameters :
  • email

    user email

  • password

    user password

Returns: void

user data

resetPassword
resetPassword(email: any)

Reset user password

Parameters :
  • email

    user email

Returns: void

Promise containing null

Properties

userData
userData: {}

User data

import { SessionFlow } from '@nodeart/session-flow';
import {Injectable, NgZone} from "@angular/core";
import {DbAbstractionLayer} from "@nodeart/dal";
/**
 * Service that provide user data and methods to work with auth
 */
@Injectable()
export class AuthService {
  
  /**
   * User data
   */
  userData = {};
  constructor(protected dal: DbAbstractionLayer, protected sessionFlow: SessionFlow, protected zone: NgZone){
    this.dal.getAuth().onAuthStateChanged(authData => {
      if(authData === null){
        this.sessionFlow.userId = 'guest';
        this.userData = {};
        this.dal.initializeBasketHistory(sessionFlow.deviceId);
      }
      if(authData){
        let uid = authData.uid;
        this.userData = {};
        this.userData['uid'] = uid;
        this.dal.getUserData(uid).subscribe( data => {
          if(data.val()){
            let userData = data.val()[0]['_source'];
            userData.userId = data.val()[0]['_id'];
            this.sessionFlow.userId = data.val()[0]['_id'];
            this.dal.initializeBasketHistory(userData.userId);
            this.zone.run(() => {
              this.userData = userData;
            });
          }
        });
      }
    });
  }


  /**
   * Logout method
   */
  logout(){
    return this.dal.logout();
  }

  /**
   * Register user
   * @param registerForm
   * @returns {Object} userData 
   */
  register(registerForm){
    return this.dal.registerUser(registerForm);
  }

  /**
   * Login with email and password
   * @param email user email
   * @param password user password
   * 
   * @returns user data
   */
  login(email, password){
    return this.dal.loginEmail(email, password);
  }

  /**
   * Reset user password
   * @param email user email
   * @returns Promise containing null
   */
  resetPassword(email) {
    return this.dal.resetPassword(email);
  }
}

results matching ""

    No results matching ""