Skip to content

Commit

Permalink
Refactor auth middleware (#1279)
Browse files Browse the repository at this point in the history
- Switch to TypeScript
- Use early return
- Switch to regular for loop

Co-authored-by: Geometrically <[email protected]>
  • Loading branch information
brawaru and Geometrically authored Jul 11, 2024
1 parent aecbc71 commit ce42502
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
35 changes: 0 additions & 35 deletions apps/frontend/src/middleware/auth.js

This file was deleted.

35 changes: 35 additions & 0 deletions apps/frontend/src/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const whitelistedParams = ["flow", "error"];

export default defineNuxtRouteMiddleware(async (_to, from) => {
const config = useRuntimeConfig();
const auth = await useAuth();

if (auth.value.user) return;

const fullPath = from.fullPath;

const url = new URL(fullPath, config.public.apiBaseUrl);

const extractedParams = Object.create(null) as Record<string, string>;

for (const param of whitelistedParams) {
const val = url.searchParams.get(param);
if (val != null) {
extractedParams[param] = val;
url.searchParams.delete(param);
}
}

const redirect = encodeURIComponent(url.pathname + url.search);

return await navigateTo(
{
path: "/auth/sign-in",
query: {
redirect,
...extractedParams,
},
},
{ replace: true },
);
});

0 comments on commit ce42502

Please sign in to comment.