diff --git a/src/app/pages/login/login.component.ts b/src/app/pages/login/login.component.ts
index a82bdb2..f67d88f 100644
--- a/src/app/pages/login/login.component.ts
+++ b/src/app/pages/login/login.component.ts
@@ -1,4 +1,4 @@
-import {ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA, OnDestroy} from '@angular/core';
+import {Component, OnDestroy} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {InputTextModule} from 'primeng/inputtext';
import {Button} from 'primeng/button';
@@ -7,10 +7,12 @@ import {ToastModule} from 'primeng/toast';
import {MessageService} from 'primeng/api';
import {Author} from '../../models/author';
import {Subscription, switchMap} from 'rxjs';
-import { CookieService } from 'ngx-cookie-service';
+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';
+import {DateTime} from 'luxon';
@Component({
selector: 'app-login',
@@ -36,7 +38,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 +47,23 @@ 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: '/'
+ });
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: '/' });
+ this.cookieService.set('token-expiration-date', DateTime.now().plus({millisecond: this.configurationService.getTokenTTL()}).toISO(), {
+ secure: true,
+ path: '/',
+ })
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..19f422e 100644
--- a/src/app/pages/logout/logout.component.ts
+++ b/src/app/pages/logout/logout.component.ts
@@ -16,13 +16,18 @@ import {ConfigurationService} from '../../configuration.service';
})
export class LogoutComponent implements OnInit{
- constructor(private cookiesService: CookieService,
+ constructor(private cookieService: CookieService,
private messageService: MessageService,
private router: Router,
private configurationService: ConfigurationService,) { }
ngOnInit(): void {
- this.cookiesService.delete('author', '/', this.configurationService.getServerAddress())
- this.cookiesService.delete('token', '/', this.configurationService.getServerAddress())
+ console.log(this.cookieService.getAll())
+ const routes: string[] = ['/', '/login', '/register', '/logout', '/profile', '/post', '/new-post']
+ Object.keys(this.cookieService.getAll()).forEach(key => {
+ routes.forEach(route => {
+ this.cookieService.delete(key, route, 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..41e3091 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,17 +61,19 @@ 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.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),
}
)
+ } else {
+ this.authService.checkSessionExpiration();
}
}
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..9f46dd2 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.isSessionExpired()) && this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
+ } else {
+ this.authService.checkSessionExpiration();
}
}
@@ -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..81fa62a 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,12 @@ 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.isSessionExpired()) && this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
+ } else {
+ this.authService.checkSessionExpiration();
}
const postId = params.get('postId');
if (postId) {
@@ -96,7 +98,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.html b/src/app/pages/profile/profile.component.html
index 4142855..ecdbf9d 100644
--- a/src/app/pages/profile/profile.component.html
+++ b/src/app/pages/profile/profile.component.html
@@ -25,11 +25,6 @@
-
-
-
-
}
}
diff --git a/src/app/pages/profile/profile.component.ts b/src/app/pages/profile/profile.component.ts
index 565250e..de4ac47 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,17 @@ 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.isSessionExpired()) && this.authService.isAuthenticated()) {
+ this.actualAuthor = this.authService.getAuthenticatedAuthor();
+ } else {
+ this.authService.checkSessionExpiration();
}
}
diff --git a/tsconfig.json b/tsconfig.json
index a8bb65b..727d676 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -12,10 +12,11 @@
"skipLibCheck": true,
"isolatedModules": true,
"esModuleInterop": true,
+ "resolveJsonModule": true,
"sourceMap": true,
"declaration": false,
"experimentalDecorators": true,
- "moduleResolution": "bundler",
+ "moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",