import { Injectable } from "@angular/core" ; import { CredenciaisDTO } from "../models/credenciais.dto" ; import { HttpClient } from "@angular/common/http" ; import { API_CONFIG } from "../config/api.config" ; import { LocalUser } from "../models/local_user" ; import { StorageService } from "./storage.service" ; import { JwtHelper } from 'angular2-jwt' ; import { CartService } from "./domain/cart.service" ; @ Injectable ( ) export class AuthService { jwtHelper : JwtHelper = new JwtHelper ( ) ; constructor ( public http , public storage , public cartService ) { } authenticate ( creds ) { return this . http . post ( `${ API_CONFIG . baseUrl }/login` , creds , { observe : 'response' , responseType : 'text' } ) ; } refreshToken ( ) { return this . http . post ( `${ API_CONFIG . baseUrl }/auth/refresh_token` , { } , { observe : 'response' , responseType : 'text' } ) ; } successfulLogin ( authorizationValue ) { let tok = authorizationValue . substring ( 7 ) ; let user = { token : tok , email : this . jwtHelper . decodeToken ( tok ) . sub } ; this . storage . setLocalUser ( user ) ; this . cartService . createOrClearCart ( ) ; } logout ( ) { this . storage . setLocalUser ( null ) ; } }