Select Git revision
auth.service.ts
auth.service.ts 3.46 KiB
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { Observable, BehaviorSubject } from 'rxjs';
import { tap, map } from 'rxjs/operators';
import { environment } from 'environments/environment';
import { User } from './models';
import { ObjectStoredItem, SimpleStoredItem } from '../storage';
import { UserAdapter } from './adapters';
import { HttpParams } from '@angular/common/http';
class StoredUser extends ObjectStoredItem<User> { key = 'oser-cs-user-info'; }
class StoredToken extends SimpleStoredItem { key = 'oser-cs-user-token'; }
@Injectable({
providedIn: 'root',
})
export class AuthService {
private loginUrl = environment.apiUrl + 'auth/get-token/';
private resetUrl = environment.apiUrl + 'rest-auth/password/reset/';
private resetConfirmUrl = environment.apiUrl + 'rest-auth/password/reset/confirm/';
private mandatorySignatureUrl = environment.apiUrl + 'charter/';
//add the corresponding path in backend interface
fromGuard: boolean;
redirectUrl: string;
fromUnauthorized: boolean;
private userAdapter = new UserAdapter();
private user = new StoredUser();
private token = new StoredToken();
private user$: BehaviorSubject<User>;
constructor(private http: HttpClient, private router: Router) {
const userData = this.getUserSnapshot();
const initialUser = userData ? new User(userData) : null;
this.user$ = new BehaviorSubject(initialUser);
}
login(username: string, password: string): Observable<boolean> {
return this.http.post<any>(this.loginUrl, { username: username, password: password }).pipe(
tap(data => this.token.set(data.token)),
map(data => this.userAdapter.adapt(data.user)),
tap((user: User) => this.user.set(user)),
tap((user: User) => this.user$.next(user)),
map(() => true),
);
}
reset(email: string): Observable<boolean> {
console.log("reset function of auth service");
return this.http.post<any>(this.resetUrl, { email }).pipe(
map(() => true),
);
}
resetConfirm(uid: string, token: string, new_password1: string, new_password2: string): Observable<boolean> {
console.log("reset confirm function");
return this.http.post<any>(this.resetConfirmUrl, { uid, token, new_password1, new_password2 }).pipe(
map(() => true),
);
}
redirectLogin() {
this.router.navigate(['/connexion']);
}