16
src/app/services/api.service.spec.ts
Normal file
16
src/app/services/api.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
27
src/app/services/api.service.ts
Normal file
27
src/app/services/api.service.ts
Normal 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');
|
||||
}
|
||||
}
|
||||
16
src/app/services/auth-guard.service.spec.ts
Normal file
16
src/app/services/auth-guard.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
22
src/app/services/auth-guard.service.ts
Normal file
22
src/app/services/auth-guard.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
16
src/app/services/auth-interceptor.service.spec.ts
Normal file
16
src/app/services/auth-interceptor.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
27
src/app/services/auth-interceptor.service.ts
Normal file
27
src/app/services/auth-interceptor.service.ts
Normal 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() { }
|
||||
}
|
||||
16
src/app/services/auth.service.spec.ts
Normal file
16
src/app/services/auth.service.spec.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
67
src/app/services/auth.service.ts
Normal file
67
src/app/services/auth.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user