mirror of
https://github.com/kevin-DL/commander_league_web.git
synced 2026-01-19 21:15:25 +00:00
Initial commit from Create Next App
This commit is contained in:
17
pages/api/getFood.js
Normal file
17
pages/api/getFood.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { verifyIdToken } from '../../utils/auth/firebaseAdmin'
|
||||
const favoriteFoods = ['pizza', 'burger', 'chips', 'tortilla']
|
||||
|
||||
const getFood = async (req, res) => {
|
||||
const token = req.headers.token
|
||||
|
||||
try {
|
||||
await verifyIdToken(token)
|
||||
return res.status(200).json({
|
||||
food: favoriteFoods[Math.floor(Math.random() * favoriteFoods.length)],
|
||||
})
|
||||
} catch (error) {
|
||||
return res.status(401).send('You are unauthorised')
|
||||
}
|
||||
}
|
||||
|
||||
export default getFood
|
||||
14
pages/auth.js
Normal file
14
pages/auth.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import FirebaseAuth from '../components/FirebaseAuth'
|
||||
|
||||
const Auth = () => {
|
||||
return (
|
||||
<div>
|
||||
<p>Sign in</p>
|
||||
<div>
|
||||
<FirebaseAuth />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Auth
|
||||
17
pages/example.js
Normal file
17
pages/example.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import Link from 'next/link'
|
||||
|
||||
const Example = (props) => {
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
This page is static because it does not fetch any data or include the
|
||||
authed user info.
|
||||
</p>
|
||||
<Link href={'/'}>
|
||||
<a>Home</a>
|
||||
</Link>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Example
|
||||
63
pages/index.js
Normal file
63
pages/index.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import useSWR from 'swr'
|
||||
import Link from 'next/link'
|
||||
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>
|
||||
<p>
|
||||
You are not signed in.{' '}
|
||||
<Link href={'/auth'}>
|
||||
<a>Sign in</a>
|
||||
</Link>
|
||||
</p>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div>
|
||||
<p>You're signed in. Email: {user.email}</p>
|
||||
<p
|
||||
style={{
|
||||
display: 'inline-block',
|
||||
color: 'blue',
|
||||
textDecoration: 'underline',
|
||||
cursor: 'pointer',
|
||||
}}
|
||||
onClick={() => logout()}
|
||||
>
|
||||
Log out
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<Link href={'/example'}>
|
||||
<a>Another example page</a>
|
||||
</Link>
|
||||
</div>
|
||||
{error && <div>Failed to fetch food!</div>}
|
||||
{data && !error ? (
|
||||
<div>Your favorite food is {data.food}.</div>
|
||||
) : (
|
||||
<div>Loading...</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Index
|
||||
Reference in New Issue
Block a user