22
33import com .example .usermanagement .dto .accounts .*;
44import com .example .usermanagement .entities .Account ;
5+ import com .example .usermanagement .events .publishers .emails .EmailVerificationTokenGeneratedEvent ;
6+ import com .example .usermanagement .events .publishers .emails .PasswordResetGeneratedEvent ;
57import com .example .usermanagement .interfaces .services .IAccountService ;
8+ import com .example .usermanagement .interfaces .services .IEmailService ;
69import com .example .usermanagement .interfaces .services .IEmailVerificationTokenService ;
7- import jakarta . persistence . EntityExistsException ;
10+ import com . example . usermanagement . interfaces . services . IPasswordResetTokenService ;
811import lombok .RequiredArgsConstructor ;
12+ import org .springframework .context .ApplicationEventPublisher ;
913import org .springframework .data .domain .Page ;
1014import org .springframework .http .HttpStatus ;
1115import org .springframework .http .ResponseEntity ;
@@ -21,8 +25,11 @@ public class AccountController {
2125
2226 private final IAccountService accountService ;
2327 private final IEmailVerificationTokenService emailVerificationTokenService ;
28+ private final IPasswordResetTokenService passwordResetTokenService ;
29+ private final IEmailService emailService ;
30+ private final ApplicationEventPublisher eventPublisher ;
2431
25- // auth related
32+ // account management related
2633 @ PostMapping
2734 public ResponseEntity <UUID > createAccount (@ RequestBody CreateAccountDTO requestBody ) {
2835
@@ -32,49 +39,12 @@ public ResponseEntity<UUID> createAccount(@RequestBody CreateAccountDTO requestB
3239 // generate email verification token
3340 String token = emailVerificationTokenService .generateEmailVerificationToken (userAccount );
3441
35- // TODO: send email with token
42+ String body = "Click here to verify your email: http://localhost:8080/api/accounts/verify-email?token=" + token ;
43+ emailService .
sendEmail (
"[email protected] " ,
"Email verification" ,
body );
3644
3745 return new ResponseEntity <>(userAccount .getId (), HttpStatus .CREATED );
3846 }
3947
40- @ GetMapping ("/verify-email" )
41- public ResponseEntity <String > verifyEmail (@ RequestParam String token ) {
42- String email = emailVerificationTokenService .consumeEmailVerificationToken (token );
43- accountService .verifyAccountEmail (email );
44- return new ResponseEntity <>("Email verified" , HttpStatus .OK );
45- }
46-
47- // resend email verification token
48- @ PostMapping ("/verify-email/resend" )
49- public ResponseEntity <String > resendEmailVerificationToken (@ RequestParam String email ) {
50- Account account = accountService .getAccountByEmail (email );
51- String token = emailVerificationTokenService .generateEmailVerificationToken (account );
52-
53- // TODO: send email with token
54-
55- return new ResponseEntity <>("Email verification token sent" , HttpStatus .OK );
56- }
57-
58- @ PostMapping ("/reset-password/request" )
59- public ResponseEntity <String > resetPassword (@ RequestParam String email ) {
60- Account account = accountService .getAccountByEmail (email );
61- accountService .requestResetPassword (account );
62- return new ResponseEntity <>("Password reset" , HttpStatus .OK );
63- }
64-
65- @ PostMapping ("/reset-password/confirm" )
66- public ResponseEntity <String > resetPassword (@ RequestBody ResetPasswordRequest requestBody ) {
67- accountService .resetPassword (requestBody .getToken (), requestBody .getNewPassword ());
68- return new ResponseEntity <>("Password reset" , HttpStatus .OK );
69- }
70-
71- @ PostMapping ("/change-password" )
72- public ResponseEntity <String > changePassword (@ RequestBody ChangePasswordRequest requestBody ) {
73- accountService .changeMyPassword (requestBody .getOldPassword (), requestBody .getNewPassword ());
74- return new ResponseEntity <>("Password changed" , HttpStatus .OK );
75- }
76-
77- // info related
7848 @ GetMapping
7949 public ResponseEntity <Page <GeneralAccountDTO >> getAccounts (
8050 @ RequestParam (required = false ) String email ,
@@ -111,6 +81,51 @@ public ResponseEntity<List<AccountAuthoritiesEditResponse>> editAuthorities(@Req
11181 return new ResponseEntity <>(res , HttpStatus .OK );
11282 }
11383
84+
85+ // email verification related
86+ @ GetMapping ("/verify-email" )
87+ public ResponseEntity <String > verifyEmail (@ RequestParam String token ) {
88+ String email = emailVerificationTokenService .consumeEmailVerificationToken (token );
89+ accountService .verifyAccountEmail (email );
90+ return new ResponseEntity <>("Email verified" , HttpStatus .OK );
91+ }
92+
93+ @ PostMapping ("/verify-email/resend" )
94+ public ResponseEntity <String > resendEmailVerificationToken (@ RequestParam String email ) {
95+ Account account = accountService .getAccountByEmail (email );
96+ String token = emailVerificationTokenService .generateEmailVerificationToken (account );
97+
98+ eventPublisher .publishEvent (new EmailVerificationTokenGeneratedEvent (this , token , email ));
99+
100+ return new ResponseEntity <>("Email verification token sent" , HttpStatus .OK );
101+ }
102+
103+
104+ // password related
105+ @ PostMapping ("/reset-password/resend" )
106+ public ResponseEntity <String > requestResetPassword (@ RequestParam String email ) {
107+ Account account = accountService .getAccountByEmail (email );
108+ String token = passwordResetTokenService .generatePasswordResetToken (account );
109+
110+ eventPublisher .publishEvent (new PasswordResetGeneratedEvent (this , email , token ));
111+
112+ return new ResponseEntity <>("Password reset token sent" , HttpStatus .OK );
113+ }
114+
115+ @ PostMapping ("/reset-password" )
116+ public ResponseEntity <String > confirmResetPassword (@ RequestBody ResetPasswordRequest requestBody ) {
117+ accountService .resetPassword (requestBody .getToken (), requestBody .getNewPassword ());
118+ return new ResponseEntity <>("Password reset" , HttpStatus .OK );
119+ }
120+
121+ @ PostMapping ("/change-password" )
122+ public ResponseEntity <String > changePassword (@ RequestBody ChangePasswordRequest requestBody ) {
123+ accountService .changeMyPassword (requestBody .getOldPassword (), requestBody .getNewPassword ());
124+ return new ResponseEntity <>("Password changed" , HttpStatus .OK );
125+ }
126+
127+
128+ // special info management related
114129 @ PostMapping ("/{accountId}/identity-verification" )
115130 public ResponseEntity <String > verifyIdentity (@ RequestParam boolean verify , @ PathVariable UUID accountId ) {
116131 Account account = accountService .getAccountById (accountId );
0 commit comments