2017-06-30 20:18:11 +00:00
|
|
|
import React from 'react'
|
|
|
|
import cookie from 'cookie'
|
2018-05-18 08:55:12 +00:00
|
|
|
import { ApolloConsumer } from 'react-apollo'
|
2017-06-30 20:18:11 +00:00
|
|
|
|
|
|
|
import redirect from '../lib/redirect'
|
2017-11-02 16:56:14 +00:00
|
|
|
import checkLoggedIn from '../lib/checkLoggedIn'
|
2017-06-30 20:18:11 +00:00
|
|
|
|
2018-05-18 08:55:12 +00:00
|
|
|
export default class Index extends React.Component {
|
2017-06-30 20:18:11 +00:00
|
|
|
static async getInitialProps (context, apolloClient) {
|
2018-05-18 08:55:12 +00:00
|
|
|
const { loggedInUser } = await checkLoggedIn(context.apolloClient)
|
2017-06-30 20:18:11 +00:00
|
|
|
|
|
|
|
if (!loggedInUser.user) {
|
|
|
|
// If not signed in, send them somewhere more useful
|
|
|
|
redirect(context, '/signin')
|
|
|
|
}
|
|
|
|
|
|
|
|
return { loggedInUser }
|
|
|
|
}
|
|
|
|
|
2018-05-18 08:55:12 +00:00
|
|
|
signout = apolloClient => () => {
|
2017-06-30 20:18:11 +00:00
|
|
|
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.
|
2018-05-18 08:55:12 +00:00
|
|
|
apolloClient.cache.reset().then(() => {
|
2017-06-30 20:18:11 +00:00
|
|
|
// Redirect to a more useful page when signed out
|
|
|
|
redirect({}, '/signin')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
2018-05-18 08:55:12 +00:00
|
|
|
<ApolloConsumer>
|
|
|
|
{client => (
|
|
|
|
<div>
|
|
|
|
Hello {this.props.loggedInUser.user.name}!<br />
|
|
|
|
<button onClick={this.signout(client)}>Sign out</button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</ApolloConsumer>
|
2017-06-30 20:18:11 +00:00
|
|
|
)
|
|
|
|
}
|
2018-12-17 16:34:32 +00:00
|
|
|
}
|