Skip to content

Commit

Permalink
Check API key on a page navigation (Closes #163)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadiahmed098 committed Oct 3, 2022
1 parent bcdda16 commit 2927418
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions api/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import routes from "./routes/index.js";
const app = Router();

// Setup our routes
app.get("/", (req, res, next) => { res.status(204).send(); next(); });
app.use("/account", routes.account);
app.use("/budgets", routes.budgets);
app.use("/purchase", routes.purchase);
Expand Down
21 changes: 18 additions & 3 deletions ui/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,20 @@ const router = createRouter({
});

// Make sure user is logged in before moving
router.beforeEach((to, from, next) => {
router.beforeEach(async (to, from, next) => {
const requiresAuth = to.matched.some(x => x.meta.requiresAuth);

if (!requiresAuth) { // does not need auth
next();
} else if (requiresAuth && auth_state.state.uname !== '') { // needs auth and auth set
next();
// verify API key is valid
const response = await fetch("/api/v2/");
if (response.status === 401) {
auth_state.clearAuthState();
next(`/login?returnto=${to.fullPath}`);
} else {
next();
}
} else { // check if cookie exists
let user = null;

Expand All @@ -337,7 +344,15 @@ router.beforeEach((to, from, next) => {

if (user !== null) { // found valid old login
auth_state.setAuthState(user);
next();

// verify API key is valid
const response = await fetch("/api/v2/");
if (response.status === 401) {
auth_state.clearAuthState();
next(`/login?returnto=${to.fullPath}`);
} else {
next();
}
} else { // need to login
next(`/login?returnto=${to.fullPath}`);
}
Expand Down

0 comments on commit 2927418

Please sign in to comment.