-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh.go
More file actions
43 lines (29 loc) · 860 Bytes
/
refresh.go
File metadata and controls
43 lines (29 loc) · 860 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
43
package main
import (
"net/http"
"time"
"github.com/SandeshNarayan/chirpy/internal/auth"
)
func (cfg *apiConfig) handlerRefresh(w http.ResponseWriter, r *http.Request){
accessToken, err := auth.GetBearerToken(r.Header)
if err!=nil {
respondWithError(w, http.StatusUnauthorized, "Missing or invalid token", err)
return
}
user, err := cfg.dbQueries.GetUserFromToken(r.Context(), accessToken)
if err!=nil {
respondWithError(w, http.StatusUnauthorized, "Invalid token", err)
return
}
newAccessToken, err := auth.MakeJWT(user.ID, cfg.jwtSecret, time.Hour)
if err!=nil{
respondWithError(w, http.StatusInternalServerError, "could not create token", err)
return
}
type response struct{
Token string `json:"token"`
}
respondWithJson(w, http.StatusOK, response{
Token: newAccessToken,
})
}