Skip to content

Commit

Permalink
catch expired error and increase time
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbush committed Sep 30, 2024
1 parent 16fc324 commit 0ed67cc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public List<PostResponseDTO> getPostsByUser(@PathVariable final Long userId) {
@PostMapping
public ResponseEntity<PostResponseDTO> createPost(
@RequestBody final PostRequest postRequest, final Authentication authentication) {
if (authentication == null) {
return ResponseEntity.status(401).build();
}
String username = authentication.getName();
User user = userService.findByUsername(username);

Expand All @@ -57,6 +60,9 @@ public ResponseEntity<PostResponseDTO> updatePost(
@PathVariable final Long id,
@RequestBody final PostRequest postRequest,
final Authentication authentication) {
if (authentication == null) {
return ResponseEntity.status(401).build();
}
String username = authentication.getName();
User user = userService.findByUsername(username);

Expand All @@ -71,6 +77,9 @@ public ResponseEntity<PostResponseDTO> updatePost(
@DeleteMapping("/{id}")
public ResponseEntity<Void> deletePost(
@PathVariable final Long id, final Authentication authentication) {
if (authentication == null) {
return ResponseEntity.status(401).build();
}
String username = authentication.getName();
User user = userService.findByUsername(username);
postService.deletePost(id, user);
Expand Down
2 changes: 1 addition & 1 deletion postrify-backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
app.jwtSecret=YourJwtSecretKey
app.jwtExpirationInMs=86400000
app.jwtExpirationInMs=2592000000
spring.jpa.open-in-view=false
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PostResponseDTO } from '../../models/post-response.model';
import { AuthService } from '../../services/auth.service';
import { PostService } from '../../services/post.service';
import { CommonModule } from '@angular/common';
import { ToastService } from '../../services/toast.service';

@Component({
selector: 'app-post-detail',
Expand Down Expand Up @@ -219,6 +220,7 @@ export class PostDetailComponent implements OnInit {
private postService: PostService,
private router: Router,
private authService: AuthService,
private toastService: ToastService,
) {}

ngOnInit(): void {
Expand Down Expand Up @@ -249,7 +251,17 @@ export class PostDetailComponent implements OnInit {
console.log('Post deleted successfully');
this.router.navigate(['/']);
},
error: (error) => console.error('Error deleting post', error),
error: (error) => {
console.error('Error deleting post', error);
this.toastService.show('Error deleting post', 'error');
if (error.status === 401) {
this.toastService.show(
'You need to be logged in to delete a post',
'error',
);
this.router.navigate(['/login']);
}
},
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export class PostFormComponent {
error: (error) => {
console.error('Error creating post', error);
this.toastService.show('Error creating post', 'error');
if (error.status === 401) {
this.toastService.show(
'You need to be logged in to create a post',
'error',
);
this.router.navigate(['/login']);
}
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export class PostUpdateComponent implements OnInit {
},
error: (error) => {
console.error('Error fetching post data', error);
this.toastService.show('Error fetching post data', 'error');
},
});
}
Expand All @@ -185,6 +186,13 @@ export class PostUpdateComponent implements OnInit {
error: (error) => {
console.error('Error updating post', error);
this.toastService.show('Error updating post', 'error');
if (error.status === 401) {
this.toastService.show(
'You need to be logged in to update a post',
'error',
);
this.router.navigate(['/login']);
}
},
});
}
Expand Down

0 comments on commit 0ed67cc

Please sign in to comment.