Skip to content

Instantly share code, notes, and snippets.

@luandevpro
Created October 13, 2019 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luandevpro/6d63fdebd1214989b086a049119469f2 to your computer and use it in GitHub Desktop.
Save luandevpro/6d63fdebd1214989b086a049119469f2 to your computer and use it in GitHub Desktop.
import withFirebase from '../../../middlewares/withFirebase';
import cookies from '../../../middlewares/cookies';
async function handle(req, res) {
const token = req.cookies.token || req.headers.authorization.split(' ')[1];
if (token) {
req.admin
.auth()
.verifyIdToken(token)
.then((decodedIdToken) => {
if (decodedIdToken) {
const expiresIn = 60 * 60 * 24 * 7 * 1000;
req.admin
.auth()
.createSessionCookie(token, { expiresIn })
.then((sessionCookie) => {
const options = {
maxAge: expiresIn,
domain: 'localhost',
httpOnly: true,
};
res.cookie('token', sessionCookie, options);
res.json({ sessionCookie });
});
}
})
.catch(() => {
res.status(200).json({ token });
});
} else {
res.status(400).json({ message: 'Not authorized! Go back!' });
}
}
export default withFirebase(cookies(handle));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment