- @if(isAuthed()){
+ @if(userService.canCreateSeasons()){
}
@for (season of seasons; track season) {
diff --git a/packages/bridge-ui/src/app/pages/seasons/seasons.component.ts b/packages/bridge-ui/src/app/pages/seasons/seasons.component.ts
index 9588ba2..8d99e68 100644
--- a/packages/bridge-ui/src/app/pages/seasons/seasons.component.ts
+++ b/packages/bridge-ui/src/app/pages/seasons/seasons.component.ts
@@ -2,12 +2,12 @@ import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SeasonsService } from '../../services/seasons.service';
-import { AuthService } from '../../services/auth.service';
import { SeasonCardComponent } from '../../components/season-card/season-card.component';
import { SeasonCardNewComponent } from '../../components/season-card-new/season-card-new.component';
import { Season } from '../../models/season.model';
+import { UsersService } from '../../services/users.service';
@Component({
selector: 'app-seasons',
@@ -26,7 +26,7 @@ export class SeasonsComponent {
constructor(
private seasonService: SeasonsService,
- private authService: AuthService
+ public userService: UsersService
) {}
ngOnInit() {
@@ -35,7 +35,4 @@ export class SeasonsComponent {
});
}
- isAuthed() {
- return this.authService.isAuthenticated();
- }
}
diff --git a/packages/bridge-ui/src/app/services/auth.service.ts b/packages/bridge-ui/src/app/services/auth.service.ts
deleted file mode 100644
index 7bf4f65..0000000
--- a/packages/bridge-ui/src/app/services/auth.service.ts
+++ /dev/null
@@ -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
{
- 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;
- }
- }
-}
diff --git a/packages/bridge-ui/src/app/services/seasons.service.ts b/packages/bridge-ui/src/app/services/seasons.service.ts
index b379eed..c18939a 100644
--- a/packages/bridge-ui/src/app/services/seasons.service.ts
+++ b/packages/bridge-ui/src/app/services/seasons.service.ts
@@ -81,7 +81,7 @@ export class SeasonsService {
}
create(title: string, subTitle: string, startingDate: Date) {
- return this.httpClient.post(this.serverEndpointService.getCurrentEndpoint()+"seasons/", {title, subTitle, startingDate});
+ return this.httpClient.post(this.serverEndpointService.getCurrentEndpoint()+"seasons/create", {title, subTitle, startingDate});
}
updateSeason(id: string) {
diff --git a/packages/bridge-ui/src/app/services/auth.service.spec.ts b/packages/bridge-ui/src/app/services/users.service.spec.ts
similarity index 56%
rename from packages/bridge-ui/src/app/services/auth.service.spec.ts
rename to packages/bridge-ui/src/app/services/users.service.spec.ts
index f1251ca..f81244a 100644
--- a/packages/bridge-ui/src/app/services/auth.service.spec.ts
+++ b/packages/bridge-ui/src/app/services/users.service.spec.ts
@@ -1,13 +1,13 @@
import { TestBed } from '@angular/core/testing';
-import { AuthService } from './auth.service';
+import { UsersService } from './users.service';
-describe('AuthService', () => {
- let service: AuthService;
+describe('UsersService', () => {
+ let service: UsersService;
beforeEach(() => {
TestBed.configureTestingModule({});
- service = TestBed.inject(AuthService);
+ service = TestBed.inject(UsersService);
});
it('should be created', () => {
diff --git a/packages/bridge-ui/src/app/services/users.service.ts b/packages/bridge-ui/src/app/services/users.service.ts
new file mode 100644
index 0000000..1d02d85
--- /dev/null
+++ b/packages/bridge-ui/src/app/services/users.service.ts
@@ -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")
+ }
+}