Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
389 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Your base application URL | ||
NEXT_PUBLIC_FRONTEND_URL=https://example.com | ||
NEXTAUTH_URL=https://example.com | ||
|
||
# Next-auth secret. Generate a random string and set it here | ||
# See next-auth docs https://next-auth.js.org/configuration/options#secret | ||
NEXTAUTH_SECRET=your-secret-key | ||
|
||
# Google OAuth credentials | ||
AUTH_GOOGLE_ID=something-something.apps.googleusercontent.com | ||
AUTH_GOOGLE_SECRET=google-secret-key |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import NextAuth from "next-auth" | ||
|
||
import { authOptions } from "@/lib/auth" | ||
|
||
const handler = NextAuth(authOptions) | ||
|
||
export { handler as GET, handler as POST } |
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 { SignInPageClient } from "@/components/sign-in"; | ||
import { getCurrentUser } from "@/lib/session"; | ||
|
||
export default async function SignInPage() { | ||
const user = await getCurrentUser(); | ||
|
||
return <SignInPageClient user={user} />; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SessionInfo } from "@/components/session-info"; | ||
|
||
export default function LoginSuccessPage() { | ||
return ( | ||
<main className="flex min-h-screen flex-col items-center justify-between p-24"> | ||
<div className="relative flex items-center justify-center border border-black"> | ||
<h3 className="text-2xl font-bold">Login Success page</h3> | ||
<SessionInfo /> | ||
</div> | ||
</main> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use client"; | ||
|
||
import { signOut, useSession } from "next-auth/react"; | ||
|
||
export function SessionInfo() { | ||
const { data: session, status } = useSession(); | ||
|
||
const handleSignInClick = async () => { | ||
window.open( | ||
`${process.env.NEXT_PUBLIC_FRONTEND_URL}/google-signin`, | ||
"_blank", | ||
"width=500,height=800" | ||
); | ||
}; | ||
|
||
return ( | ||
<div className="flex flex-col items-center justify-center w-full h-full p-8 space-y-4 text-center"> | ||
<h1 className="text-2xl font-bold">Session Info</h1> | ||
Auth Status: {status} | ||
<code className="font-mono font-bold"> | ||
{JSON.stringify({ session }, null, 2)} | ||
</code> | ||
<br /> | ||
<div> | ||
<button | ||
className="px-4 py-2 text-white bg-blue-500 rounded-lg" | ||
onClick={handleSignInClick} | ||
> | ||
Sign in | ||
</button> | ||
<button | ||
className="px-4 py-2 text-white bg-red-500 rounded-lg" | ||
onClick={() => signOut()} | ||
> | ||
Sign out | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
} |
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,42 @@ | ||
"use client"; | ||
|
||
import { useFrontContext } from "@/providers/front"; | ||
import { signIn } from "next-auth/react"; | ||
import { useEffect } from "react"; | ||
|
||
export const SignInPageClient = ({ user }: { user: any }) => { | ||
useEffect(() => { | ||
if (!user) | ||
void signIn("google", { | ||
callbackUrl: `${process.env.NEXT_PUBLIC_FRONTEND_URL}/login-success`, | ||
}); | ||
|
||
if (user) { | ||
console.log("WE HAVE A USER", user); | ||
window.opener.postMessage( | ||
{ user }, | ||
process.env.NEXT_PUBLIC_FRONTEND_URL as string | ||
); | ||
window.close(); | ||
} | ||
}, [user]); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
width: "100vw", | ||
height: "100vh", | ||
position: "absolute", | ||
left: 0, | ||
top: 0, | ||
background: "white", | ||
}} | ||
></div> | ||
); | ||
}; | ||
|
||
export function FrontAuth() { | ||
const front = useFrontContext(); | ||
|
||
return front?.authenticate(); | ||
} |
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,43 @@ | ||
import { NextAuthOptions } from 'next-auth'; | ||
import GoogleProvider from 'next-auth/providers/google'; | ||
|
||
const cookieOptions = { | ||
httpOnly: true, | ||
partioned: true, | ||
sameSite: 'none', | ||
secure: true, | ||
path: '/', | ||
} | ||
|
||
export const authOptions: NextAuthOptions = { | ||
secret: process.env.NEXTAUTH_SECRET, | ||
|
||
// Configure one or more authentication providers | ||
providers: [ | ||
GoogleProvider({ | ||
clientId: process.env.AUTH_GOOGLE_ID as string, | ||
clientSecret: process.env.AUTH_GOOGLE_SECRET as string, | ||
}), | ||
], | ||
|
||
// Update cookie options to be secure and sameSite: 'none' and partioned | ||
cookies: { | ||
sessionToken: { | ||
name: `__Secure-next-auth.session-token`, | ||
options: cookieOptions | ||
}, | ||
callbackUrl: { | ||
name: `__Secure-next-auth-callback-url`, | ||
options: cookieOptions | ||
}, | ||
csrfToken: { | ||
name: `__Secure-next-auth.csrf-token`, | ||
options: cookieOptions | ||
}, | ||
pkceCodeVerifier: { | ||
name: `__Secure-next-auth.pkce.code_verifier`, | ||
options: cookieOptions | ||
} | ||
}, | ||
|
||
}; |
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,11 @@ | ||
'use server' | ||
|
||
import { getServerSession } from "next-auth/next" | ||
|
||
import { authOptions } from "@/lib/auth" | ||
|
||
export async function getCurrentUser() { | ||
const session = await getServerSession(authOptions) | ||
|
||
return session?.user | ||
} |
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
Oops, something went wrong.