🔒 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-navbar *ngIf="authService.isLoggedIn()"></app-navbar>
|
||||||
<app-home-navbar *ngIf="!localStorageService.getToken()"></app-home-navbar>
|
<app-home-navbar *ngIf="!authService.isLoggedIn()"></app-home-navbar>
|
||||||
|
|
||||||
<router-outlet />
|
<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 { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
import { LoginService } from './login.service';
|
import { AuthService } from './auth.service';
|
||||||
|
|
||||||
describe('LoginService', () => {
|
describe('AuthService', () => {
|
||||||
let service: LoginService;
|
let service: AuthService;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({});
|
TestBed.configureTestingModule({});
|
||||||
service = TestBed.inject(LoginService);
|
service = TestBed.inject(AuthService);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be created', () => {
|
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 { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { environment } from '../../../environment';
|
import { environment } from '../../../environment';
|
||||||
|
import { AuthService } from '../auth/auth.service';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class FriendsService {
|
export class FriendsService {
|
||||||
private apiURL = environment.apiURL;
|
private apiURL = environment.apiURL;
|
||||||
constructor(private http: HttpClient) {}
|
constructor(private http: HttpClient, private authService: AuthService) {}
|
||||||
|
|
||||||
getFriend() {
|
getFriend() {
|
||||||
const url = `${this.apiURL}/friends`;
|
const url = `${this.apiURL}/friends`;
|
||||||
const headers = new HttpHeaders({
|
const headers = this.authService.getAuthHeaders();
|
||||||
'Content-Type': 'application/json',
|
headers.set('Content-Type', 'application/json');
|
||||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
|
||||||
});
|
|
||||||
return this.http.get<any[]>(url, { headers });
|
return this.http.get<any[]>(url, { headers });
|
||||||
}
|
}
|
||||||
|
|
||||||
getFriendById(id: string) {
|
getFriendById(id: string) {
|
||||||
const url = `${this.apiURL}/user/${id}`;
|
const url = `${this.apiURL}/user/${id}`;
|
||||||
const headers = new HttpHeaders({
|
const headers = this.authService.getAuthHeaders();
|
||||||
'Content-Type': 'application/json',
|
headers.set('Content-Type', 'application/json');
|
||||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
|
||||||
});
|
|
||||||
return this.http.get<any>(url, { headers });
|
return this.http.get<any>(url, { headers });
|
||||||
}
|
}
|
||||||
|
|
||||||
addFriend(user_id: string) {
|
addFriend(user_id: string) {
|
||||||
const url = `${this.apiURL}/friend/add`;
|
const url = `${this.apiURL}/friend/add`;
|
||||||
const headers = new HttpHeaders({
|
const headers = this.authService.getAuthHeaders();
|
||||||
'Content-Type': 'application/json',
|
headers.set('Content-Type', 'application/json');
|
||||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
|
||||||
});
|
|
||||||
return this.http.post<any>(url, { friend_user_id: user_id }, { headers });
|
return this.http.post<any>(url, { friend_user_id: user_id }, { headers });
|
||||||
}
|
}
|
||||||
|
|
||||||
acceptFriendById(id: string) {
|
acceptFriendById(id: string) {
|
||||||
const url = `${this.apiURL}/friend/${id}/accept`;
|
const url = `${this.apiURL}/friend/${id}/accept`;
|
||||||
const headers = new HttpHeaders({
|
const headers = this.authService.getAuthHeaders();
|
||||||
'Content-Type': 'application/json',
|
headers.set('Content-Type', 'application/json');
|
||||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
|
||||||
});
|
|
||||||
return this.http.patch<any>(url, [], { headers });
|
return this.http.patch<any>(url, [], { headers });
|
||||||
}
|
}
|
||||||
|
|
||||||
denyFriendById(id: string) {
|
denyFriendById(id: string) {
|
||||||
const url = `${this.apiURL}/friend/${id}/deny`;
|
const url = `${this.apiURL}/friend/${id}/deny`;
|
||||||
const headers = new HttpHeaders({
|
const headers = this.authService.getAuthHeaders();
|
||||||
'Content-Type': 'application/json',
|
headers.set('Content-Type', 'application/json');
|
||||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
|
||||||
});
|
|
||||||
return this.http.delete<any>(url, { headers });
|
return this.http.delete<any>(url, { headers });
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteFriend(id: string) {
|
deleteFriend(id: string) {
|
||||||
const url = `${this.apiURL}/friend/${id}/delete`;
|
const url = `${this.apiURL}/friend/${id}/delete`;
|
||||||
const headers = new HttpHeaders({
|
const headers = this.authService.getAuthHeaders();
|
||||||
'Content-Type': 'application/json',
|
headers.set('Content-Type', 'application/json');
|
||||||
Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
|
|
||||||
});
|
|
||||||
return this.http.delete<any>(url, { headers });
|
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