From 560944597073a3a1d56e2bb2f4d43a756b3dccc5 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 2 Feb 2021 09:58:35 +0200 Subject: Improved how post page handles user that isn't logged in --- .../app/components/post-page/post-page.component.html | 3 ++- .../src/app/components/post-page/post-page.component.ts | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'src/DevHive.Angular') diff --git a/src/DevHive.Angular/src/app/components/post-page/post-page.component.html b/src/DevHive.Angular/src/app/components/post-page/post-page.component.html index f22646d..08e63f9 100644 --- a/src/DevHive.Angular/src/app/components/post-page/post-page.component.html +++ b/src/DevHive.Angular/src/app/components/post-page/post-page.component.html @@ -1,8 +1,9 @@
-
+
+
diff --git a/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts b/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts index a7843e3..95bf58f 100644 --- a/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts +++ b/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts @@ -16,6 +16,7 @@ import { Post } from 'src/models/post'; }) export class PostPageComponent implements OnInit { private _title = 'Post'; + public loggedIn = false; public editable = false; public editingPost = false; public postId: Guid; @@ -28,6 +29,7 @@ export class PostPageComponent implements OnInit { } ngOnInit(): void { + this.loggedIn = this._tokenService.getTokenFromSessionStorage() !== ''; this.postId = Guid.parse(this._router.url.substring(6)); // Gets the post and the logged in user and compares them, @@ -35,7 +37,9 @@ export class PostPageComponent implements OnInit { this._postService.getPostRequest(this.postId).subscribe( (result: object) => { this.post = result as Post; - this.editable = this.post.creatorUsername === this._tokenService.getUsernameFromSessionStorageToken(); + if (this.loggedIn) { + this.editable = this.post.creatorUsername === this._tokenService.getUsernameFromSessionStorageToken(); + } }, (err: HttpErrorResponse) => { this._router.navigate(['/not-found']); @@ -59,9 +63,13 @@ export class PostPageComponent implements OnInit { this._router.navigate(['/profile/' + this.post.creatorUsername]); } + toLogin(): void { + this._router.navigate(['/login']); + } + editPost(): void { if (this._tokenService.getTokenFromSessionStorage() === '') { - this._router.navigate(['/login']); + this.toLogin(); return; } @@ -79,6 +87,11 @@ export class PostPageComponent implements OnInit { } addComment(): void { + if (!this.loggedIn) { + this._router.navigate(['/login']); + return; + } + const newComment = this.addCommentFormGroup.get('newComment')?.value; if (newComment !== '' && newComment !== null) { this._commentService.createCommentWithSessionStorageRequest(this.postId, newComment).subscribe( -- cgit v1.2.3