34f1aefa4a
* Start from existing example * Upgrade some dependencies * Use dotenv * Remove custom server * Add serverless Firebase auth * Add TODOs * Update project name * Fix build script * Remove server middleware from client JS bundle * Add logout functionality * Redirect to auth page on logout * Remove TODO * Add comments about the cookie-session approach * Remove the sessions folder * Add comments for eslint * Remove unused files * Clarify comment * Update README.md * Rename variable for clarity * Update README.md * Change some comments * Add more to gitignore * Remove the bundle analyzer * Move server-side auth user logic from _app.js to a HOC to support static HTML rendering Co-authored-by: Joe Haddad <timer150@gmail.com>
25 lines
635 B
JavaScript
25 lines
635 B
JavaScript
// From:
|
|
// https://github.com/zeit/next.js/blob/canary/examples/with-firebase-authentication/pages/index.js
|
|
|
|
import fetch from 'isomorphic-unfetch'
|
|
|
|
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',
|
|
})
|
|
}
|