diff --git a/src/app/app.component.css b/src/app/app.component.css
index e69de29..a305c99 100644
--- a/src/app/app.component.css
+++ b/src/app/app.component.css
@@ -0,0 +1,5 @@
+.expired-dialog {
+ margin-top: 2rem;
+ display: flex;
+ justify-content: space-around;
+}
diff --git a/src/app/app.component.html b/src/app/app.component.html
index f294d32..b094b8e 100644
--- a/src/app/app.component.html
+++ b/src/app/app.component.html
@@ -1,2 +1,11 @@
+@if (isBrowser()) {
+
+ Votre session a expiré ! Vous pouvez vous déconnecter.
+
+
+}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 55e28cc..c906e01 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -1,22 +1,46 @@
-import {Component} from '@angular/core';
+import {Component, Inject, OnInit, PLATFORM_ID} from '@angular/core';
import {MenubarModule} from 'primeng/menubar';
-import {MenuItem, MessageService} from 'primeng/api';
+import {MessageService} from 'primeng/api';
import {FloatLabelModule} from 'primeng/floatlabel';
-import {CookieService} from 'ngx-cookie-service';
import {ToastModule} from 'primeng/toast';
+import {DialogModule} from 'primeng/dialog';
+import {isPlatformBrowser} from '@angular/common';
+import {Button} from 'primeng/button';
+import {AuthService} from './auth.service';
+import {CookieService} from 'ngx-cookie-service';
+import {Router} from '@angular/router';
+import {ConfigurationService} from './configuration.service';
@Component({
selector: 'app-root',
standalone: true,
- imports: [MenubarModule, FloatLabelModule, ToastModule],
+ imports: [MenubarModule, FloatLabelModule, ToastModule, DialogModule, Button],
providers: [
MessageService,
],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
-export class AppComponent{
+export class AppComponent implements OnInit {
+ isSessionExpired: boolean = false;
- constructor() {
+ constructor(@Inject(PLATFORM_ID) private platformId: object,
+ private authService: AuthService,
+ private cookieService: CookieService) {
+ }
+
+ isBrowser(): boolean {
+ return isPlatformBrowser(this.platformId);
+ }
+
+ setSessionExpiredFalse(): void {
+ this.isSessionExpired = false;
+ this.authService.setSessionExpired(false);
+ }
+
+ ngOnInit(): void {
+ this.authService.sessionExpired$.subscribe(expired => {
+ this.isSessionExpired = expired;
+ });
}
}
diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts
index 4720348..76e09c2 100644
--- a/src/app/app.routes.ts
+++ b/src/app/app.routes.ts
@@ -1,7 +1,6 @@
import {Routes} from '@angular/router';
import {LoginComponent} from './pages/login/login.component';
import {HomeComponent} from './pages/home/home.component';
-import {RegisterFormComponent} from './components/register-form/register-form.component';
import {LogoutComponent} from './pages/logout/logout.component';
import {NotFoundComponent} from './pages/not-found/not-found.component';
import {authGuard} from './guards/auth.guard';
@@ -15,7 +14,7 @@ import {RegisterComponent} from './pages/register/register.component';
export const routes: Routes = [
{path: '', component: HomeComponent},
{path: 'login', component: LoginComponent, canActivate: [authGuard]},
- {path: 'register', component: RegisterComponent, canActivate: [authGuard]},
+ {path: 'register', component: RegisterComponent, canActivate: [authGuard]},
{path: 'logout', component: LogoutComponent},
{path: 'profile/:authorId', component: ProfileComponent},
{path: 'post/:postId', component: PostComponent},
diff --git a/src/app/auth.service.ts b/src/app/auth.service.ts
new file mode 100644
index 0000000..c0051c3
--- /dev/null
+++ b/src/app/auth.service.ts
@@ -0,0 +1,47 @@
+import { Injectable } from '@angular/core';
+import { CookieService } from 'ngx-cookie-service';
+import { Author } from './models/author';
+import { BehaviorSubject } from 'rxjs';
+
+@Injectable({
+ providedIn: 'root'
+})
+export class AuthService {
+ private sessionExpiredSubject = new BehaviorSubject(false);
+ sessionExpired$ = this.sessionExpiredSubject.asObservable();
+
+ constructor(private cookieService: CookieService) {
+ this.checkSessionExpiration();
+ }
+
+ isAuthenticated(): boolean {
+ return this.cookieService.check("author") &&
+ this.cookieService.check("token") &&
+ this.cookieService.get("author") !== '' &&
+ this.cookieService.get("token") !== '';
+ }
+
+ isSessionExpired(): boolean {
+ return !(this.cookieService.check("author") &&
+ this.cookieService.check("token")) &&
+ !this.isAuthenticated();
+ }
+
+ getAuthenticatedAuthor(): Author {
+ return JSON.parse(this.cookieService.get('author'));
+ }
+
+ getAuthenticatedAuthorToken(): string {
+ return this.cookieService.get('token');
+ }
+
+ setSessionExpired(expired: boolean) {
+ this.sessionExpiredSubject.next(expired);
+ }
+
+ checkSessionExpiration() {
+ if (this.isSessionExpired()) {
+ this.setSessionExpired(true);
+ }
+ }
+}
diff --git a/src/app/components/comment-form/comment-form.component.ts b/src/app/components/comment-form/comment-form.component.ts
index 6daf490..47061e4 100644
--- a/src/app/components/comment-form/comment-form.component.ts
+++ b/src/app/components/comment-form/comment-form.component.ts
@@ -3,12 +3,12 @@ import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/
import {InputTextareaModule} from 'primeng/inputtextarea';
import {Button} from 'primeng/button';
import {CommentService} from '../../services/comment.service';
-import {CookieService} from 'ngx-cookie-service';
import {Author} from '../../models/author';
import {Subscription} from 'rxjs';
import {Comment} from '../../models/comment';
import {MessageService} from 'primeng/api';
import {NgStyle} from '@angular/common';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-comment-form',
@@ -31,13 +31,13 @@ export class CommentFormComponent {
subs: Subscription[] = [];
constructor(private commentService: CommentService,
- private cookieService: CookieService,
- private messageService: MessageService,) {
+ private messageService: MessageService,
+ private authService: AuthService,) {
}
onSubmit() {
- let token: string = this.cookieService.get("token");
- let author: Author = this.cookieService.get("author") ? JSON.parse(this.cookieService.get("author")) : undefined;
+ let token: string = this.authService.getAuthenticatedAuthorToken();
+ let author: Author = this.authService.getAuthenticatedAuthor();
if (this.commentForm.valid && author && token && this.commentForm.value.content) {
// get l'image de profile après avoir créé le commentaire
this.subs.push(this.commentService.create(this.commentForm.value.content, this.postId, author.id, token).subscribe({
diff --git a/src/app/components/header/header.component.ts b/src/app/components/header/header.component.ts
index b90aa88..d183956 100644
--- a/src/app/components/header/header.component.ts
+++ b/src/app/components/header/header.component.ts
@@ -1,9 +1,9 @@
-import { Component } from '@angular/core';
-import { CookieService } from 'ngx-cookie-service';
-import { MenuItem } from 'primeng/api';
-import { MenubarModule } from 'primeng/menubar';
-import { ToastModule } from 'primeng/toast';
-import { Author } from '../../models/author';
+import {Component} from '@angular/core';
+import {MenuItem} from 'primeng/api';
+import {MenubarModule} from 'primeng/menubar';
+import {ToastModule} from 'primeng/toast';
+import {Author} from '../../models/author';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-header',
@@ -19,13 +19,14 @@ export class HeaderComponent {
actualAuthor: Author | undefined;
items: MenuItem[] = [];
- constructor(private cookieService: CookieService) {
+ constructor(private authService: AuthService) {
this.initializeMenu();
}
private initializeMenu(): void {
- const authorData = this.cookieService.get('author');
- this.actualAuthor = authorData ? JSON.parse(authorData) : undefined;
+ if (this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
+ }
if (this.actualAuthor) {
this.items = this.getMenuForAuthor(this.actualAuthor);
diff --git a/src/app/components/post-form/post-form.component.ts b/src/app/components/post-form/post-form.component.ts
index 2065d8d..f839b54 100644
--- a/src/app/components/post-form/post-form.component.ts
+++ b/src/app/components/post-form/post-form.component.ts
@@ -1,16 +1,16 @@
-import {AfterViewInit, Component, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
+import {Component, Input, OnDestroy} from '@angular/core';
import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
import {InputTextModule} from 'primeng/inputtext';
import {InputTextareaModule} from 'primeng/inputtextarea';
import {FileSelectEvent, FileUploadModule} from 'primeng/fileupload';
import {mergeMap, Subscription} from 'rxjs';
import {PostService} from '../../services/post.service';
-import {CookieService} from 'ngx-cookie-service';
import {MessageService} from 'primeng/api';
import {EditorModule} from 'primeng/editor';
import {Router} from '@angular/router';
import {Author} from '../../models/author';
import {AuthorService} from '../../services/author.service';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-post-form',
@@ -49,7 +49,7 @@ export class PostFormComponent implements OnDestroy {
private formBuilder: FormBuilder,
private postService: PostService,
private authorService: AuthorService,
- private cookieService: CookieService,
+ private authService: AuthService,
private messageService: MessageService,
private router: Router
) {
@@ -105,9 +105,9 @@ export class PostFormComponent implements OnDestroy {
if (this.isUpdateMode && this.postId) {
this.subs.push(
- this.postService.updatePost(this.postId, postData, this.cookieService.get('token')).pipe(
+ this.postService.updatePost(this.postId, postData, this.authService.getAuthenticatedAuthorToken()).pipe(
mergeMap((_) => {
- return this.postService.changeIllustration(this.postId, this.uploadedFile, this.cookieService.get('token'));
+ return this.postService.changeIllustration(this.postId, this.uploadedFile, this.authService.getAuthenticatedAuthorToken());
})
).subscribe({
next: (_) => {
@@ -119,11 +119,11 @@ export class PostFormComponent implements OnDestroy {
);
} else {
this.subs.push(
- this.postService.createPost(postData, this.cookieService.get("token")).pipe(
+ this.postService.createPost(postData, this.authService.getAuthenticatedAuthorToken()).pipe(
mergeMap(post =>
- this.authorService.attributePost(this.actualAuthor?.id, post.id, this.cookieService.get("token")).pipe(
+ this.authorService.attributePost(this.actualAuthor?.id, post.id, this.authService.getAuthenticatedAuthorToken()).pipe(
mergeMap((_) =>
- this.postService.changeIllustration(post.id, this.uploadedFile, this.cookieService.get("token"))
+ this.postService.changeIllustration(post.id, this.uploadedFile, this.authService.getAuthenticatedAuthorToken()),
)
)
)
@@ -143,7 +143,7 @@ export class PostFormComponent implements OnDestroy {
private transformYouTubeLinksToIframes(html: string): string {
return html.replace(/]*href="(https?:\/\/(?:www\.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([a-zA-Z0-9_-]+)[^"]*)".*?<\/a>/g,
- (_, url, _prefix, videoId) => {
+ (_, _url, _prefix, videoId) => {
return ``;
});
}
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 954ff77..d73cdb1 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
@@ -10,6 +10,7 @@ import {FileSelectEvent, FileUploadModule} from 'primeng/fileupload';
import {CookieService} from 'ngx-cookie-service';
import {Author} from '../../models/author';
import {Router} from '@angular/router';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-update-profile',
@@ -38,6 +39,7 @@ export class UpdateProfileFormComponent implements OnDestroy {
private authorService: AuthorService,
private messageService: MessageService,
private cookieService: CookieService,
+ private authService: AuthService,
private router: Router,
) {
this.form = this.formBuilder.group({
@@ -78,7 +80,7 @@ export class UpdateProfileFormComponent implements OnDestroy {
}
onSubmit() {
- const token: string | undefined = this.cookieService.get('token');
+ const token: string = this.authService.getAuthenticatedAuthorToken();
if (this.form.valid && token && this.password === this.passwordConfirm) {
const newUsername = this.form.value.username;
if (this.uploadedFile) {
diff --git a/src/app/guards/admin.guard.ts b/src/app/guards/admin.guard.ts
index 2508747..32c9d94 100644
--- a/src/app/guards/admin.guard.ts
+++ b/src/app/guards/admin.guard.ts
@@ -7,11 +7,8 @@ export const adminGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const cookieService = inject(CookieService);
- if (cookieService.get("author") !== '') {
- if (JSON.parse(cookieService.get("author")).role !== 'ADMIN')
- {
- router.navigate(['/']);
- }
+ if (cookieService.get("author") === '' || (JSON.parse(cookieService.get("author")).role !== 'ADMIN' && (cookieService.check("author") && cookieService.check("token")))) {
+ router.navigate(['/']);
}
return true;
diff --git a/src/app/guards/auth.guard.ts b/src/app/guards/auth.guard.ts
index 5f42d2a..0d4456a 100644
--- a/src/app/guards/auth.guard.ts
+++ b/src/app/guards/auth.guard.ts
@@ -5,7 +5,7 @@ import {CookieService} from 'ngx-cookie-service';
export const authGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const cookieService = inject(CookieService);
- if (cookieService.get("author") !== '') {
+ if (cookieService.check("author") || cookieService.check("token")) {
router.navigate(['/']);
}
diff --git a/src/app/guards/writer.guard.ts b/src/app/guards/writer.guard.ts
index 6bb0526..7abe5a2 100644
--- a/src/app/guards/writer.guard.ts
+++ b/src/app/guards/writer.guard.ts
@@ -6,12 +6,9 @@ export const writerGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const cookieService = inject(CookieService);
- if (cookieService.get("author") !== '') {
- if (JSON.parse(cookieService.get("author")).role !== 'WRITER' && JSON.parse(cookieService.get("author")).role !== 'ADMIN')
- {
+ if (cookieService.get("author") === '' || (JSON.parse(cookieService.get("author")).role !== 'WRITER' && (cookieService.check("author") && cookieService.check("token")))) {
router.navigate(['/']);
}
- }
return true;
};
diff --git a/src/app/pages/home/home.component.ts b/src/app/pages/home/home.component.ts
index d9f09ed..f567ad0 100644
--- a/src/app/pages/home/home.component.ts
+++ b/src/app/pages/home/home.component.ts
@@ -1,19 +1,14 @@
import {Component, OnDestroy} from '@angular/core';
import {AvatarModule} from 'primeng/avatar';
-import {Button} from 'primeng/button';
-import {AuthorService} from '../../services/author.service';
import {Author} from '../../models/author';
-import {JsonPipe} from '@angular/common';
import {Subscription} from 'rxjs';
-import {MessageService} from 'primeng/api';
import {HeaderComponent} from '../../components/header/header.component';
import {ToastModule} from 'primeng/toast';
-import {CookieService} from 'ngx-cookie-service';
import {PostService} from '../../services/post.service';
-import {Post} from '../../models/post';
import {PostHomeComponent} from '../../components/post-home/post-home.component';
import {AuthorWithPost} from '../../models/author-with-post';
import {FooterComponent} from '../../components/footer/footer.component';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-home',
@@ -35,10 +30,10 @@ export class HomeComponent implements OnDestroy {
constructor(
private postService: PostService,
- private cookieService: CookieService) {
+ private authService: AuthService) {
- if (this.cookieService.get('author')) {
- this.actualAuthor = JSON.parse(this.cookieService.get('author'));
+ if (this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
}
this.subs.push(this.postService.listWithAuthors()
.subscribe({
diff --git a/src/app/pages/login/login.component.ts b/src/app/pages/login/login.component.ts
index a82bdb2..b40d179 100644
--- a/src/app/pages/login/login.component.ts
+++ b/src/app/pages/login/login.component.ts
@@ -11,6 +11,7 @@ import { CookieService } from 'ngx-cookie-service';
import {HeaderComponent} from '../../components/header/header.component';
import {Router} from '@angular/router';
import {FooterComponent} from '../../components/footer/footer.component';
+import {ConfigurationService} from '../../configuration.service';
@Component({
selector: 'app-login',
@@ -36,7 +37,8 @@ export class LoginComponent implements OnDestroy {
constructor(private authorService: AuthorService,
private messageService: MessageService,
private cookieService: CookieService,
- private router: Router) {}
+ private router: Router,
+ private configurationService: ConfigurationService,) {}
sendLogins(): void {
if (this.password === this.confirmPassword) {
@@ -44,13 +46,22 @@ export class LoginComponent implements OnDestroy {
(
this.authorService.login(this.name, this.password).pipe(
switchMap((tokenObj: any) => {
- this.cookieService.set("token", tokenObj.token);
+ this.cookieService.delete('token', '/', this.configurationService.getServerAddress())
+ this.cookieService.set("token", tokenObj.token, {
+ secure: true,
+ path: '/',
+ expires: new Date(new Date().getTime() + 1000 * 60),
+ sameSite: "Strict"});
return this.authorService.me(tokenObj.token)
}))
.subscribe({
next: (author: Author) => {
- console.log(author)
- this.cookieService.set("author", JSON.stringify(author), {path: '/'});
+ this.cookieService.delete('author', '/', this.configurationService.getServerAddress())
+ this.cookieService.set("author", JSON.stringify(author), {
+ secure : true,
+ path: '/',
+ expires: new Date(new Date().getTime() + 1000 * 60),
+ sameSite: "Strict" });
this.getAuthorCookie();
this.router.navigate(['/']).then(() => {
this.successMessage('Connecté avec succès', 'Heureux de vous revoir ' + this.actualAuthor?.name)
diff --git a/src/app/pages/logout/logout.component.ts b/src/app/pages/logout/logout.component.ts
index a29420b..6eff55e 100644
--- a/src/app/pages/logout/logout.component.ts
+++ b/src/app/pages/logout/logout.component.ts
@@ -21,8 +21,8 @@ export class LogoutComponent implements OnInit{
private router: Router,
private configurationService: ConfigurationService,) { }
ngOnInit(): void {
- this.cookiesService.delete('author', '/', this.configurationService.getServerAddress())
- this.cookiesService.delete('token', '/', this.configurationService.getServerAddress())
+ this.cookiesService.delete('author', '/', this.configurationService.getServerAddress());
+ this.cookiesService.delete('token', '/', this.configurationService.getServerAddress());
this.router.navigate(['/']).then(() => this.successMessage('Déconnexion', 'Vous avez été deconnecté avec succès'));
}
diff --git a/src/app/pages/my-posts/my-posts.component.ts b/src/app/pages/my-posts/my-posts.component.ts
index 6194a56..3d57e40 100644
--- a/src/app/pages/my-posts/my-posts.component.ts
+++ b/src/app/pages/my-posts/my-posts.component.ts
@@ -1,9 +1,8 @@
import {Component, OnDestroy} from '@angular/core';
import {HeaderComponent} from '../../components/header/header.component';
import {TableModule} from 'primeng/table';
-import {CookieService} from 'ngx-cookie-service';
import {AuthorService} from '../../services/author.service';
-import {ReplaySubject, Subscription} from 'rxjs';
+import {Subscription} from 'rxjs';
import {Post} from '../../models/post';
import {Author} from '../../models/author';
import {MessageService} from 'primeng/api';
@@ -15,6 +14,7 @@ import {PostHomeComponent} from '../../components/post-home/post-home.component'
import {PostService} from '../../services/post.service';
import {PostFormComponent} from "../../components/post-form/post-form.component";
import {FooterComponent} from '../../components/footer/footer.component';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-my-posts',
@@ -39,14 +39,14 @@ export class MyPostsComponent implements OnDestroy {
updateDialogVisibility: boolean[] = [];
deleteDialogVisibility: boolean[] = [];
posts: Post[] = [];
- actualAuthor: Author | undefined;
+ actualAuthor: Author;
- constructor(private cookieService: CookieService,
+ constructor(private authService: AuthService,
private postService: PostService,
private authorService: AuthorService,
private messageService: MessageService) {
- this.actualAuthor = this.cookieService.get('author') ? JSON.parse(this.cookieService.get('author')) : undefined;
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
this.updatePosts();
}
@@ -61,8 +61,8 @@ export class MyPostsComponent implements OnDestroy {
}
updatePosts(): void {
- if (this.cookieService.get('token')) {
- this.authorService.getAuthorsPosts(this.actualAuthor?.id, this.cookieService.get('token')).subscribe({
+ if (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),
}
@@ -71,7 +71,7 @@ export class MyPostsComponent implements OnDestroy {
}
deletePost(id: bigint, rowIndex: number) {
- this.postService.deletePost(id, this.cookieService.get('token')).subscribe({
+ this.postService.deletePost(id, this.authService.getAuthenticatedAuthorToken()).subscribe({
next: (_) => {
this.updatePosts()
this.successMessage("Post supprimé", "Ce post a été supprimé avec succès")
diff --git a/src/app/pages/new-post/new-post.component.ts b/src/app/pages/new-post/new-post.component.ts
index 36a9f59..4de2352 100644
--- a/src/app/pages/new-post/new-post.component.ts
+++ b/src/app/pages/new-post/new-post.component.ts
@@ -1,13 +1,11 @@
-import {Component, OnDestroy} from '@angular/core';
+import {Component, EventEmitter, OnDestroy} from '@angular/core';
import {HeaderComponent} from '../../components/header/header.component';
-import {FormBuilder, FormControl, FormGroup, ReactiveFormsModule, ValidationErrors, Validators} from '@angular/forms';
+import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
import {InputTextModule} from 'primeng/inputtext';
import {InputTextareaModule} from 'primeng/inputtextarea';
import {FileSelectEvent, FileUploadModule} from 'primeng/fileupload';
-import {mergeMap, Subscription, switchMap} from 'rxjs';
-import {Post} from '../../models/post';
+import {mergeMap, Subscription} from 'rxjs';
import {PostService} from '../../services/post.service';
-import {CookieService} from 'ngx-cookie-service';
import {MessageService} from 'primeng/api';
import {EditorModule} from 'primeng/editor';
import {AuthorService} from '../../services/author.service';
@@ -15,6 +13,7 @@ import {Author} from '../../models/author';
import {Router} from '@angular/router';
import {PostFormComponent} from '../../components/post-form/post-form.component';
import {FooterComponent} from '../../components/footer/footer.component';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-new-post',
@@ -33,6 +32,7 @@ import {FooterComponent} from '../../components/footer/footer.component';
styleUrl: './new-post.component.css'
})
export class NewPostComponent implements OnDestroy {
+ isSessionExpired: EventEmitter = new EventEmitter();
subs: Subscription[] = []
actualAuthor: Author | undefined;
uploadedFile: File | undefined;
@@ -40,9 +40,9 @@ export class NewPostComponent implements OnDestroy {
constructor(private formBuilder: FormBuilder,
private postService: PostService,
- private cookieService: CookieService,
private authorService: AuthorService,
private messageService: MessageService,
+ private authService : AuthService,
private router: Router) {
this.form = this.formBuilder.group({
description: ['', [Validators.required, Validators.maxLength(512)]],
@@ -50,8 +50,10 @@ export class NewPostComponent implements OnDestroy {
body: ['', [Validators.required]],
category: ['', [Validators.required, Validators.maxLength(50)]],
});
- if (this.cookieService.get("author")) {
- this.actualAuthor = JSON.parse(this.cookieService.get("author"));
+ if (this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
+ } else {
+ this.isSessionExpired.emit(true);
}
}
@@ -73,11 +75,11 @@ export class NewPostComponent implements OnDestroy {
};
this.subs.push(
- this.postService.createPost(postToPost, this.cookieService.get("token")).pipe(
+ this.postService.createPost(postToPost, this.authService.getAuthenticatedAuthorToken()).pipe(
mergeMap(post =>
- this.authorService.attributePost(this.actualAuthor?.id, post.id, this.cookieService.get("token")).pipe(
+ this.authorService.attributePost(this.actualAuthor?.id, post.id, this.authService.getAuthenticatedAuthorToken()).pipe(
mergeMap((_) =>
- this.postService.changeIllustration(post.id, this.uploadedFile, this.cookieService.get("token"))
+ this.postService.changeIllustration(post.id, this.uploadedFile, this.authService.getAuthenticatedAuthorToken()),
)
)
)
diff --git a/src/app/pages/post/post.component.ts b/src/app/pages/post/post.component.ts
index 97c134b..7135742 100644
--- a/src/app/pages/post/post.component.ts
+++ b/src/app/pages/post/post.component.ts
@@ -11,12 +11,12 @@ import {Comment} from '../../models/comment';
import {AvatarModule} from 'primeng/avatar';
import {CardModule} from 'primeng/card';
import {SafeHtmlPipe} from '../../pipes/safe-html-pipe';
-import {CookieService} from 'ngx-cookie-service';
import {Author} from '../../models/author';
import {CommentFormComponent} from '../../components/comment-form/comment-form.component';
import {FooterComponent} from '../../components/footer/footer.component';
import {Button} from 'primeng/button';
import {DialogModule} from 'primeng/dialog';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-post',
@@ -47,10 +47,10 @@ export class PostComponent {
private postService: PostService,
private commentService: CommentService,
private messageService: MessageService,
- private cookieService: CookieService,) {
+ private authService: AuthService,) {
this.route.paramMap.subscribe(params => {
- if (this.cookieService.get('author')) {
- this.actualAuthor = JSON.parse(this.cookieService.get('author'))
+ if (this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
}
const postId = params.get('postId');
if (postId) {
@@ -96,7 +96,7 @@ export class PostComponent {
}
deleteComment(comment: Comment) {
- const token = this.cookieService.get('token');
+ const token = this.authService.getAuthenticatedAuthorToken();
if (token) {
this.subs.push(
this.commentService.delete(comment.id, token).subscribe({
diff --git a/src/app/pages/profile/profile.component.ts b/src/app/pages/profile/profile.component.ts
index 565250e..1da3ae6 100644
--- a/src/app/pages/profile/profile.component.ts
+++ b/src/app/pages/profile/profile.component.ts
@@ -1,7 +1,6 @@
import {Component, OnDestroy} from '@angular/core';
import {HeaderComponent} from '../../components/header/header.component';
import {ActivatedRoute} from '@angular/router';
-import {CookieService} from 'ngx-cookie-service';
import {Author} from '../../models/author';
import {Subscription} from 'rxjs';
import {AuthorService} from '../../services/author.service';
@@ -11,6 +10,7 @@ import {Button} from 'primeng/button';
import {DialogModule} from 'primeng/dialog';
import {UpdateProfileFormComponent} from '../../components/update-profile-form/update-profile-form.component';
import {FooterComponent} from '../../components/footer/footer.component';
+import {AuthService} from '../../auth.service';
@Component({
selector: 'app-profile',
@@ -37,15 +37,15 @@ export class ProfileComponent implements OnDestroy {
constructor(private route: ActivatedRoute,
private authorService: AuthorService,
- private cookieService: CookieService) {
+ private authService: AuthService) {
this.route.paramMap.subscribe(params => {
this.subs.push(this.authorService.getAuthor(params.get('authorId')).subscribe(author => {
this.concernedAuthor = author;
this.authorName = author.name;
}));
})
- if (this.cookieService.get('author')) {
- this.actualAuthor = JSON.parse(this.cookieService.get("author"));
+ if (this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
}
}