18dc1f66c6
Since 9.4 release, fetch is pollyfilled by default from #12353, so the import is not needed anymore.
23 lines
595 B
JavaScript
23 lines
595 B
JavaScript
// From:
|
|
// https://github.com/zeit/next.js/blob/canary/examples/with-firebase-authentication/pages/index.js
|
|
|
|
export const setSession = user => {
|
|
// Log in.
|
|
if (user) {
|
|
return user.getIdToken().then(token => {
|
|
return fetch('/api/login', {
|
|
method: 'POST',
|
|
// eslint-disable-next-line no-undef
|
|
headers: new Headers({ 'Content-Type': 'application/json' }),
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({ token }),
|
|
})
|
|
})
|
|
}
|
|
|
|
// Log out.
|
|
return fetch('/api/logout', {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
})
|
|
}
|