-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_revoke.go
More file actions
32 lines (26 loc) · 853 Bytes
/
handle_revoke.go
File metadata and controls
32 lines (26 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"net/http"
"github.com/hawkaii/Chirpy-go/internal/auth"
)
func (cfg *apiConfig) handlerRevoke(w http.ResponseWriter, r *http.Request) {
// Extract the refresh token from the Authorization header
refreshToken, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find refresh token")
return
}
// Validate the refresh token and retrieve the associated user
user, err := cfg.DB.ValidateRefreshToken(refreshToken)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Invalid or expired refresh token")
return
}
// Revoke the refresh token
err = cfg.DB.DeleteRefreshToken(user.ID)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't revoke refresh token")
return
}
respondWithJSON(w, http.StatusNoContent, nil)
}