authentication
This commit is contained in:
108
client/src/app/core/auth/auth.service.ts
Normal file
108
client/src/app/core/auth/auth.service.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { ActivatedRoute } from '@angular/router';
|
||||
import { BehaviorSubject, Observable, tap, of, catchError } from 'rxjs';
|
||||
import { IUser } from '../../model/interface/user.interface';
|
||||
import { environment } from '../../../environments/environment.development';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthService {
|
||||
|
||||
private accessTokenSubject = new BehaviorSubject<string | null>(null);
|
||||
private refreshToken: string | null = null;
|
||||
private http: HttpClient = inject(HttpClient);
|
||||
private route: ActivatedRoute = inject(ActivatedRoute);
|
||||
|
||||
private user: IUser | null = null;
|
||||
|
||||
constructor() {
|
||||
const token = localStorage.getItem('accessToken_vault');
|
||||
const refresh = localStorage.getItem('refreshToken_vault');
|
||||
|
||||
this.accessTokenSubject.next(token);
|
||||
this.refreshToken = refresh;
|
||||
}
|
||||
|
||||
|
||||
getMe() {
|
||||
if (!this.getAccessToken()) {
|
||||
return false;
|
||||
}
|
||||
return new Promise(resolve => {
|
||||
this.http.get<IUser>('/api/auth/me').subscribe({
|
||||
next: user => {
|
||||
this.user = user;
|
||||
resolve(true)
|
||||
},
|
||||
error: () => {
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
authenticateWithCode(authcode: string) {
|
||||
return new Promise(resolve => {
|
||||
this.http.post<IUser>('/api/auth/auth-code', { code: authcode }).subscribe(user => {
|
||||
this.setTokens({ accessToken: user.accessToken, refreshToken: user.refreshToken});
|
||||
this.user = user;
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
get authenticated(): boolean {
|
||||
return this.user != null;
|
||||
}
|
||||
|
||||
|
||||
login(credentials: { username: string; password: string }): Observable<any> {
|
||||
return this.http.post<any>('/api/auth/login', credentials).pipe(
|
||||
tap(tokens => {
|
||||
this.setTokens(tokens);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private setTokens(tokens: { accessToken: string; refreshToken: string }) {
|
||||
this.accessTokenSubject.next(tokens.accessToken);
|
||||
this.refreshToken = tokens.refreshToken;
|
||||
localStorage.setItem('accessToken_vault', tokens.accessToken);
|
||||
localStorage.setItem('refreshToken_vault', tokens.refreshToken);
|
||||
}
|
||||
|
||||
getAccessToken(): string | null {
|
||||
return this.accessTokenSubject.value;
|
||||
}
|
||||
|
||||
refreshAccessToken(): Observable<any> {
|
||||
if (!this.refreshToken) {
|
||||
return of(null);
|
||||
}
|
||||
|
||||
return this.http.post<any>('/api/auth/refresh', { refreshToken: this.refreshToken }).pipe(
|
||||
tap(tokens => {
|
||||
this.setTokens(tokens);
|
||||
}),
|
||||
catchError(() => {
|
||||
this.logout();
|
||||
return of(null);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
logout() {
|
||||
this.accessTokenSubject.next(null);
|
||||
this.refreshToken = null;
|
||||
localStorage.removeItem('accessToken_vault');
|
||||
localStorage.removeItem('refreshToken_vault');
|
||||
}
|
||||
|
||||
public routeToLogin() {
|
||||
const url = `https://sso.beantastic.de?client_id=ffc46841-26f8-4946-a57a-5a9f8f21bc13&redirect_uri=${environment.location}`;
|
||||
location.href = url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user