TG-2 TG-9 - Push base Angular app

This commit is contained in:
2024-11-03 11:16:14 +01:00
parent cd590d21d9
commit 68d4b82756
36 changed files with 879 additions and 441 deletions

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ApiService } from './api.service';
describe('ApiService', () => {
let service: ApiService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ApiService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {UserData} from "../interface/user";
@Injectable({
providedIn: 'root'
})
export class ApiService {
private apiUrl = 'http://localhost:8080/api/';
constructor(
private http: HttpClient
) {
}
// v1 USERS
getCurrentUser() {
return this.http.get<UserData>(this.apiUrl + 'v1/users/me');
}
getAllUsers() {
return this.http.get(this.apiUrl + 'v1/users/all');
}
}

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { AuthGuardService } from './auth-guard.service';
describe('AuthGuardService', () => {
let service: AuthGuardService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthGuardService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from "@angular/router";
import {AuthService} from "./auth.service";
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(private router: Router, private authService: AuthService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authService.isLoggedIn()) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}
}

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { AuthInterceptorService } from './auth-interceptor.service';
describe('AuthInterceptorService', () => {
let service: AuthInterceptorService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthInterceptorService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Observable} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class AuthInterceptorService implements HttpInterceptor {
intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const tokenId = localStorage.getItem('tokenId');
if (!!tokenId) {
const cloned = req.clone({
headers: req.headers.set("Authorization",
"Bearer " + tokenId)
});
return next.handle(cloned);
}
return next.handle(req);
}
constructor() { }
}

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,67 @@
import { Injectable } from '@angular/core';
import {TokenResponse} from "../interface/auth";
import moment from "moment";
import {HttpClient} from "@angular/common/http";
import {tap} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class AuthService {
private apiUrl = 'http://localhost:8080/api/';
constructor(
private httpClient: HttpClient
) {
}
public register(email: string, username: string, password: string) {
const body = {
email: email,
username: username,
password: password
}
return this.httpClient.post(this.apiUrl + 'v1/auth/register', body);
}
public authenticate(email: string, password: string) {
const body = {
email: email,
password: password
}
return this.httpClient.post<TokenResponse>(this.apiUrl + 'v1/auth/login', body)
.pipe(tap(token => {this.setSession(token)}));
}
private setSession(res: TokenResponse) {
const expiresAt = moment().add(res.expiresIn, 'milliseconds');
localStorage.setItem('tokenId', res.token);
localStorage.setItem("expiresAt", JSON.stringify(expiresAt.valueOf()) );
}
public logout() {
localStorage.removeItem("tokenId");
localStorage.removeItem("expiresAt");
}
public isLoggedIn() {
return moment().isBefore(this.getExpiration());
}
isLoggedOut() {
return !this.isLoggedIn();
}
getExpiration() {
const expiration = localStorage.getItem("expiresAt");
const expiresAt = JSON.parse(expiration ? expiration : '{}');
return moment(expiresAt);
}
}