Affichage des erreurs envoyés par l'api et ajout d'une enum role

This commit is contained in:
Guams 2025-02-07 18:24:31 +01:00
parent 2921ecc303
commit 192db6d370
15 changed files with 40 additions and 29 deletions

View File

@ -51,7 +51,7 @@ export class CommentFormComponent {
this.successMessage("Succès", "Commentaire créé avec succès");
},
error: (error) => {
this.failureMessage("Erreur d'envois", error.message);
this.failureMessage("Erreur d'envois", error.error.message);
}
}))
}

View File

@ -4,6 +4,7 @@ import {MenubarModule} from 'primeng/menubar';
import {ToastModule} from 'primeng/toast';
import {Author} from '../../models/author';
import {AuthService} from '../../auth.service';
import {Role} from '../../models/role'
@Component({
selector: 'app-header',
@ -44,19 +45,19 @@ export class HeaderComponent {
}
];
if (author.role === 'WRITER') {
if (author.role === Role.WRITER) {
return [
...commonItems,
this.getWriterMenu(),
this.getUserMenu(author)
];
} else if (author.role === 'ADMIN') {
} else if (author.role === Role.ADMIN) {
return [
...commonItems,
this.getAdminMenu(),
this.getUserMenu(author)
];
} else if (author.role === 'READER') {
} else if (author.role === Role.READER) {
return [
...commonItems,
this.getUserMenu(author)

View File

@ -79,7 +79,7 @@ export class PostFormComponent implements OnDestroy {
});
},
error: (err) => {
this.failureMessage('Erreur', `Impossible de charger le post : ${err.message}`);
this.failureMessage('Erreur', err.error.message);
}
})
);
@ -113,7 +113,7 @@ export class PostFormComponent implements OnDestroy {
next: (_) => {
this.successMessage('Succès', 'Post mis à jour avec succès')
},
error: (err) => this.failureMessage('Erreur', err.message)
error: (err) => this.failureMessage('Erreur', err.error.message)
})
);
} else {
@ -132,7 +132,7 @@ export class PostFormComponent implements OnDestroy {
this.successMessage('Succès', 'Post créé avec succès')
});
},
error: (err) => this.failureMessage('Erreur', err.message)
error: (err) => this.failureMessage('Erreur', err.error.message)
})
);
}

View File

@ -9,6 +9,7 @@ import {Router} from '@angular/router';
import {MessageService} from 'primeng/api';
import {Author} from '../../models/author';
import {AuthService} from '../../auth.service';
import {Role} from '../../models/role';
@Component({
selector: 'app-register-form',
@ -28,11 +29,11 @@ export class RegisterFormComponent implements OnDestroy {
@Output() createdAuthor: EventEmitter<Author> = new EventEmitter();
@Input() password: string = "";
@Input() passwordConfirm: string = "";
@Input() role: string = "READER"
@Input() role: string = Role.READER
@Input() adminForm: boolean = false;
roles = [
{name: 'Lecteur', value: 'READER'},
{name: 'Écrivain', value: 'WRITER'}
{name: 'Lecteur', value: Role.READER},
{name: 'Écrivain', value: Role.WRITER}
];
subs: Subscription[] = [];
form: FormGroup;
@ -51,7 +52,7 @@ export class RegisterFormComponent implements OnDestroy {
username: ['', [Validators.required, Validators.maxLength(255)]],
password: ['', [Validators.required, Validators.maxLength(50)]],
passwordConfirm: ['', [Validators.required, Validators.maxLength(50)]],
role: [{value: 'READER'}, [Validators.required, Validators.maxLength(10)]],
role: [{value: Role.READER}, [Validators.required, Validators.maxLength(10)]],
});
}
@ -88,13 +89,13 @@ export class RegisterFormComponent implements OnDestroy {
this.createdAuthor.emit(author);
},
error: (err) => {
this.failureMessage("Erreur", err.message);
this.failureMessage("Erreur", err.error.message);
}
});
} else {
this.subs.push(this.authorService.createAccount(this.username, this.password).subscribe({
next: () => this.router.navigate(['/login']).then(() => this.successMessage('Succès', 'Utilisateur créé avec succès')),
error: (err) => this.failureMessage('Erreur', err.message)
error: (err) => this.failureMessage('Erreur', err.error.message)
}));
}

View File

@ -96,7 +96,7 @@ export class UpdateProfileFormComponent implements OnDestroy {
this.router.navigate(['/']);
},
error: (err) => {
this.failureMessage("Erreur", err.message);
this.failureMessage("Erreur", err.error.error.message);
}
}));
} else {
@ -108,7 +108,7 @@ export class UpdateProfileFormComponent implements OnDestroy {
this.router.navigate(['/']);
},
error: (err) => {
this.failureMessage("Erreur", err.message);
this.failureMessage("Erreur", err.error.error.message);
}
}));
}

View File

