Skip to content

Commit 5ad9570

Browse files
committed
Google authentication with NextAuth.js
1 parent c7391dc commit 5ad9570

File tree

7 files changed

+915
-15
lines changed

7 files changed

+915
-15
lines changed

.env.local.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Authenticatation
2+
NEXT_PUBLIC_GOOGLE_ID=
3+
NEXT_PUBLIC_GOOGLE_SECRET=

firebase.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import firebase from "firebase";
2+
3+
const firebaseConfig = {
4+
apiKey: "AIzaSyCyhheT-YLEn3cudCiV3TskTOEH299cl_Q",
5+
authDomain: "clone-8e1eb.firebaseapp.com",
6+
projectId: "clone-8e1eb",
7+
storageBucket: "clone-8e1eb.appspot.com",
8+
messagingSenderId: "75306579859",
9+
appId: "1:75306579859:web:884d105b9b9af9e4031a66"
10+
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"@heroicons/react": "^2.0.13",
1313
"eslint": "8.28.0",
1414
"eslint-config-next": "13.0.5",
15+
"firebase": "^9.14.0",
1516
"next": "13.0.5",
17+
"next-auth": "^4.18.3",
1618
"react": "18.2.0",
1719
"react-currency-formatter": "^1.1.0",
1820
"react-dom": "18.2.0",

src/components/Header.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import Image from 'next/image';
2-
import { MagnifyingGlassIcon, ShoppingCartIcon, Bars3Icon } from '@heroicons/react/24/outline'
2+
import { MagnifyingGlassIcon, ShoppingCartIcon, Bars3Icon } from '@heroicons/react/24/outline';
3+
import { useSession, signIn, signOut } from "next-auth/react";
34

45
function Header() {
6+
7+
// document -> https://next-auth.js.org/getting-started/client#usesession
8+
const { data: session, status } = useSession();
9+
console.log('>>> session', session);
10+
511
return (
612
<header>
713
<div className="flex items-center bg-amazon_blue p-1 flex-grow py-2">
@@ -11,9 +17,9 @@ function Header() {
1117
width={150}
1218
height={40}
1319
objectFit="contain"
14-
className="cursor-pointer"
20+
className="cursor-pointer"
1521
alt="logo"
16-
/>
22+
/>
1723
</div>
1824

1925
<div className="hidden sm:flex items-center h-10 rounded-md flex-grow cursor-pointer bg-yellow-400 hover:bg-yellow-500">
@@ -22,8 +28,8 @@ function Header() {
2228
</div>
2329

2430
<div className="text-white flex items-center text-xs space-x-6 mx-6 whitespace-nowrap">
25-
<div className="link">
26-
<p>Hello User A</p>
31+
<div className="link" onClick={signIn}>
32+
<p>{session? `Hello, ${session.user.name}` : 'Sign in'}</p>
2733
<p className="font-extrabold md:text-sm">Account & Lists</p>
2834
</div>
2935
<div className="link">

src/pages/_app.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import '../styles/globals.css'
1+
import '../styles/globals.css';
2+
import { SessionProvider } from "next-auth/react"
23

3-
function MyApp({ Component, pageProps }) {
4-
return <Component {...pageProps} />
4+
function MyApp({ Component, pageProps: { session, ...pageProps }, }) {
5+
return (
6+
<SessionProvider session={session}>
7+
<Component {...pageProps} />
8+
</SessionProvider>
9+
)
510
}
611

712
export default MyApp

src/pages/api/auth/[...nextauth].js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import NextAuth from "next-auth";
2+
import GoogleProvider from "next-auth/providers/google";
3+
4+
export const authOptions = {
5+
// Configure one or more authentication providers
6+
providers: [
7+
GoogleProvider({
8+
clientId: process.env.NEXT_PUBLIC_GOOGLE_ID,
9+
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_SECRET,
10+
}),
11+
// ...add more providers here
12+
],
13+
}
14+
15+
export default NextAuth(authOptions)

0 commit comments

Comments
 (0)