33import java .time .LocalDateTime ;
44import java .util .UUID ;
55
6- import org .springframework .scheduling .annotation .Async ;
6+ import org .slf4j .Logger ;
7+ import org .slf4j .LoggerFactory ;
78import org .springframework .security .core .userdetails .UserDetails ;
89import org .springframework .security .core .userdetails .UserDetailsService ;
910import org .springframework .security .crypto .password .PasswordEncoder ;
1213
1314import com .meevent .webapi .dto .request .LoginRequest ;
1415import com .meevent .webapi .dto .request .RegisterRequest ;
15- import com .meevent .webapi .dto . request .VerificationToken ;
16+ import com .meevent .webapi .model .VerificationToken ;
1617import com .meevent .webapi .dto .response .AuthResponse ;
1718import com .meevent .webapi .model .AttendeeProfile ;
1819import com .meevent .webapi .model .City ;
3132@ RequiredArgsConstructor
3233public class AuthService {
3334
35+ private static final Logger LOGGER = LoggerFactory .getLogger (AuthService .class );
36+
3437 private final IMailService mailService ; /* <-- Azure service implementation */
3538 private final IVerificationTokenRepository tokenRepository ;
3639 private final IUserRepository userRepository ;
@@ -49,7 +52,13 @@ public AuthResponse login(LoginRequest request) {
4952 );
5053
5154 if (!user .getActive ()) {
52- throw new IllegalStateException ("Account is disabled" );
55+ LOGGER .warn ("Login attempt on disabled account: email={}" , request .email ());
56+ throw new IllegalArgumentException ("Invalid credentials" );
57+ }
58+
59+ if (user .getVerificationStatus () != UserVerificationStatus .VERIFIED ) {
60+ LOGGER .warn ("Login attempt on unverified account: email={}" , request .email ());
61+ throw new IllegalArgumentException ("Invalid credentials" );
5362 }
5463
5564 if (!passwordEncoder .matches (
@@ -68,8 +77,7 @@ public AuthResponse login(LoginRequest request) {
6877 }
6978
7079 @ Transactional
71- @ Async
72- public AuthResponse register (RegisterRequest request ) {
80+ public void register (RegisterRequest request ) {
7381
7482 if (userRepository .existsByEmailIgnoreCase (request .email ())) {
7583 throw new IllegalArgumentException ("Email is already registered" );
@@ -97,16 +105,15 @@ public AuthResponse register(RegisterRequest request) {
97105 user .setVerificationStatus (UserVerificationStatus .PENDING ); /* <--- change form not_verified to ---> pending */
98106
99107 userRepository .save (user );
108+ LOGGER .info ("User registered successfully: email={}" , user .getEmail ());
100109
101110 String tokenValue = UUID .randomUUID ().toString ();
102111 VerificationToken token = new VerificationToken (tokenValue , user , 1 ); /*<--- An hour to expire the token */
103112 tokenRepository .save (token );
104113 String subject = "Verifica tu cuenta en Meevent" ;
105114 String message = "¡Hola! Gracias por registrarte. Haz clic en el siguiente enlace para verificar tu cuenta: " ;
106115
107- String verificationLink = tokenValue ;
108-
109- mailService .sendVerificationEmail (user .getEmail (), subject , message , verificationLink );
116+ mailService .sendVerificationEmail (user .getEmail (), subject , message , tokenValue );
110117
111118 City city = cityRepository .findById (request .cityId ())
112119 .orElseThrow (()
@@ -123,35 +130,36 @@ public AuthResponse register(RegisterRequest request) {
123130 attendeeProfile .setPhoneE164 (phoneE164 );
124131
125132 attendeeProfileRepository .save (attendeeProfile );
126-
127- UserDetails userDetails
128- = userDetailsService .loadUserByUsername (user .getEmail ());
129-
130- return new AuthResponse (
131- jwtService .generateToken (userDetails )
132- );
133133 }
134134
135135 @ Transactional
136- public void verifyEmail (String tokenValue ) {
136+ public AuthResponse verifyEmail (String tokenValue ) {
137137 VerificationToken token = tokenRepository .findByToken (tokenValue )
138- .orElseThrow (() -> new RuntimeException ("Token no encontrado" ));
138+ .orElseThrow (() -> {
139+ LOGGER .warn ("Verification attempt with unknown token" );
140+ return new RuntimeException ("Token invalido o expirado" );
141+ });
139142
140143 if (token .isUsed ()) {
141- throw new RuntimeException ("Este token ya ha sido utilizado" );
144+ LOGGER .warn ("Verification attempt with used token: tokenId={}" , token .getId ());
145+ throw new RuntimeException ("Token invalido o expirado" );
142146 }
143147
144148 if (token .getExpiryDate ().isBefore (LocalDateTime .now ())) {
145- throw new RuntimeException ("El token ha expirado" );
149+ LOGGER .warn ("Verification attempt with expired token: tokenId={}" , token .getId ());
150+ throw new RuntimeException ("Token invalido o expirado" );
146151 }
147152
148153 // User updated
149154 User user = token .getToUser ();
150155 user .setVerificationStatus (UserVerificationStatus .VERIFIED );
151156 userRepository .save (user );
152157
153- // Change state of token from PENDING to USED creo
158+ // Change state of token from PENDING to USED creo
154159 token .setUsed (true );
155160 tokenRepository .save (token );
161+
162+ UserDetails userDetails = userDetailsService .loadUserByUsername (user .getEmail ());
163+ return new AuthResponse (jwtService .generateToken (userDetails ));
156164 }
157165}
0 commit comments