refactor(auth): extract timestamp validation to separate function
Move timestamp validation logic from handlers to ValidateTimeStamp function in auth.go to avoid code duplication and improve maintainability
This commit is contained in:
+6
-9
@@ -54,9 +54,8 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
currentTime := time.Now().UnixMilli()
|
||||
if !globalConfig.Debug && (currentTime-req.TimeStamp > 3000 || req.TimeStamp-currentTime > 3000) {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "timestamp out of valid range")
|
||||
if err := ValidateTimeStamp(req.TimeStamp); err != nil {
|
||||
SendErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -128,9 +127,8 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
currentTime := time.Now().UnixMilli()
|
||||
if !globalConfig.Debug && (currentTime-req.TimeStamp > 3000 || req.TimeStamp-currentTime > 3000) {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "timestamp out of valid range")
|
||||
if err := ValidateTimeStamp(req.TimeStamp); err != nil {
|
||||
SendErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -227,9 +225,8 @@ func ValidateRequestWithBody(w http.ResponseWriter, r *http.Request, body []byte
|
||||
return 0, "", errors.New("timeStamp is required")
|
||||
}
|
||||
|
||||
currentTime := time.Now().UnixMilli()
|
||||
if !globalConfig.Debug && (currentTime-int64(timeStamp) > 3000 || int64(timeStamp)-currentTime > 3000) {
|
||||
return 0, "", errors.New("timestamp out of valid range")
|
||||
if err := ValidateTimeStamp(int64(timeStamp)); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
userID, err := extractUserIDFromToken(token)
|
||||
|
||||
Reference in New Issue
Block a user