diff options
author | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-03-09 22:19:17 +0100 |
---|---|---|
committer | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-03-09 22:19:17 +0100 |
commit | cbdc861b07e82502664f1c849f141a51104cd3d0 (patch) | |
tree | 9959995ea265df2d0be6ff307838a957b813f88e /frontend/src/app/_services | |
parent | 8e7f2346cc4507b7e8056b57e01c0e85c11a4a2c (diff) | |
parent | a95a921478435cc6400236d291281c3d0156a7e1 (diff) |
Merge branch 'dev' of http://gitlab.pmf.kg.ac.rs/igrannonica/neuronstellar into microservice
Diffstat (limited to 'frontend/src/app/_services')
-rw-r--r-- | frontend/src/app/_services/auth-guard.service.spec.ts | 16 | ||||
-rw-r--r-- | frontend/src/app/_services/auth-guard.service.ts | 21 | ||||
-rw-r--r-- | frontend/src/app/_services/auth.service.spec.ts | 16 | ||||
-rw-r--r-- | frontend/src/app/_services/auth.service.ts | 31 |
4 files changed, 84 insertions, 0 deletions
diff --git a/frontend/src/app/_services/auth-guard.service.spec.ts b/frontend/src/app/_services/auth-guard.service.spec.ts new file mode 100644 index 00000000..35afd377 --- /dev/null +++ b/frontend/src/app/_services/auth-guard.service.spec.ts @@ -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(); + }); +}); diff --git a/frontend/src/app/_services/auth-guard.service.ts b/frontend/src/app/_services/auth-guard.service.ts new file mode 100644 index 00000000..b6d3678d --- /dev/null +++ b/frontend/src/app/_services/auth-guard.service.ts @@ -0,0 +1,21 @@ +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; +import { Observable } from 'rxjs'; +import { AuthService } from './auth.service'; + + +@Injectable({ + providedIn: 'root' +}) +export class AuthGuardService implements CanActivate { + + constructor(private auth: AuthService, private router: Router) { } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> { + if (this.auth.isAuthenticated()) { + return true; + } + this.router.navigate(['login']); + return false; + } +} diff --git a/frontend/src/app/_services/auth.service.spec.ts b/frontend/src/app/_services/auth.service.spec.ts new file mode 100644 index 00000000..f1251cac --- /dev/null +++ b/frontend/src/app/_services/auth.service.spec.ts @@ -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(); + }); +}); diff --git a/frontend/src/app/_services/auth.service.ts b/frontend/src/app/_services/auth.service.ts new file mode 100644 index 00000000..7129b95b --- /dev/null +++ b/frontend/src/app/_services/auth.service.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { JwtHelperService } from '@auth0/angular-jwt'; +import { CookieService } from 'ngx-cookie-service'; +import { API_SETTINGS } from 'src/config'; + +const jwtHelper = new JwtHelperService(); + +@Injectable({ + providedIn: 'root' +}) +export class AuthService { + + constructor(private http: HttpClient, private cookie: CookieService) { } + + login(username: string, password: string) { + return this.http.post(`${API_SETTINGS.apiURL}/auth/login`, { username, password }, { responseType: 'text' }); + } + + register(user: any) { + return this.http.post(`${API_SETTINGS.apiURL}/auth/register`, user); + } + + isAuthenticated(): boolean { + if (this.cookie.check('token')) { + var token = this.cookie.get('token'); + return !jwtHelper.isTokenExpired(token); + } + return false; + } +} |