Skip to content

Commit

Permalink
Add authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
HamedBahram committed Nov 28, 2022
1 parent 58bf2b6 commit 24eb64f
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 73 deletions.
28 changes: 28 additions & 0 deletions app/LoginButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use client'

import { useSession, signIn, signOut } from 'next-auth/react'

const LoginButton = () => {
const { data: session } = useSession()
if (session) {
return (
<button
className='text-sm font-medium tracking-wider uppercase text-stone-500'
onClick={() => signOut()}
>
Sign Out
</button>
)
}

return (
<button
className='text-sm font-medium tracking-wider uppercase text-stone-500'
onClick={() => signIn()}
>
Sign In
</button>
)
}

export default LoginButton
10 changes: 8 additions & 2 deletions app/header.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import Link from 'next/link'
import LoginButton from './LoginButton'

const Header = () => {
return (
<header className='bg-stone-100 py-12'>
<nav className='center'>
<ul className='flex justify-center gap-8 text-sm font-medium tracking-wider uppercase text-stone-500'>
<nav className='center flex items-center text-sm font-medium tracking-wider uppercase text-stone-500'>
<ul className='ml-auto flex justify-center gap-8'>
<li>
<Link href='/'>Home</Link>
</li>
<li>
<Link href='/users'>Users</Link>
</li>
</ul>
<ul className='ml-auto'>
<li>
<LoginButton />
</li>
</ul>
</nav>
</header>
)
Expand Down
9 changes: 6 additions & 3 deletions app/layout.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import './globals.css'
import Footer from './footer'
import Header from './header'
import Provider from './provider'

export default function RootLayout({ children }) {
return (
<html lang='en'>
<head />
<body>
<Header />
<main>{children}</main>
<Footer />
<Provider>
<Header />
<main>{children}</main>
<Footer />
</Provider>
</body>
</html>
)
Expand Down
9 changes: 9 additions & 0 deletions app/provider.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use client'

import { SessionProvider } from 'next-auth/react'

const Provider = ({ children }) => {
return <SessionProvider>{children}</SessionProvider>
}

export default Provider
2 changes: 1 addition & 1 deletion app/users/[userId]/user.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const User = ({ user }) => {
<div>
<div className='relative h-40 w-40 rounded-full'>
<Image
src={user?.imageUrl}
src={user?.image}
alt={user?.name}
style={{ objectFit: 'cover' }}
fill
Expand Down
6 changes: 4 additions & 2 deletions lib/prisma/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()
export default prisma
const client = globalThis.prisma || new PrismaClient()
if (process.env.NODE_ENV !== 'production') globalThis.prisma = client

export default client
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const nextConfig = {
appDir: true
},
images: {
domains: ['github.com']
domains: ['github.com', 'lh3.googleusercontent.com']
}
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
},
"dependencies": {
"@heroicons/react": "^2.0.13",
"@next-auth/prisma-adapter": "^1.0.5",
"@prisma/client": "^4.6.1",
"eslint": "8.27.0",
"eslint-config-next": "13.0.2",
"next": "13.0.2",
"next": "13.0.5",
"next-auth": "^4.17.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "^4.8.4"
Expand Down
22 changes: 22 additions & 0 deletions pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import NextAuth from 'next-auth'
import prisma from '@/lib/prisma'
import GoogleProvider from 'next-auth/providers/google'
import { PrismaAdapter } from '@next-auth/prisma-adapter'

export default async function auth(req, res) {
return await NextAuth(req, res, {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
})
],
pages: {
// signIn: '/signin'
},
adapter: PrismaAdapter(prisma),
session: {
strategy: 'jwt'
}
})
}
Loading

0 comments on commit 24eb64f

Please sign in to comment.