-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_refresh.go
More file actions
44 lines (33 loc) · 825 Bytes
/
Copy pathhandler_refresh.go
File metadata and controls
44 lines (33 loc) · 825 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
44
package main
import (
"net/http"
"time"
"github.com/e-300/http-server-go/internal/auth"
)
func (cfg *apiConfig) handlerRefresh(w http.ResponseWriter, r *http.Request){
type response struct{
Token string `json:"token"`
}
refreshToken, err := auth.GetBearerToken(r.Header)
if err != nil{
respondWithError(w, http.StatusBadRequest, "Couldnt find Token", err)
return
}
user, err := cfg.db.GetUserFromRefreshToken(r.Context(), refreshToken)
if err != nil{
respondWithError(w, http.StatusUnauthorized, "Token Not in DB", err)
return
}
accessToken, err := auth.MakeJWT(
user.ID,
cfg.jwtSecret,
time.Hour,
)
if err != nil{
respondWithError(w, http.StatusUnauthorized, "Couldnt issue access Token", err)
return
}
respondWithJSON(w, http.StatusOK, response{
Token: accessToken,
})
}