Skip to content

Commit

Permalink
Add redirect functionality after login (#76)
Browse files Browse the repository at this point in the history
Gone is the frustration of scanning a qr code to only be sent to the homepage
  • Loading branch information
harrisony authored Jun 29, 2024
1 parent fff0f43 commit 6a1ffd7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions frontend/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export default defineNuxtRouteMiddleware(async () => {
const ctx = useAuthContext();
const api = useUserApi();
const redirectTo = useState("authRedirect");

if (!ctx.isAuthorized()) {
if (window.location.pathname !== "/") {
console.debug("[middleware/auth] isAuthorized returned false, redirecting to /");
redirectTo.value = window.location.pathname;
return navigateTo("/");
}
}
Expand All @@ -15,6 +17,7 @@ export default defineNuxtRouteMiddleware(async () => {
if (error) {
if (window.location.pathname !== "/") {
console.debug("[middleware/user] user is null and fetch failed, redirecting to /");
redirectTo.value = window.location.pathname;
return navigateTo("/");
}
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
const loading = ref(false);
const loginPassword = ref("");
const redirectTo = useState("authRedirect");
async function login() {
loading.value = true;
Expand All @@ -116,7 +117,8 @@
toast.success("Logged in successfully");
navigateTo("/home");
navigateTo(redirectTo.value || "/home");
redirectTo.value = null;
loading.value = false;
}
Expand Down

0 comments on commit 6a1ffd7

Please sign in to comment.