@ -1,15 +1,15 @@
import {CanActivateFn, Router} from '@angular/router';
import {inject} from '@angular/core';
import {CookieService} from 'ngx-cookie-service';
import {Author} from '../models/author';
import {AuthService} from '../auth.service';
import {Role} from '../models/role';
export const adminGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const cookieService = inject(CookieService);
const authService: AuthService = inject(AuthService);
if ((authService.isAuthenticated() && JSON.parse(cookieService.get("author")).role !== 'ADMIN') || !authService.isAuthenticated()) {
if ((authService.isAuthenticated() && JSON.parse(cookieService.get("author")).role !== Role.ADMIN) || !authService.isAuthenticated()) {
router.navigate(['/']);
}

View File

@ -2,13 +2,14 @@ import {CanActivateFn, Router} from '@angular/router';
import {inject} from '@angular/core';
import {CookieService} from 'ngx-cookie-service';
import {AuthService} from '../auth.service';
import {Role} from '../models/role';
export const writerGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const cookieService = inject(CookieService);
const authService = inject(AuthService);
if ((authService.isAuthenticated() && JSON.parse(cookieService.get("author")).role !== 'WRITER') || !authService.isAuthenticated()) {
if ((authService.isAuthenticated() && JSON.parse(cookieService.get("author")).role !== Role.WRITER) || !authService.isAuthenticated()) {
router.navigate(['/']);
}

5
src/app/models/role.ts Normal file
View File

@ -0,0 +1,5 @@
export enum Role {
WRITER = "WRITER",
READER = "READER",
ADMIN = "ADMIN",
}

View File

@ -39,8 +39,8 @@ export class AdminAuthorsComponent implements OnDestroy{
next: (authors: Author[]) => {
this.authors = authors;
},
error: (error: Error) => {
this.failureMessage("Erreur", error.message)
error: (error) => {
this.failureMessage("Erreur", error.error.message)
}
})
}

View File

@ -45,7 +45,7 @@ export class HomeComponent implements OnDestroy {
}
return 0
}),
error: (err) => console.error(err.message),
error: (err) => console.error(err.error.error.message),
}));
}

View File

@ -67,7 +67,7 @@ export class LoginComponent implements OnDestroy {
this.successMessage('Connecté avec succès', 'Heureux de vous revoir ' + this.actualAuthor?.name)
});
},
error: (err) => this.failureMessage('Erreur de connexion', err.message)
error: (err) => this.failureMessage('Erreur de connexion', err.error.message)
})
);
} else {

View File

@ -62,7 +62,7 @@ export class MyPostsComponent implements OnDestroy {
if (!(this.authService.isSessionExpired()) && this.authService.isAuthenticated()) {
this.authorService.getAuthorsPosts(this.actualAuthor?.id, this.authService.getAuthenticatedAuthorToken()).subscribe({
next: posts => this.posts = posts,
error: error => this.failureMessage("Erreur", error.message),
error: error => this.failureMessage("Erreur", error.error.message),
}
)
} else {
@ -76,7 +76,7 @@ export class MyPostsComponent implements OnDestroy {
this.updatePosts()
this.successMessage("Post supprimé", "Ce post a été supprimé avec succès")
},
error: error => this.failureMessage("Erreur", error.message),
error: error => this.failureMessage("Erreur", error.error.message),
});
this.closeDialog(this.deleteDialogVisibility, rowIndex)
}

View File

@ -88,7 +88,7 @@ export class NewPostComponent implements OnDestroy {
});
},
error: (err) => {
this.failureMessage('Erreur', err.message);
this.failureMessage('Erreur', err.error.message);
}
})
);

View File

@ -23,7 +23,7 @@
<p> Envoyé le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}</p>
}
@if (actualAuthor) {
@if (comment.authorId === actualAuthor.id || actualAuthor.role === 'ADMIN') {
@if (comment.authorId === actualAuthor.id || actualAuthor.role === Role.ADMIN) {
<p-button severity="danger" icon="pi pi-trash" (onClick)="setCommentDialogVisible(comment.id)"></p-button>
<p-dialog [closable]="false" [modal]="true" header='Supprimer le commentaire ?' [visible]="commentDeleteDialogMap.get(comment.id) ?? false">
<div class="delete-dialog">
@ -50,7 +50,7 @@
<p> Envoyé le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}</p>
}
@if (actualAuthor) {
@if (comment.authorId === actualAuthor.id || actualAuthor.role === 'ADMIN') {
@if (comment.authorId === actualAuthor.id || actualAuthor.role === Role.ADMIN) {
<p-button severity="danger" icon="pi pi-trash" (onClick)="setCommentDialogVisible(comment.id)"></p-button>
<p-dialog [closable]="false" [modal]="true" header='Supprimer le commentaire ?' [visible]="commentDeleteDialogMap.get(comment.id) ?? false">
<div class="delete-dialog">

View File

@ -16,6 +16,7 @@ import {CommentFormComponent} from '../../components/comment-form/comment-form.c
import {Button} from 'primeng/button';
import {DialogModule} from 'primeng/dialog';
import {AuthService} from '../../auth.service';
import {Role} from '../../models/role';
@Component({
selector: 'app-post',
@ -73,7 +74,7 @@ export class PostComponent {
});
this.comments.forEach(comment => this.commentDeleteDialogMap.set(comment.id, false));
},
error: err => this.failureMessage("Erreur", err.message)
error: err => this.failureMessage("Erreur", err.error.message)
}));
}
if (this.concernedPost?.body) {
@ -106,7 +107,7 @@ export class PostComponent {
this.setCommentDialogHidden(comment.id);
this.commentDeleteDialogMap.delete(comment.id);
},
error: err => this.failureMessage("Erreur", err.message)
error: err => this.failureMessage("Erreur", err.error.message)
})
);
}
@ -142,4 +143,6 @@ export class PostComponent {
closable: false
});
}
protected readonly Role = Role;
}