refresh token on page refresh

This commit is contained in:
hay-kot 2021-03-04 21:24:48 -09:00
commit b811ec14cd
5 changed files with 26 additions and 7 deletions

View file

@ -47,7 +47,7 @@ const apiReq = {
return response; return response;
} else return; } else return;
}); });
// processResponse(response); processResponse(response);
return response; return response;
}, },

View file

@ -1,13 +1,14 @@
import { baseURL } from "./api-utils"; import { baseURL } from "./api-utils";
import { apiReq } from "./api-utils"; import { apiReq } from "./api-utils";
import axios from "axios";
const authPrefix = baseURL + "auth"; const authPrefix = baseURL + "auth";
const userPrefix = baseURL + "users"; const userPrefix = baseURL + "users";
const authURLs = { const authURLs = {
token: `${authPrefix}/token`, token: `${authPrefix}/token`,
refresh: `${authPrefix}/refresh`,
}; };
const usersURLs = { const usersURLs = {
users: `${userPrefix}`, users: `${userPrefix}`,
self: `${userPrefix}/self`, self: `${userPrefix}/self`,
@ -24,6 +25,12 @@ export default {
}); });
return response; return response;
}, },
async refresh() {
let response = await axios.get(authURLs.refresh).catch(function(event) {
console.log("Fetch failed", event);
});
return response.data ? response.data : false;
},
async allUsers() { async allUsers() {
let response = await apiReq.get(usersURLs.users); let response = await apiReq.get(usersURLs.users);
return response.data; return response.data;

View file

@ -63,6 +63,20 @@ const actions = {
} }
}, },
async refreshToken({ commit, getters }) {
if (!getters.getIsLoggedIn) {
commit("setIsLoggedIn", false); // This is to be here... for some reasons? ¯\_(ツ)_/¯
console.log("Not Logged In");
return;
}
try {
let authResponse = await api.users.refresh();
commit("setToken", authResponse.access_token);
} catch {
console.log("Failed Token Refresh, Logging Out...");
commit("setIsLoggedIn", false);
}
},
async initTheme({ dispatch, getters }) { async initTheme({ dispatch, getters }) {
//If theme is empty resetTheme //If theme is empty resetTheme

View file

@ -1,6 +1,6 @@
import { vueApp } from "../main"; import { vueApp } from "../main";
// TODO: Migrate to Mixins
const notifyHelpers = { const notifyHelpers = {
baseCSS: "notify-base", baseCSS: "notify-base",
error: "notify-error-color", error: "notify-error-color",

View file

@ -60,10 +60,8 @@ def get_long_token(
) )
@router.post("/refresh") @router.get("/refresh")
async def refresh_token( async def refresh_token(current_user: UserInDB = Depends(manager)):
current_user: UserInDB = Depends(manager),
):
""" Use a valid token to get another token""" """ Use a valid token to get another token"""
access_token = manager.create_access_token( access_token = manager.create_access_token(
data=dict(sub=current_user.email), expires=timedelta(hours=1) data=dict(sub=current_user.email), expires=timedelta(hours=1)