rsnext/examples/with-firebase-authentication/utils/auth/firebaseSessionHandler.js
Willian Justen 18dc1f66c6 Remove isomorphic-unfetch from examples (#12948)
Since 9.4 release, fetch is pollyfilled by default from #12353,
so the import is not needed anymore.
2020-05-15 22:23:55 +02:00

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',
})
}