Skip to content
Snippets Groups Projects
Commit d07aa01e authored by Jérémie BRISON's avatar Jérémie BRISON Committed by Hugo SUBTIL
Browse files

feat(admin) : add guard + service

parent 9fc2711f
No related branches found
No related tags found
3 merge requests!68Recette,!67Dev,!51Feat/admin panel attachment
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AboutComponent } from './about/about.component';
import { PanelComponent } from './admin/panel/panel.component';
import { AdminGuard } from './guards/admin.guard';
import { AuthGuard } from './guards/auth.guard';
import { HomeComponent } from './home/home.component';
import { LegalNoticeComponent } from './legal-notice/legal-notice.component';
......@@ -58,6 +60,11 @@ const routes: Routes = [
path: 'reset-password',
component: ResetPasswordComponent,
},
{
path: 'admin',
canActivate: [AdminGuard],
component: PanelComponent,
},
{
path: '**',
redirectTo: 'home',
......
......@@ -27,6 +27,8 @@ import { CustomHttpInterceptor } from './config/http-interceptor';
import { ProfileModule } from './profile/profile.module';
import { ResetEmailComponent } from './reset-email/reset-email.component';
import { ResetPasswordComponent } from './reset-password/reset-password.component';
import { AdminModule } from './admin/admin.module';
import { AdminGuard } from './guards/admin.guard';
@NgModule({
declarations: [
......@@ -47,12 +49,13 @@ import { ResetPasswordComponent } from './reset-password/reset-password.componen
ResetEmailComponent,
ResetPasswordComponent,
],
imports: [BrowserModule, HttpClientModule, AppRoutingModule, SharedModule, MapModule, ProfileModule],
imports: [BrowserModule, HttpClientModule, AppRoutingModule, SharedModule, MapModule, ProfileModule, AdminModule],
providers: [
{ provide: LOCALE_ID, useValue: 'fr' },
{ provide: HTTP_INTERCEPTORS, useClass: CustomHttpInterceptor, multi: true },
CustomBreakPointsProvider,
AuthGuard,
AdminGuard,
],
bootstrap: [AppComponent],
})
......
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Injectable } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { ProfileService } from '../profile/services/profile.service';
/**
* Guard to assert that we are logged in admin. Otherwise redirect to home
*/
@Injectable()
export class AdminGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router, private profileService: ProfileService) {}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): UrlTree | boolean {
if (this.authService.isLoggedIn()) {
if (this.profileService.isAdmin()) {
return true;
}
return this.router.parseUrl('/profile');
}
return this.router.parseUrl('/home');
}
}
......@@ -7,9 +7,10 @@
<p>
Mes structures :
<span *ngFor="let structureId of userProfile.structuresLink">
<b>{{ structureId }}</b>
<strong>{{ structureId }}</strong>
</span>
</p>
<button routerLink="/admin">Accèder au panel d'administration</button>
<button (click)="toogleAddStructure()">Ajouter une structure</button>
<button (click)="toogleChangeEmail()">Changer d'email</button>
<form
......
......@@ -40,6 +40,13 @@ export class ProfileService {
return this.http.post<any>(`${this.baseUrl}`, body);
}
public isAdmin(): boolean {
if (this.currentProfile) {
return this.currentProfile.role == 1;
}
return false;
}
public changePassword(newPassword: string, oldPassword: string): Observable<User> {
return this.http.post<any>(`${this.baseUrl}/change-password`, { newPassword, oldPassword });
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment