forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React from 'react'
import cookie from 'cookie'
import { useApolloClient } from '@apollo/react-hooks'
import { withApollo } from '../lib/apollo'
import redirect from '../lib/redirect'
import checkLoggedIn from '../lib/checkLoggedIn'
const IndexPage = ({ loggedInUser }) => {
const apolloClient = useApolloClient()
const signout = () => {
document.cookie = cookie.serialize('token', '', {
maxAge: -1 // Expire the cookie immediately
})
// Force a reload of all the current queries now that the user is
// logged in, so we don't accidentally leave any state around.
apolloClient.cache.reset().then(() => {
// Redirect to a more useful page when signed out
redirect({}, '/signin')
})
}
return (
<div>
Hello {loggedInUser.name}!<br />
<button onClick={signout}>Sign out</button>
</div>
)
}
IndexPage.getInitialProps = async context => {
const data = await checkLoggedIn(context.apolloClient)
if (!data.me) {
// If not signed in, send them somewhere more useful
redirect(context, '/signin')
}
return { loggedInUser: data.me }
}
export default withApollo(IndexPage)