aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/auth.service.ts
diff options
context:
space:
mode:
authorDanijel Andjelkovic <adanijel99@gmail.com>2022-03-05 19:46:16 +0100
committerDanijel Andjelkovic <adanijel99@gmail.com>2022-03-05 19:46:16 +0100
commit9b925712cbdedacdf8af947e929ca3975b56e3fa (patch)
tree5a1c230a2c9484dd610df74b09c861fb20b2d853 /frontend/src/app/auth.service.ts
parentcfadf0c8edfc66e54df8186d98b968c5682a3356 (diff)
Added auth and auth guard services, included ngx-cookie-service and angular-jwt. Made a configuration file.
Diffstat (limited to 'frontend/src/app/auth.service.ts')
-rw-r--r--frontend/src/app/auth.service.ts24
1 files changed, 23 insertions, 1 deletions
diff --git a/frontend/src/app/auth.service.ts b/frontend/src/app/auth.service.ts
index af27fdec..29dca31d 100644
--- a/frontend/src/app/auth.service.ts
+++ b/frontend/src/app/auth.service.ts
@@ -1,9 +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() { }
+ constructor(private http: HttpClient, private cookie: CookieService) { }
+
+ login(username: string, password: string) {
+ return this.http.post(`${API_SETTINGS.apiURL}/login`, { username, password });
+ }
+
+ register(username: string, password: string) {
+ return this.http.post(`${API_SETTINGS.apiURL}/register`, { username, password });
+ }
+
+ isAuthenticated(): boolean {
+ if (this.cookie.check('token')) {
+ var token = this.cookie.get('token');
+ return !jwtHelper.isTokenExpired(token);
+ }
+ return false;
+ }
}