Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save skolhustick/9d8a9c0510c884a0c538a2b70c8c8ac1 to your computer and use it in GitHub Desktop.
Save skolhustick/9d8a9c0510c884a0c538a2b70c8c8ac1 to your computer and use it in GitHub Desktop.
import { getSession, signOut, useSession } from 'next-auth/react'
import Link from 'next/link'
import React from 'react'
const Homepage = () => {
// Check if the user is authenticated from the client
const { data: session, status } = useSession()
if (status === 'loading') {
return <>Loading...</>
}
if (status === 'authenticated') {
return (
<>
Signed in as {session.user.email} <br />
<button onClick={() => signOut()}>Sign out</button>
</>
)
}
if (status === 'unauthenticated') {
return (
<>
Not signed in <br />
<Link href='/api/auth/signin'>
<a>Login</a>
</Link>
</>
)
}
}
export const getServerSideProps = async ctx => {
// Check if the user is authenticated from the server
const session = await getSession(ctx)
console.log({ session })
return {
props: {
session
}
}
}
export default Homepage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment