2020-06-11 19:38:29 +02:00
|
|
|
import useSWR from 'swr'
|
2020-05-11 03:32:20 +02:00
|
|
|
import Link from 'next/link'
|
2020-06-11 19:38:29 +02:00
|
|
|
import { useUser } from '../utils/auth/useUser'
|
|
|
|
|
|
|
|
const fetcher = (url, token) =>
|
|
|
|
fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: new Headers({ 'Content-Type': 'application/json', token }),
|
|
|
|
credentials: 'same-origin',
|
|
|
|
}).then((res) => res.json())
|
|
|
|
|
|
|
|
const Index = () => {
|
|
|
|
const { user, logout } = useUser()
|
|
|
|
const { data, error } = useSWR(
|
|
|
|
user ? ['/api/getFood', user.token] : null,
|
|
|
|
fetcher
|
|
|
|
)
|
|
|
|
if (!user) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<p>Hi there!</p>
|
2020-05-11 03:32:20 +02:00
|
|
|
<p>
|
|
|
|
You are not signed in.{' '}
|
|
|
|
<Link href={'/auth'}>
|
|
|
|
<a>Sign in</a>
|
|
|
|
</Link>
|
|
|
|
</p>
|
2020-06-11 19:38:29 +02:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<p>You're signed in. Email: {user.email}</p>
|
|
|
|
<p
|
|
|
|
style={{
|
|
|
|
display: 'inlinelock',
|
|
|
|
color: 'blue',
|
|
|
|
textDecoration: 'underline',
|
|
|
|
cursor: 'pointer',
|
|
|
|
}}
|
|
|
|
onClick={() => logout()}
|
|
|
|
>
|
|
|
|
Log out
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-05-11 03:32:20 +02:00
|
|
|
<div>
|
|
|
|
<Link href={'/example'}>
|
|
|
|
<a>Another example page</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
2020-06-11 19:38:29 +02:00
|
|
|
{error && <div>Failed to fetch food!</div>}
|
2020-06-24 23:53:04 +02:00
|
|
|
{data ? (
|
|
|
|
<div>Your favorite food is {data.food}.</div>
|
|
|
|
) : (
|
|
|
|
<div>Loading...</div>
|
|
|
|
)}
|
2020-05-11 03:32:20 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2017-04-22 14:56:28 +02:00
|
|
|
|
2020-06-11 19:38:29 +02:00
|
|
|
export default Index
|