Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tu-01-43): update user access #66

Merged
merged 6 commits into from
Aug 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: implement access level guards
  • Loading branch information
stardustmeg committed Aug 18, 2024

Verified

This commit was signed with the committer’s verified signature.
XeronOwO Xeron
commit fe46ec564ebf59b61e3c16184cc75547d0d0c194
13 changes: 7 additions & 6 deletions src/app/app.routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Routes } from '@angular/router';

import { adminGuard } from './auth/guards/admin.guard';
import { authGuard, loginGuard } from './auth/guards/login.guard';
import { ADMIN_PATH, APP_PATH } from './shared/constants/routes';

const routes: Routes = [
@@ -9,27 +11,27 @@ const routes: Routes = [
title: 'Tu-Tu | Home',
},
{
canActivate: [loginGuard],
path: APP_PATH.SIGN_IN.toLowerCase(),
loadComponent: () => import('./auth/pages/login/login.component').then((c) => c.LoginComponent),
title: `Tu-Tu | ${APP_PATH.SIGN_IN}`,
},
{
canActivate: [loginGuard],
path: APP_PATH.SIGN_UP.toLowerCase(),
loadComponent: () => import('./auth/pages/register/register.component').then((c) => c.RegisterComponent),
title: `Tu-Tu | ${APP_PATH.SIGN_UP}`,
},

{
// TBD: remove when we have users logged in
// canActivate: [authGuard],
canActivate: [authGuard],
path: APP_PATH.PROFILE.toLowerCase(),
loadComponent: () => import('./profile/pages/profile/profile.component').then((c) => c.ProfileComponent),
title: `Tu-Tu | ${APP_PATH.PROFILE}`,
},

{
// TBD: remove when we have users logged in
// canActivate: [authGuard],
canActivate: [authGuard],
path: APP_PATH.ORDERS.toLowerCase(),
loadComponent: () => import('./orders/pages/orders/orders.component').then((c) => c.OrdersComponent),
title: `Tu-Tu | ${APP_PATH.ORDERS}`,
@@ -43,8 +45,7 @@ const routes: Routes = [
},

{
// TBD: Remove when we have Admin created
// canActivate: [adminGuard],
canActivate: [adminGuard],
path: APP_PATH.ADMIN.toLowerCase(),
loadComponent: () =>
import('./admin/layout/admin-layout/admin-layout.component').then((c) => c.AdminLayoutComponent),
23 changes: 21 additions & 2 deletions src/app/auth/services/auth-service/auth.service.ts
Original file line number Diff line number Diff line change
@@ -4,8 +4,10 @@ import { Router } from '@angular/router';
import { MessageService } from 'primeng/api';
import { Subscription } from 'rxjs';

import { ADMIN_CREDENTIALS } from '@/app/admin/constants/adminCredentials';
import { OverriddenHttpErrorResponse } from '@/app/api/models/errorResponse';
import { User } from '@/app/api/models/user';
import { LocalStorageService } from '@/app/core/services/local-storage/local-storage.service';

import { SignUpService } from '../../../api/signUpService/sign-up.service';

@@ -15,12 +17,17 @@ import { SignUpService } from '../../../api/signUpService/sign-up.service';
export class AuthService implements OnDestroy {
private signUpService = inject(SignUpService);
private router = inject(Router);
private messageService = inject(MessageService);
private localStorageService = inject(LocalStorageService);

public isRegistrationSuccess$$ = signal(false);
public errorMessage$$ = signal<string>('');
private messageService = inject(MessageService);
public isLoggedIn$$ = signal(this.localStorageService.getValueByKey('token') !== null);
public isAdmin$$ = signal(this.localStorageService.getValueByKey('email') === ADMIN_CREDENTIALS.email);

private subscription: Subscription | null = null;

public registrateUser(user: User): void {
public registerUser(user: User): void {
this.subscription = this.signUpService.signUp(user).subscribe({
next: () => {
this.isRegistrationSuccess$$.set(true);
@@ -36,6 +43,18 @@ export class AuthService implements OnDestroy {
});
}

public setLoginSignals(userData: User): void {
this.isLoggedIn$$.set(true);
if (userData.email === ADMIN_CREDENTIALS.email && userData.password === ADMIN_CREDENTIALS.password) {
this.isAdmin$$.set(true);
}
}

public setLogoutSignals(): void {
this.isLoggedIn$$.set(false);
this.isAdmin$$.set(false);
}

public ngOnDestroy(): void {
if (this.subscription) {
this.subscription.unsubscribe();