-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
with protect and redirect components
- Loading branch information
Showing
14 changed files
with
178 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
cli/template/extras/proofkit-auth/components/auth/protect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getCurrentSession } from "@/server/auth/utils/session"; | ||
import AuthRedirect from "./redirect"; | ||
|
||
/** | ||
* This server component will protect the contents of it's children from users who aren't logged in | ||
* It will redirect to the login page if the user is not logged in, or the verify email page if the user is logged in but hasn't verified their email | ||
*/ | ||
export default async function Protect({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const { session, user } = await getCurrentSession(); | ||
if (!session) return <AuthRedirect path="/auth/login" />; | ||
if (!user.emailVerified) return <AuthRedirect path="/auth/verify-email" />; | ||
return <>{children}</>; | ||
} |
26 changes: 26 additions & 0 deletions
26
cli/template/extras/proofkit-auth/components/auth/redirect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"use client"; | ||
|
||
import { redirect } from "next/navigation"; | ||
import { Center, Loader } from "@mantine/core"; | ||
import { useEffect } from "react"; | ||
import Cookies from "js-cookie"; | ||
|
||
/** | ||
* A client-side component that redirects to the given path, but saves the current path in the redirectTo cookie. | ||
*/ | ||
export default function AuthRedirect({ path }: { path: string }) { | ||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
Cookies.set("redirectTo", window.location.pathname, { | ||
expires: 1 / 24 / 60, // 1 hour | ||
}); | ||
redirect(path); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<Center h="100vh"> | ||
<Loader /> | ||
</Center> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
cli/template/extras/proofkit-auth/server/auth/utils/redirect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { cookies } from "next/headers"; | ||
|
||
export async function getRedirectCookie() { | ||
const cookieStore = await cookies(); | ||
const redirectTo = cookieStore.get("redirectTo")?.value; | ||
cookieStore.delete("redirectTo"); | ||
return redirectTo ?? "/"; | ||
} |