Skip to content

Commit 269b417

Browse files
committed
fix: implement redirect handling after authentication
1 parent 7352d21 commit 269b417

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dist"
99
],
1010
"license": "WTFPL",
11-
"version": "1.0.2",
11+
"version": "1.0.3",
1212
"type": "module",
1313
"keywords": [
1414
"auth",

src/redirect.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type NextRequest, NextResponse } from "next/server";
2+
import { redirect } from "next/navigation";
23
import { type Session, getGlobalConfig } from "./index";
34
import { cookies } from "next/headers";
45
import { ExchangeCodeForTokens } from "./lib/oauth";
@@ -58,5 +59,10 @@ export const handleRedirect = async (req: NextRequest) => {
5859
expiresIn: response.expiresIn,
5960
});
6061

62+
const redirectUri = cookieStore.get("REDIRECT_AFTER")?.value || "/";
63+
6164
cookieStore.set("AUTH_SESSION", token, { sameSite: "lax", httpOnly: true, secure: true });
65+
cookieStore.delete("REDIRECT_AFTER");
66+
67+
return redirect(redirectUri)
6268
};

src/server-actions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NextRequest, NextResponse } from "next/server";
2+
import { headers } from "next/headers";
23
import { cookies } from "next/headers";
34
import { redirect } from "next/navigation";
45
import { type Session, getGlobalConfig } from "./index";
@@ -18,9 +19,13 @@ export const getSession = async (): Promise<Session | null> => {
1819
}
1920
};
2021

21-
export const signIn = async (): Promise<NextResponse> => {
22+
export const signIn = async (redirectTo?: string): Promise<NextResponse> => {
2223
const config = getGlobalConfig();
2324
const session = await getSession();
25+
const cookieStore = await cookies();
26+
const headersList = await headers();
27+
const redirectUri = redirectTo ?? headersList.get("Referer") ?? "/";
28+
await cookieStore.set("REDIRECT_AFTER", redirectUri, { sameSite: "lax", httpOnly: true, secure: true });
2429
if (session) {
2530
return NextResponse.json({ message: "Already signed in" }, { status: 200 });
2631
}

0 commit comments

Comments
 (0)