From c3bd1188aa396cbf00c593d259a9da0eddeeea3b Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Wed, 4 May 2022 12:58:19 +0000 Subject: [PATCH] fix: expired token error --- frontend/src/api/pub.js | 10 +++++++--- frontend/src/api/utils.js | 10 +++++++--- http/auth.go | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/frontend/src/api/pub.js b/frontend/src/api/pub.js index f84504f9..1511143d 100644 --- a/frontend/src/api/pub.js +++ b/frontend/src/api/pub.js @@ -4,9 +4,13 @@ import { baseURL } from "@/utils/constants"; export async function fetch(url, password = "") { url = removePrefix(url); - const res = await fetchURL(`/api/public/share${url}`, { - headers: { "X-SHARE-PASSWORD": encodeURIComponent(password) }, - }); + const res = await fetchURL( + `/api/public/share${url}`, + { + headers: { "X-SHARE-PASSWORD": encodeURIComponent(password) }, + }, + false + ); let data = await res.json(); data.url = `/share${url}`; diff --git a/frontend/src/api/utils.js b/frontend/src/api/utils.js index 549a74ec..ddddcfa7 100644 --- a/frontend/src/api/utils.js +++ b/frontend/src/api/utils.js @@ -1,9 +1,9 @@ import store from "@/store"; -import { renew } from "@/utils/auth"; +import { renew, logout } from "@/utils/auth"; import { baseURL } from "@/utils/constants"; import { encodePath } from "@/utils/url"; -export async function fetchURL(url, opts) { +export async function fetchURL(url, opts, auth = true) { opts = opts || {}; opts.headers = opts.headers || {}; @@ -25,7 +25,7 @@ export async function fetchURL(url, opts) { throw error; } - if (res.headers.get("X-Renew-Token") === "true") { + if (auth && res.headers.get("X-Renew-Token") === "true") { await renew(store.state.jwt); } @@ -33,6 +33,10 @@ export async function fetchURL(url, opts) { const error = new Error(await res.text()); error.status = res.status; + if (auth && res.status == 401) { + logout(); + } + throw error; } diff --git a/http/auth.go b/http/auth.go index c243295f..447af91a 100644 --- a/http/auth.go +++ b/http/auth.go @@ -71,7 +71,7 @@ func withUser(fn handleFunc) handleFunc { token, err := request.ParseFromRequest(r, &extractor{}, keyFunc, request.WithClaims(&tk)) if err != nil || !token.Valid { - return http.StatusForbidden, nil + return http.StatusUnauthorized, nil } expired := !tk.VerifyExpiresAt(time.Now().Add(time.Hour).Unix(), true)