Skip to content

Commit

Permalink
add user role
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoSchw committed Nov 2, 2024
1 parent 734b481 commit 6f83fde
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 22 deletions.
1 change: 1 addition & 0 deletions projects/core/src/lib/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './lobby-media-muted';
export * from './lobby-media-purpose';
export * from './lobby-media-stream';
export * from './media-devices';
export * from './role';
export * from './token';
export * from './sdp-media-info';
export * from './sdp-media-line';
Expand Down
7 changes: 7 additions & 0 deletions projects/core/src/lib/entities/role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum Role {
ANONYMOUS = 0,
ADMIN,
USER,
GUEST,
SERVICE,
}
3 changes: 3 additions & 0 deletions projects/core/src/lib/entities/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {Role} from './role';

export interface User {
name: string,
domain: string,
role: Role,
}
28 changes: 17 additions & 11 deletions projects/core/src/lib/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import {HTTP_INTERCEPTORS} from '@angular/common/http';
import {AuthInterceptor} from './auth.interceptor';
import {ReqTokenInterceptor} from './req-token.interceptor';
import {UnauthorizedInterceptor} from './unauthorized.interceptor';

export const httpInterceptorProviders = [
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: ReqTokenInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: ReqTokenInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: UnauthorizedInterceptor,
multi: true,
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class ReqTokenInterceptor implements HttpInterceptor {
if (httpEvent.type === HttpEventType.Sent) {
return;
}
console.log('response: ', httpEvent);

if (httpEvent instanceof HttpResponse) {
if (httpEvent.headers.has(headerName)) {
Expand Down
26 changes: 26 additions & 0 deletions projects/core/src/lib/interceptors/unauthorized.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {SessionService} from '../provider';
import {Router} from '@angular/router';
import {catchError, Observable, of, throwError} from 'rxjs';
import {Injectable} from '@angular/core';

@Injectable()
export class UnauthorizedInterceptor implements HttpInterceptor {
constructor(private router: Router, private session: SessionService) {
}

intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(req).pipe(
catchError((error): Observable<HttpEvent<unknown>> => {
if (error instanceof HttpErrorResponse && error.status === 401) {
this.session.clearData();
this.router.navigateByUrl('/', {replaceUrl: true});
return of()
} else {
return throwError(error);
}
})
);
}
}

40 changes: 30 additions & 10 deletions projects/core/src/lib/provider/session.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of, Subject} from 'rxjs';
import {ClientUser} from '../entities';
import {Role} from '../entities';
import {User} from '../entities/user';

const USER_NAME_KEY = 'user-name';
const USER_DOMAIN_KEY = 'user-domain';
const USER_KEY = 'user';
const SESSION_TOKEN_KEY = 'jwt';

@Injectable({
Expand All @@ -15,8 +14,8 @@ export class SessionService {
private readonly anonymous = 'anonymous';

constructor() {
const user = window.localStorage.getItem(USER_NAME_KEY);
this.userName$ = new BehaviorSubject<string>(user === null ? this.anonymous : user);
const user = this.loadUser();
this.userName$ = new BehaviorSubject<string>(user === null ? this.anonymous : user.name);
}

public setAuthenticationToken(token: string) {
Expand All @@ -31,7 +30,7 @@ export class SessionService {
if (this.getAuthenticationToken() !== null) {
return of(true);
}
return of(false)
return of(false);
}

getUserName(): Observable<string> {
Expand All @@ -43,16 +42,37 @@ export class SessionService {
this.userName$.next(this.anonymous);
}

getUserRole(): Role {
if (!this.isActive()) {
return Role.ANONYMOUS;
}

const user = this.loadUser();
if (user === null) {
return Role.ANONYMOUS;
}
return user.role;
}

public setUser(user: User) {
window.localStorage.setItem(USER_NAME_KEY, user.name);
window.localStorage.setItem(USER_DOMAIN_KEY, user.domain);
this.saveUser(user);
this.userName$.next(user.name);
}

public removeUser(key: string) {
window.localStorage.removeItem(USER_NAME_KEY);
window.localStorage.removeItem(USER_DOMAIN_KEY);
window.localStorage.removeItem(USER_KEY);
this.userName$.next(this.anonymous);
}

private loadUser(): User | null {
const user = window.localStorage.getItem(USER_KEY);
if (user !== null) {
return JSON.parse(user) as User;
}
return null;
}

private saveUser(user: User): void {
window.localStorage.setItem(USER_KEY, JSON.stringify(user));
}
}

0 comments on commit 6f83fde

Please sign in to comment.