Skip to content

Commit a78b3cd

Browse files
committed
update to API spec
1 parent 1209624 commit a78b3cd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/dao/user.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ func (d *UserDao) GenerateOTP(c context.Context, username string) (api.OTPRespon
267267
}
268268
return api.OTPResponse{
269269
Otp: otpCode,
270-
Username: user.Username,
271270
ValidUntil: exp_date,
272271
}, nil
273272
}
@@ -293,3 +292,22 @@ func (d *UserDao) ValidateOTP(c context.Context, otpCode string) (bool, error) {
293292

294293
return true, nil
295294
}
295+
296+
func (d *UserDao) GetUserByOTP(c context.Context, otpCode string) (*api.UserResponse, error) {
297+
query := `
298+
SELECT u.user_id, u.username, u.email, u.name, u.surname, u.role
299+
FROM users u
300+
JOIN otps o ON u.user_id = o.user_id
301+
WHERE o.otp_code = $1 AND o.expires_at > CURRENT_TIMESTAMP
302+
`
303+
row := d.db.QueryRow(c, query, otpCode)
304+
305+
var user api.UserResponse
306+
if err := row.Scan(&user.Id, &user.Username, &user.Email, &user.Name, &user.Surname, &user.Role); err != nil {
307+
if strings.Contains(err.Error(), "no rows in result set") {
308+
return nil, ErrUserNotFound
309+
}
310+
return nil, fmt.Errorf("failed to get user by OTP: %w", err)
311+
}
312+
return &user, nil
313+
}

src/handlers/session.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,17 @@ func (h *SessionHandlers) ValidateOTP(c *gin.Context) {
276276
}
277277
c.JSON(http.StatusOK, gin.H{"message": "OTP validated successfully"})
278278
}
279+
280+
func (h *SessionHandlers) GetUserByOTP(c *gin.Context, otp string) {
281+
user, err := h.dao.GetUserByOTP(c.Request.Context(), otp)
282+
if err != nil {
283+
if err == dao.ErrUserNotFound {
284+
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
285+
return
286+
}
287+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user by OTP"})
288+
return
289+
}
290+
291+
c.JSON(http.StatusOK, user.Username)
292+
}

0 commit comments

Comments
 (0)