Skip to content

Commit c06be58

Browse files
committed
updated with-auth example
1 parent 66b4b59 commit c06be58

4 files changed

Lines changed: 9 additions & 8 deletions

File tree

examples/with-auth/src/components/Counter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default function Counter() {
66
return (
77
<button
88
class="w-[200px] rounded-full bg-gray-100 border-2 border-gray-300 focus:border-gray-400 active:border-gray-400 px-[2rem] py-[1rem]"
9-
onClick={() => setCount(count() + 1)}
9+
onclick={() => setCount(count() + 1)}
1010
>
1111
Clicks: {count()}
1212
</button>

examples/with-auth/src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const querySession = query(async (path: string) => {
1616
const { data } = await getSession();
1717
if (path === "/login" && data.id) return redirect("/");
1818
if (data.id) return data;
19-
if (isProtectedRoute(path)) throw redirect("/login");
19+
if (isProtectedRoute(path)) throw redirect(`/login?redirect=${path}`);
2020
return null;
2121
}, "session");
2222

examples/with-auth/src/lib/server.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ export const getSession = () =>
1212
password: process.env.SESSION_SECRET!
1313
});
1414

15-
export const createSession = async ({ id, email }: Session) => {
15+
export const createSession = async (user: Session, dest?: string) => {
16+
const validDest = dest?.[0] === "/" && dest[1] !== "/";
1617
const session = await getSession();
17-
await session.update({ id, email });
18-
return redirect("/");
18+
await session.update(user);
19+
return redirect(validDest ? dest : "/");
1920
};
2021

21-
export const oauthSignIn = async (email: string) => {
22+
export const oauthSignIn = async (email: string, dest?: string) => {
2223
let user = await findUser({ email });
2324
if (!user) user = await createUser({ email });
24-
return createSession(user);
25+
return createSession(user, dest);
2526
};
2627

2728
export const passwordSignIn = async (email: string, password: string) => {

examples/with-auth/src/routes/api/oauth/[...oauth].ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const config: Configuration = {
77
id: process.env.DISCORD_ID!,
88
secret: process.env.DISCORD_SECRET!
99
},
10-
handler: async ({ email }) => oauthSignIn(email)
10+
handler: async ({ email }, redirect) => oauthSignIn(email, redirect)
1111
};
1212

1313
export const GET = OAuth(config);

0 commit comments

Comments
 (0)