diff --git a/src/app/components/comment-form/comment-form.component.ts b/src/app/components/comment-form/comment-form.component.ts index 47061e4..56d2dca 100644 --- a/src/app/components/comment-form/comment-form.component.ts +++ b/src/app/components/comment-form/comment-form.component.ts @@ -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); } })) } diff --git a/src/app/components/header/header.component.ts b/src/app/components/header/header.component.ts index 679d081..1ff54ca 100644 --- a/src/app/components/header/header.component.ts +++ b/src/app/components/header/header.component.ts @@ -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) diff --git a/src/app/components/post-form/post-form.component.ts b/src/app/components/post-form/post-form.component.ts index c3573bc..e0da7ee 100644 --- a/src/app/components/post-form/post-form.component.ts +++ b/src/app/components/post-form/post-form.component.ts @@ -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) }) ); } diff --git a/src/app/components/register-form/register-form.component.ts b/src/app/components/register-form/register-form.component.ts index 6a62d22..04f09db 100644 --- a/src/app/components/register-form/register-form.component.ts +++ b/src/app/components/register-form/register-form.component.ts @@ -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 = 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) })); } diff --git a/src/app/components/update-profile-form/update-profile-form.component.ts b/src/app/components/update-profile-form/update-profile-form.component.ts index d73cdb1..6900917 100644 --- a/src/app/components/update-profile-form/update-profile-form.component.ts +++ b/src/app/components/update-profile-form/update-profile-form.component.ts @@ -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); } })); } diff --git a/src/app/guards/admin.guard.ts b/src/app/guards/admin.guard.ts index 7137c05..5e90c8d 100644 --- a/src/app/guards/admin.guard.ts +++ b/src/app/guards/admin.guard.ts @@ -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(['/']); } diff --git a/src/app/guards/writer.guard.ts b/src/app/guards/writer.guard.ts index 7fa5965..ad596bd 100644 --- a/src/app/guards/writer.guard.ts +++ b/src/app/guards/writer.guard.ts @@ -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(['/']); } diff --git a/src/app/models/role.ts b/src/app/models/role.ts new file mode 100644 index 0000000..d08096b --- /dev/null +++ b/src/app/models/role.ts @@ -0,0 +1,5 @@ +export enum Role { + WRITER = "WRITER", + READER = "READER", + ADMIN = "ADMIN", +} diff --git a/src/app/pages/admin-authors/admin-authors.component.ts b/src/app/pages/admin-authors/admin-authors.component.ts index 8ef8ec8..ea5d7ea 100644 --- a/src/app/pages/admin-authors/admin-authors.component.ts +++ b/src/app/pages/admin-authors/admin-authors.component.ts @@ -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) } }) } diff --git a/src/app/pages/home/home.component.ts b/src/app/pages/home/home.component.ts index 7d72096..1aa49d1 100644 --- a/src/app/pages/home/home.component.ts +++ b/src/app/pages/home/home.component.ts @@ -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), })); } diff --git a/src/app/pages/login/login.component.ts b/src/app/pages/login/login.component.ts index 6dc34bf..8825cf4 100644 --- a/src/app/pages/login/login.component.ts +++ b/src/app/pages/login/login.component.ts @@ -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 { diff --git a/src/app/pages/my-posts/my-posts.component.ts b/src/app/pages/my-posts/my-posts.component.ts index 94932cc..18923f6 100644 --- a/src/app/pages/my-posts/my-posts.component.ts +++ b/src/app/pages/my-posts/my-posts.component.ts @@ -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) } diff --git a/src/app/pages/new-post/new-post.component.ts b/src/app/pages/new-post/new-post.component.ts index e33a27a..98c3cd6 100644 --- a/src/app/pages/new-post/new-post.component.ts +++ b/src/app/pages/new-post/new-post.component.ts @@ -88,7 +88,7 @@ export class NewPostComponent implements OnDestroy { }); }, error: (err) => { - this.failureMessage('Erreur', err.message); + this.failureMessage('Erreur', err.error.message); } }) ); diff --git a/src/app/pages/post/post.component.html b/src/app/pages/post/post.component.html index b1e6cf0..0f22b6a 100644 --- a/src/app/pages/post/post.component.html +++ b/src/app/pages/post/post.component.html @@ -23,7 +23,7 @@

Envoyé le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}

} @if (actualAuthor) { - @if (comment.authorId === actualAuthor.id || actualAuthor.role === 'ADMIN') { + @if (comment.authorId === actualAuthor.id || actualAuthor.role === Role.ADMIN) {
@@ -50,7 +50,7 @@

Envoyé le {{ comment.commentDate | date: "dd/MM/yyyy à HH:mm" }}

} @if (actualAuthor) { - @if (comment.authorId === actualAuthor.id || actualAuthor.role === 'ADMIN') { + @if (comment.authorId === actualAuthor.id || actualAuthor.role === Role.ADMIN) {
diff --git a/src/app/pages/post/post.component.ts b/src/app/pages/post/post.component.ts index 51610ab..07d87e2 100644 --- a/src/app/pages/post/post.component.ts +++ b/src/app/pages/post/post.component.ts @@ -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; }