-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-refresh.go
More file actions
42 lines (33 loc) · 1006 Bytes
/
user-refresh.go
File metadata and controls
42 lines (33 loc) · 1006 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
33
34
35
36
37
38
39
40
41
42
package main
import (
"log"
"net/http"
"github.com/mdmdj/bootdev-chirpy/internal/auth"
)
type refreshResponse struct {
Token string `json:"token"`
}
func refreshRoute(w http.ResponseWriter, r *http.Request) {
refreshToken, err := auth.GetBearerToken(r.Header)
if err != nil {
log.Printf("refreshRoute, bad bearer token header: %s", err)
respondWithError(w, http.StatusBadRequest, "Something went wrong")
return
}
dbUser, err := cfg.db.GetUserFromRefreshToken(r.Context(), refreshToken)
if err != nil {
log.Printf("refreshRoute, token not found: %s, %s", refreshToken, err)
respondWithError(w, http.StatusUnauthorized, "Something went wrong")
return
}
tokenString, err := auth.MakeJWT(dbUser.ID, cfg.secret, accessTokenTimeout)
if err != nil {
log.Printf("Error making JWT: %s", err)
respondWithError(w, http.StatusInternalServerError, "Something went wrong")
return
}
result := refreshResponse{
Token: tokenString,
}
respondWithJSON(w, http.StatusOK, result)
}