test update some permissions
This commit is contained in:
parent
ac64335d5b
commit
25ce65e425
15 changed files with 582 additions and 493 deletions
31
client/src/app/core/auth/unauthorized.component.ts
Normal file
31
client/src/app/core/auth/unauthorized.component.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
import { Router, ActivatedRoute } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-unauthorized',
|
||||
standalone: true,
|
||||
template: `
|
||||
<div class="flex pt-[100px] justify-center h-screen">
|
||||
<h1 class="text-2xl font-bold text-center text-red-500">
|
||||
Unauthorized, You 🫵🏻 not have permissions
|
||||
</h1>
|
||||
</div>`
|
||||
})
|
||||
export class UnauthorizedComponent implements OnInit {
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private activatedRoute: ActivatedRoute
|
||||
) { }
|
||||
|
||||
ngOnInit(): void {
|
||||
const redirectUrl = this.activatedRoute.snapshot.queryParams['redirectUrl'];
|
||||
if (redirectUrl) {
|
||||
this.router.navigate([redirectUrl]).catch(() => {
|
||||
// redirect to login page
|
||||
void this.router.navigate(['/login']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
12
client/src/app/core/auth/userPermissions.ts
Normal file
12
client/src/app/core/auth/userPermissions.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
export enum UserPermissions {
|
||||
NO_PERMISSION,
|
||||
THAI_PERMISSION = 1 << 0,
|
||||
MALAY_PERMISSION = 1 << 1,
|
||||
AUS_PERMISSION = 1 << 2,
|
||||
SUPER_ADMIN = 1 << 3
|
||||
}
|
||||
|
||||
export function getPermissions(perms: number) : UserPermissions[] {
|
||||
return Object.values(UserPermissions)
|
||||
.filter(permission => typeof permission === 'number' && (perms & permission) !== 0) as UserPermissions[];
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { ActivatedRoute, Params, Router } from '@angular/router';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { UserService } from '../services/user.service';
|
||||
import {getPermissions} from "../auth/userPermissions";
|
||||
|
||||
@Component({
|
||||
selector: 'app-callback',
|
||||
|
|
@ -19,18 +19,19 @@ export class CallbackComponent implements OnInit {
|
|||
this.route.queryParams.subscribe((params) => {
|
||||
console.log(params);
|
||||
|
||||
if (params['email'] && params['name'] && params['picture']) {
|
||||
if (params['email'] && params['name'] && params['picture'] && params['permissions']) {
|
||||
this.userService.setAuth({
|
||||
email: params['email'],
|
||||
name: params['name'],
|
||||
picture: params['picture'],
|
||||
permissions: getPermissions(params['permissions'])
|
||||
});
|
||||
}
|
||||
|
||||
if (params['redirect_to']) {
|
||||
this.router.navigate([params['redirect_to']]);
|
||||
void this.router.navigate([params['redirect_to']]);
|
||||
} else {
|
||||
this.router.navigate(['/']);
|
||||
void this.router.navigate(['/']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
import {UserPermissions} from "../auth/userPermissions";
|
||||
|
||||
export interface User {
|
||||
email: string;
|
||||
name: string;
|
||||
picture: string;
|
||||
permissions: UserPermissions[];
|
||||
}
|
||||
|
|
|
|||
10
client/src/app/core/notfound.component.ts
Normal file
10
client/src/app/core/notfound.component.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-notfound',
|
||||
standalone: true,
|
||||
template: `<h1>Not Found!!!</h1>`
|
||||
})
|
||||
export class NotfoundComponent {
|
||||
|
||||
}
|
||||
|
|
@ -2,10 +2,9 @@ import { Injectable } from '@angular/core';
|
|||
import { HttpClient } from '@angular/common/http';
|
||||
import {
|
||||
BehaviorSubject,
|
||||
Observable,
|
||||
distinctUntilChanged,
|
||||
map,
|
||||
tap,
|
||||
|
||||
} from 'rxjs';
|
||||
import { User } from '../models/user.model';
|
||||
import { Router } from '@angular/router';
|
||||
|
|
@ -31,6 +30,10 @@ export class UserService {
|
|||
}
|
||||
}
|
||||
|
||||
getCurrentUser(): User | null {
|
||||
return this.currentUserSubject.value
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
this.purgeAuth();
|
||||
// post to api /revoke with cookie
|
||||
|
|
@ -43,21 +46,6 @@ export class UserService {
|
|||
});
|
||||
}
|
||||
|
||||
getCurrentUser(): Observable<{ user: User }> {
|
||||
return this.http
|
||||
.get<{ user: User }>(environment.api + '/auth/user', {
|
||||
withCredentials: true,
|
||||
})
|
||||
.pipe(
|
||||
tap({
|
||||
next: ({ user }) => {
|
||||
this.setAuth(user);
|
||||
},
|
||||
error: () => this.purgeAuth(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
setAuth(user: User): void {
|
||||
window.localStorage.setItem('user', JSON.stringify(user));
|
||||
void this.currentUserSubject.next(user);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue