浏览代码

Refresh token on every request

master
Immanuel Onyeka 10 个月前
父节点
当前提交
9e0435121b
共有 1 个文件被更改,包括 11 次插入1 次删除
  1. +11
    -1
      skouter.go

+ 11
- 1
skouter.go 查看文件

@@ -961,9 +961,13 @@ func setTokenCookie(id int, role string, w http.ResponseWriter) error {
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)
if !claims.Valid() {
return
}
err := setTokenCookie(claims.Id, claims.Role, w)

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

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

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


正在加载...
取消
保存