Browse Source

Refresh token on every request

master
Immanuel Onyeka 10 months ago
parent
commit
9e0435121b
1 changed files with 11 additions and 1 deletions
  1. +11
    -1
      skouter.go

+ 11
- 1
skouter.go View File

@@ -961,9 +961,13 @@ func setTokenCookie(id int, role string, w http.ResponseWriter) error {
return nil return nil
} }


func getToken(w http.ResponseWriter, db *sql.DB, r *http.Request) {
func refreshToken(w http.ResponseWriter, db *sql.DB, r *http.Request) {
claims, _ := getClaims(r) claims, _ := getClaims(r)
if !claims.Valid() {
return
}
err := setTokenCookie(claims.Id, claims.Role, w) err := setTokenCookie(claims.Id, claims.Role, w)


if err != nil { if err != nil {
@@ -971,6 +975,8 @@ func getToken(w http.ResponseWriter, db *sql.DB, r *http.Request) {
"Token generation error", "Token generation error",
http.StatusInternalServerError) http.StatusInternalServerError)
} }
return
} }


func getClaims(r *http.Request) (UserClaims, error) { func getClaims(r *http.Request) (UserClaims, error) {
@@ -3554,8 +3560,12 @@ func api(w http.ResponseWriter, r *http.Request) {
panic(err) panic(err)
// maybe os.Exit(1) instead // maybe os.Exit(1) instead
} }
refreshToken(w, db, r)


switch { switch {
case match(p, "/api/refreshTokeen", &args):
// Dummy case to trigger refreshToken() without sending 404
case match(p, "/api/login", &args) && case match(p, "/api/login", &args) &&
r.Method == http.MethodPost: r.Method == http.MethodPost:
login(w, db, r) login(w, db, r)


Loading…
Cancel
Save