🔒 Refactor authentication handling by replacing LocalStorage/Login/Register services with AuthService across components and services
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
1109094599
commit
4279711d5c
@ -1,6 +1,6 @@
|
||||
<app-navbar *ngIf="localStorageService.getToken()"></app-navbar>
|
||||
<app-home-navbar *ngIf="!localStorageService.getToken()"></app-home-navbar>
|
||||
<app-navbar *ngIf="authService.isLoggedIn()"></app-navbar>
|
||||
<app-home-navbar *ngIf="!authService.isLoggedIn()"></app-home-navbar>
|
||||
|
||||
<router-outlet />
|
||||
|
||||
<app-admin-footer *ngIf="localStorageService.getIsAdmin() === 'true'"></app-admin-footer>
|
||||
<app-admin-footer *ngIf="authService.isAdmin()"></app-admin-footer>
|
||||
|
@ -0,0 +1,6 @@
|
||||
export interface AuthResponse {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
user_id: string;
|
||||
is_admin: boolean;
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { LoginService } from './login.service';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
describe('LoginService', () => {
|
||||
let service: LoginService;
|
||||
describe('AuthService', () => {
|
||||
let service: AuthService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(LoginService);
|
||||
service = TestBed.inject(AuthService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
@ -0,0 +1,80 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
||||
import { BehaviorSubject, Observable, tap } from 'rxjs';
|
||||
import { environment } from '../../../environment';
|
||||
import { LocalStorageService } from '../local-storage/local-storage.service';
|
||||
import { AuthResponse } from '../../model/AuthResponse';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthService {
|
||||
private isAdminSubject = new BehaviorSubject<boolean>(false);
|
||||
isAdmin$ = this.isAdminSubject.asObservable();
|
||||
username$ = new BehaviorSubject<string>('');
|
||||
isLoggedIn$ = new BehaviorSubject<boolean>(false);
|
||||
|
||||
constructor(private http: HttpClient, private localStorageService: LocalStorageService) {
|
||||
const token = this.localStorageService.getToken();
|
||||
if (token) {
|
||||
this.isAdminSubject.next(this.localStorageService.getIsAdmin() === 'true');
|
||||
this.username$.next(this.localStorageService.getUsername() || '');
|
||||
this.isLoggedIn$.next(true);
|
||||
}
|
||||
}
|
||||
|
||||
login(username: string, password: string): Observable<AuthResponse> {
|
||||
const payload = new HttpParams()
|
||||
.set('username', username)
|
||||
.set('password', password);
|
||||
|
||||
return this.http.post<AuthResponse>(`${environment.apiURL}/login`, payload).pipe(
|
||||
tap(response => {
|
||||
this.localStorageService.setToken(response.access_token);
|
||||
this.localStorageService.setIsAdmin(response.is_admin);
|
||||
this.localStorageService.setUsername(username);
|
||||
this.isAdminSubject.next(response.is_admin);
|
||||
this.username$.next(username);
|
||||
this.isLoggedIn$.next(true);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
this.localStorageService.removeToken();
|
||||
this.localStorageService.removeIsAdmin();
|
||||
this.isAdminSubject.next(false);
|
||||
this.username$.next('');
|
||||
this.isLoggedIn$.next(false);
|
||||
}
|
||||
|
||||
register(username: string, password: string): Observable<AuthResponse> {
|
||||
return this.http.post<AuthResponse>(`${environment.apiURL}/register`, { username, password }).pipe(
|
||||
tap(response => {
|
||||
this.localStorageService.setToken(response.access_token);
|
||||
this.localStorageService.setIsAdmin(response.is_admin);
|
||||
this.localStorageService.setUsername(username);
|
||||
this.isAdminSubject.next(response.is_admin);
|
||||
this.username$.next(username);
|
||||
this.isLoggedIn$.next(true);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
isAdmin(): boolean {
|
||||
return this.isAdminSubject.value || false;
|
||||
}
|
||||
|
||||
getAuthHeaders(): HttpHeaders {
|
||||
const token = this.localStorageService.getToken();
|
||||
return new HttpHeaders().set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
getUsername(): string {
|
||||
return this.username$.value || '';
|
||||
}
|
||||
|
||||
isLoggedIn(): boolean {
|
||||
return this.isLoggedIn$.value || false;
|
||||
}
|
||||
}
|
@ -1,65 +1,60 @@
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { environment } from '../../../environment';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class FriendsService {
|
||||
private apiURL = environment.apiURL;
|
||||
constructor(private http: HttpClient) {}
|
||||
constructor(private http: HttpClient, private authService: AuthService) {}
|
||||
|
||||
getFriend() {
|
||||
const url = `${this.apiURL}/friends`;
|
||||
const headers = new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
||||
});
|
||||
const headers = this.authService.getAuthHeaders();
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
return this.http.get<any[]>(url, { headers });
|
||||
}
|
||||
|
||||
getFriendById(id: string) {
|
||||
const url = `${this.apiURL}/user/${id}`;
|
||||
const headers = new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
||||
});
|
||||
const headers = this.authService.getAuthHeaders();
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
return this.http.get<any>(url, { headers });
|
||||
}
|
||||
|
||||
addFriend(user_id: string) {
|
||||
const url = `${this.apiURL}/friend/add`;
|
||||
const headers = new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
||||
});
|
||||
const headers = this.authService.getAuthHeaders();
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
return this.http.post<any>(url, { friend_user_id: user_id }, { headers });
|
||||
}
|
||||
|
||||
acceptFriendById(id: string) {
|
||||
const url = `${this.apiURL}/friend/${id}/accept`;
|
||||
const headers = new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
||||
});
|
||||
const headers = this.authService.getAuthHeaders();
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
return this.http.patch<any>(url, [], { headers });
|
||||
}
|
||||
|
||||
denyFriendById(id: string) {
|
||||
const url = `${this.apiURL}/friend/${id}/deny`;
|
||||
const headers = new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
||||
});
|
||||
const headers = this.authService.getAuthHeaders();
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
return this.http.delete<any>(url, { headers });
|
||||
}
|
||||
|
||||
deleteFriend(id: string) {
|
||||
const url = `${this.apiURL}/friend/${id}/delete`;
|
||||
const headers = new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
||||
});
|
||||
const headers = this.authService.getAuthHeaders();
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
return this.http.delete<any>(url, { headers });
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { environment } from '../../../environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class LoginService {
|
||||
private apiUrl = environment.apiURL;
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
login(username: string, password: string): Observable<any> {
|
||||
const payload = new HttpParams()
|
||||
.set('username', username)
|
||||
.set('password', password);
|
||||
|
||||
return this.http.post(this.apiUrl + '/login', payload);
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { RegisterService } from './register.service';
|
||||
|
||||
describe('RegisterService', () => {
|
||||
let service: RegisterService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(RegisterService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
@ -1,17 +0,0 @@
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { environment } from '../../../environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RegisterService {
|
||||
private apiUrl = environment.apiURL;
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
register(username: string, password: string): Observable<any> {
|
||||
return this.http.post(this.apiUrl + '/register', { username, password });
|
||||
}
|
||||
}
|
Loading…
Reference in new issue