@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders , HttpParams } from '@angular/common/http' ;
import { BehaviorSubject , Observable , tap } from 'rxjs' ;
import { environment } from '../../../environment' ;
import { LocalStorage Service } from '../cookies/cookies.service' ;
import { Cookies Service } from '../cookies/cookies.service' ;
import { AuthResponse } from '../../model/AuthResponse' ;
@Injectable ( {
@ -14,11 +14,11 @@ export class AuthService {
username $ = new BehaviorSubject < string > ( '' ) ;
isLoggedIn $ = new BehaviorSubject < boolean > ( false ) ;
constructor ( private http : HttpClient , private localStorageService: LocalStorage Service) {
const token = this . localStorage Service. getToken ( ) ;
constructor ( private http : HttpClient , private cookiesService: Cookies Service) {
const token = this . cookies Service. getToken ( ) ;
if ( token ) {
this . isAdminSubject . next ( this . localStorage Service. getIsAdmin ( ) === 'true' ) ;
this . username $ . next ( this . localStorage Service. getUsername ( ) || '' ) ;
this . isAdminSubject . next ( this . cookies Service. getIsAdmin ( ) === 'true' ) ;
this . username $ . next ( this . cookies Service. getUsername ( ) || '' ) ;
this . isLoggedIn $ . next ( true ) ;
}
}
@ -30,9 +30,9 @@ export class AuthService {
return this . http . post < AuthResponse > ( ` ${ environment . apiURL } /login ` , payload ) . pipe (
tap ( response = > {
this . localStorage Service. setToken ( response . access_token ) ;
this . localStorage Service. setIsAdmin ( response . is_admin ) ;
this . localStorage Service. setUsername ( username ) ;
this . cookies Service. setToken ( response . access_token ) ;
this . cookies Service. setIsAdmin ( response . is_admin ) ;
this . cookies Service. setUsername ( username ) ;
this . isAdminSubject . next ( response . is_admin ) ;
this . username $ . next ( username ) ;
this . isLoggedIn $ . next ( true ) ;
@ -41,8 +41,8 @@ export class AuthService {
}
logout ( ) : void {
this . localStorage Service. removeToken ( ) ;
this . localStorage Service. removeIsAdmin ( ) ;
this . cookies Service. removeToken ( ) ;
this . cookies Service. removeIsAdmin ( ) ;
this . isAdminSubject . next ( false ) ;
this . username $ . next ( '' ) ;
this . isLoggedIn $ . next ( false ) ;
@ -51,9 +51,9 @@ export class AuthService {
register ( username : string , password : string ) : Observable < AuthResponse > {
return this . http . post < AuthResponse > ( ` ${ environment . apiURL } /register ` , { username , password } ) . pipe (
tap ( response = > {
this . localStorage Service. setToken ( response . access_token ) ;
this . localStorage Service. setIsAdmin ( response . is_admin ) ;
this . localStorage Service. setUsername ( username ) ;
this . cookies Service. setToken ( response . access_token ) ;
this . cookies Service. setIsAdmin ( response . is_admin ) ;
this . cookies Service. setUsername ( username ) ;
this . isAdminSubject . next ( response . is_admin ) ;
this . username $ . next ( username ) ;
this . isLoggedIn $ . next ( true ) ;
@ -66,7 +66,7 @@ export class AuthService {
}
getAuthHeaders ( ) : HttpHeaders {
const token = this . localStorage Service. getToken ( ) ;
const token = this . cookies Service. getToken ( ) ;
return new HttpHeaders ( ) . set ( 'Authorization' , ` Bearer ${ token } ` ) ;
}