first commit

This commit is contained in:
phil 2024-10-19 11:53:15 +02:00
commit 62506c830a
1207 changed files with 40706 additions and 0 deletions

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, RouterStateSnapshot, Router } from '@angular/router'
import { Observable } from 'rxjs'
import { AuthService } from './auth.service'
@Injectable({
providedIn: 'root'
})
export class AuthGuardService {constructor(
private _authService: AuthService,
private _router: Router
) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if (this._authService.getToken()) {
return true
}
// navigate to login page
this._router.navigate(['/login'])
// you can save redirect url so after authing we can move them back to the page they requested
return false
}
}

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,33 @@
import { Injectable } from '@angular/core'
import { Router, ActivatedRoute, ParamMap } from '@angular/router'
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(
public router: Router,
public route: ActivatedRoute,
) {
}
getUserDetails() {
return localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
}
setDataInLocalStorage(variableName, data) {
localStorage.setItem(variableName, data)
}
getToken() {
return localStorage.getItem('token')
}
clearToken() {
localStorage.removeItem('token')
}
clearStorage() {
localStorage.clear()
}
}

View file

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

View file

@ -0,0 +1,34 @@
import { Injectable } from '@angular/core'
import { HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http'
import { Observable } from 'rxjs'
import { AuthService } from './auth.service'
@Injectable({
providedIn: 'root'
})
export class InterceptorService {
constructor(
private _auth: AuthService
) {
}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
// XXX: Breaks form/multipart HTTP POST requests
//if (!request.headers.has('Content-Type')) {
// request = request.clone({ headers: request.headers.set('Content-Type', 'application/json') })
//}
//request = request.clone({ headers: request.headers.set('Accept', 'application/json') }).clone({
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this._auth.getToken()}`
}
})
return next.handle(request)
}
}