move to next-auth

This commit is contained in:
Thomas Ruoff
2021-10-02 00:35:28 +02:00
committed by Thomas Ruoff
parent 9f4180388b
commit b257bc8258
12 changed files with 536 additions and 1418 deletions

16
components/denied.tsx Normal file
View File

@@ -0,0 +1,16 @@
import { signIn } from 'next-auth/react'
export default function Denied() {
return (
<>
<h1>Du bist nicht angemeldet.</h1>
<p>
<a
onClick={(e) => {
e.preventDefault()
signIn()
}}>Melde dich an um diese Seite zu sehen.</a>
</p>
</>
)
}

View File

@@ -1,28 +1,19 @@
import { useRouter } from 'next/router'
import { useContext } from 'react'
import { useSession, signOut } from 'next-auth/react'
import UserContext from '../context/user'
import fetch from '../helpers/fetch'
export default function User() {
const router = useRouter()
const { username, role } = useContext(UserContext)
const { data, status } = useSession();
if (!username || !role) {
return <div />
}
const onClickLogout = async () => {
await fetch('/api/logout', { method: 'POST' })
router.reload()
if (status === 'loading' || !data?.user?.email) {
return null;
}
return (
<>
<div className="font-extrabold bg-red-400 px-2 py-1 mr-3 rounded-sm">
{username}
{data.user.email}
</div>
<button onClick={onClickLogout} className="btn btn-blue">
<button onClick={() => signOut()} className="btn btn-blue">
Logout
</button>
</>