Skip to content

Commit

Permalink
fix: middleware fixes
Browse files Browse the repository at this point in the history
wiem bartus ze ci sie pewnie nie spodoba i chcialbys po prostu zrobic caly middleware w try catch i sama funkcja auth() jak w poprzednich tylko problem jest taki ze musze tak czy siak wykonac auth() zeby se uswierzytelnic za kazdym razem usera czy ma sesje a po tym doperio wykonac reszte logiki taka jak next jesli nie jest protected, wiec musialem tak zrobic
  • Loading branch information
qamarq committed Dec 9, 2024
1 parent 428a039 commit 2cdae97
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions frontend/src/lib/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ export const auth = async (tokens?: {
tokens?.secret ?? cookies.get("access_token_secret")?.value;

if (accessToken === "" || accessSecret === "") {
if (tokens) {
return null;
}
throw new Error("No access token or access secret");
}

Expand Down Expand Up @@ -132,6 +135,9 @@ export const auth = async (tokens?: {
name: "access_token_secret",
path: "/",
});
if (tokens) {
return null;
}
throw new Error(data.error);
}
const setCookieHeaders = response.headers.getSetCookie();
Expand All @@ -149,6 +155,9 @@ export const auth = async (tokens?: {
});
return data;
} catch (error) {
if (tokens) {
return null;
}
throw new Error("Failed to authenticate");
}
};
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export async function middleware(request: NextRequest) {
};

const isProtectedRoute = request.nextUrl.pathname.startsWith("/account");
const isLogged = await auth(tokens);
const user = await auth(tokens);

if (!isProtectedRoute) {
return NextResponse.next();
}

if (!isLogged) {
if (user === null) {
return NextResponse.redirect(new URL("/", request.url));
}

Expand Down

0 comments on commit 2cdae97

Please sign in to comment.