Skip to content

Commit

Permalink
Sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
dugjason committed Mar 27, 2024
1 parent 502c39a commit f5d29c5
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 118 deletions.
11 changes: 11 additions & 0 deletions .env.example
@@ -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
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@
/.pnp
.pnp.js
.yarn/install-state.gz
pnpm-lock.yaml

# testing
/coverage
Expand All @@ -26,6 +27,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
.env
.env*.local

# vercel
Expand Down
7 changes: 7 additions & 0 deletions app/api/auth/[...nextauth]/route.ts
@@ -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 }
8 changes: 8 additions & 0 deletions app/google-signin/page.tsx
@@ -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} />;
}
10 changes: 7 additions & 3 deletions app/layout.tsx
Expand Up @@ -2,6 +2,8 @@ import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";

import { Providers } from "@/providers";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
Expand All @@ -15,8 +17,10 @@ export default function RootLayout({
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
<Providers>
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
</Providers>
);
}
12 changes: 12 additions & 0 deletions app/login-success/page.tsx
@@ -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>
);
}
110 changes: 4 additions & 106 deletions app/page.tsx
@@ -1,112 +1,10 @@
import Image from "next/image";
import { SessionInfo } from "@/components/session-info";

export default function Home() {
export default async function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
<code className="font-mono font-bold">app/page.tsx</code>
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
<a
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
By{" "}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className="dark:invert"
width={100}
height={24}
priority
/>
</a>
</div>
</div>

<div className="relative flex place-items-center before:absolute before:h-[300px] before:w-full sm:before:w-[480px] before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-full sm:after:w-[240px] after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 before:lg:h-[360px] z-[-1]">
<Image
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>

<div className="mb-32 grid text-center lg:max-w-5xl lg:w-full lg:mb-0 lg:grid-cols-4 lg:text-left">
<a
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Docs{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Find in-depth information about Next.js features and API.
</p>
</a>

<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Learn{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Learn about Next.js in an interactive course with&nbsp;quizzes!
</p>
</a>

<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Templates{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Explore starter templates for Next.js.
</p>
</a>

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Deploy{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50 text-balance`}>
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
</a>
<div className="relative flex items-center justify-center border border-black">
<SessionInfo />
</div>
</main>
);
Expand Down
40 changes: 40 additions & 0 deletions components/session-info.tsx
@@ -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>
);
}
42 changes: 42 additions & 0 deletions components/sign-in.tsx
@@ -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();
}
43 changes: 43 additions & 0 deletions lib/auth.ts
@@ -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
}
},

};
11 changes: 11 additions & 0 deletions lib/session.ts
@@ -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
}
12 changes: 7 additions & 5 deletions package.json
Expand Up @@ -9,19 +9,21 @@
"lint": "next lint"
},
"dependencies": {
"@frontapp/plugin-sdk": "1.7.0",
"next": "14.1.4",
"next-auth": "4.24.7",
"react": "^18",
"react-dom": "^18",
"next": "14.1.4"
"react-dom": "^18"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"eslint": "^8",
"eslint-config-next": "14.1.4",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"eslint": "^8",
"eslint-config-next": "14.1.4"
"typescript": "^5"
}
}

0 comments on commit f5d29c5

Please sign in to comment.