feat(auth): add logout functionality
- Implement LogoutHandler to handle user logout requests - Add DeleteTokenInfo and GetUserIDFromToken functions to auth module - Update README.md with logout endpoint documentation
This commit is contained in:
+29
@@ -159,3 +159,32 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
postLog.Info(fmt.Sprintf("[LoginHandler] User \"%s\" Login successful", req.Username))
|
||||
}
|
||||
|
||||
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Warning(fmt.Sprintf("[LogoutHandler] Invalid request method: %s", r.Method))
|
||||
return
|
||||
}
|
||||
|
||||
if !ValidateTimeStamp(r.Header) {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid or missing X-Timestamp in header")
|
||||
return
|
||||
}
|
||||
|
||||
userID, err := GetUserIDFromToken(r.Header.Get("X-Token"))
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, "Invalid or missing token")
|
||||
postLog.Warning(fmt.Sprintf("[LogoutHandler] Invalid or missing token: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := DeleteTokenInfo(userID); err != nil {
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to logout")
|
||||
postLog.Error(fmt.Sprintf("[LogoutHandler] Failed to logout user [%d]%s: %v", userID, GetUsernameByID(userID), err))
|
||||
return
|
||||
}
|
||||
|
||||
SendSuccessResponse(w, "Logout successful", nil)
|
||||
postLog.Info(fmt.Sprintf("[LogoutHandler] User [%d]%s Logout successful", userID, GetUsernameByID(userID)))
|
||||
}
|
||||
Reference in New Issue
Block a user