12 changed files with 128 additions and 127 deletions
@ -1,76 +0,0 @@ |
|||||
import { Injectable } from '@angular/core'; |
|
||||
import { HttpClient, HttpHeaders } from "@angular/common/http"; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { jwtDecode } from "jwt-decode"; |
|
||||
import { ServerEndpointService } from './server-endpoint.service'; |
|
||||
|
|
||||
import { AuthService as Auth0Service } from '@auth0/auth0-angular'; |
|
||||
|
|
||||
@Injectable({ |
|
||||
providedIn: 'root' |
|
||||
}) |
|
||||
export class AuthService { |
|
||||
|
|
||||
_isAuthenticated:boolean = false; |
|
||||
|
|
||||
constructor( |
|
||||
private httpClient: HttpClient, |
|
||||
private serverEndpointService: ServerEndpointService, |
|
||||
private auth0: Auth0Service |
|
||||
) |
|
||||
{ |
|
||||
localStorage.removeItem('token'); |
|
||||
|
|
||||
this.auth0.isAuthenticated$.subscribe(authed => { |
|
||||
this._isAuthenticated = authed; |
|
||||
}); |
|
||||
|
|
||||
this.auth0.user$.subscribe(user => { |
|
||||
console.log(user); |
|
||||
}) |
|
||||
|
|
||||
this.auth0.idTokenClaims$.subscribe(data => { |
|
||||
console.log(data) |
|
||||
if (data && data.__raw) { |
|
||||
localStorage.setItem('token', data.__raw); |
|
||||
} |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
login() { |
|
||||
this.auth0.loginWithRedirect(); |
|
||||
} |
|
||||
|
|
||||
testProfile(): Observable<any> { |
|
||||
return this.httpClient.get(this.serverEndpointService.getCurrentEndpoint()+"authz/test") |
|
||||
} |
|
||||
|
|
||||
logout(): void { |
|
||||
this.auth0.logout(); |
|
||||
localStorage.removeItem('token'); |
|
||||
} |
|
||||
|
|
||||
isAuthenticated(): boolean { |
|
||||
if(!this._isAuthenticated) { |
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
const token = localStorage.getItem('token'); |
|
||||
|
|
||||
if (!token) { |
|
||||
return false; |
|
||||
} |
|
||||
|
|
||||
try { |
|
||||
const decoded: any = jwtDecode(token); |
|
||||
|
|
||||
// Check if the token is expired
|
|
||||
const isTokenExpired = decoded.exp < Date.now() / 1000; |
|
||||
|
|
||||
return !isTokenExpired; |
|
||||
} catch (error) { |
|
||||
console.error('Error decoding JWT:', error); |
|
||||
return false; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,13 +1,13 @@ |
|||||
import { TestBed } from '@angular/core/testing'; |
import { TestBed } from '@angular/core/testing'; |
||||
|
|
||||
import { AuthService } from './auth.service'; |
import { UsersService } from './users.service'; |
||||
|
|
||||
describe('AuthService', () => { |
describe('UsersService', () => { |
||||
let service: AuthService; |
let service: UsersService; |
||||
|
|
||||
beforeEach(() => { |
beforeEach(() => { |
||||
TestBed.configureTestingModule({}); |
TestBed.configureTestingModule({}); |
||||
service = TestBed.inject(AuthService); |
service = TestBed.inject(UsersService); |
||||
}); |
}); |
||||
|
|
||||
it('should be created', () => { |
it('should be created', () => { |
||||
@ -0,0 +1,67 @@ |
|||||
|
import { Injectable } from '@angular/core'; |
||||
|
import { AuthService, User } from '@auth0/auth0-angular'; |
||||
|
|
||||
|
import * as jwt_decode from 'jwt-decode'; |
||||
|
|
||||
|
@Injectable({ |
||||
|
providedIn: 'root' |
||||
|
}) |
||||
|
export class UsersService { |
||||
|
|
||||
|
isAuthenticated = false; |
||||
|
permissions: string[] = []; |
||||
|
user: User | null | undefined = null; |
||||
|
|
||||
|
constructor( |
||||
|
public authService: AuthService, |
||||
|
) { } |
||||
|
|
||||
|
private hasPermission(permission: string) { |
||||
|
return this.permissions.includes(permission); |
||||
|
} |
||||
|
|
||||
|
refreshUserDetails() { |
||||
|
this.authService.isAuthenticated$.subscribe(isAuthed => { |
||||
|
this.isAuthenticated = isAuthed; |
||||
|
console.log(this.isAuthenticated); |
||||
|
|
||||
|
if(!this.isAuthenticated) { return } |
||||
|
|
||||
|
this.authService.user$.subscribe(data => { |
||||
|
this.user = data; |
||||
|
}) |
||||
|
|
||||
|
this.authService.getAccessTokenSilently().subscribe(data => { |
||||
|
try { |
||||
|
let decoded = jwt_decode.jwtDecode(data) as any; |
||||
|
this.permissions = decoded['permissions']; |
||||
|
} |
||||
|
catch (error) { |
||||
|
|
||||
|
} |
||||
|
}) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
getUserName() :string { |
||||
|
if(!this.isAuthenticated || !this.user) {return ""} |
||||
|
|
||||
|
return this.user.nickname || ""; |
||||
|
} |
||||
|
|
||||
|
canCreateSeasons() : boolean { |
||||
|
return this.hasPermission("create:seasons") |
||||
|
} |
||||
|
|
||||
|
canEditSeasons() : boolean { |
||||
|
return this.hasPermission("edit:seasons") |
||||
|
} |
||||
|
|
||||
|
canDeleteSeasons() : boolean { |
||||
|
return this.hasPermission("delete:seasons") |
||||
|
} |
||||
|
|
||||
|
canCreateRaces(): boolean { |
||||
|
return this.hasPermission("create:races") |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue