Skip to content

Commit

Permalink
fix: expired token error
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiresviana committed May 4, 2022
1 parent fc209f6 commit c3bd118
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 7 additions & 3 deletions frontend/src/api/pub.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/api/utils.js
Original file line number Diff line number Diff line change
@@ -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 || {};

Expand All @@ -25,14 +25,18 @@ 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);
}

if (res.status < 200 || res.status > 299) {
const error = new Error(await res.text());
error.status = res.status;

if (auth && res.status == 401) {
logout();
}

throw error;
}

Expand Down
2 changes: 1 addition & 1 deletion http/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c3bd118

Please sign in to comment.