Skip to content

Commit

Permalink
Merge pull request #31 from jorbush/fix-jwt
Browse files Browse the repository at this point in the history
Fix JWT token issue
  • Loading branch information
jorbush authored Sep 30, 2024
2 parents 16fc324 + b6cfb6f commit 12451c6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/fly-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
flyctl secrets set SPRING_DATASOURCE_URL="${{ secrets.SPRING_DATASOURCE_URL }}"
flyctl secrets set SPRING_DATASOURCE_USERNAME="${{ secrets.SPRING_DATASOURCE_USERNAME }}"
flyctl secrets set SPRING_DATASOURCE_PASSWORD="${{ secrets.SPRING_DATASOURCE_PASSWORD }}"
flyctl secrets set APP_JWTSECRET="${{ secrets.APP_JWTSECRET }}"
- name: Deploy to Fly.io
env:
Expand Down
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 12451c6

Please sign in to comment.