Skip to content

Instantly share code, notes, and snippets.

@malerba118
Created July 14, 2023 16:44
Show Gist options
  • Save malerba118/f86f959ca3427d5ddc1d1d7da3271df9 to your computer and use it in GitHub Desktop.
Save malerba118/f86f959ca3427d5ddc1d1d7da3271df9 to your computer and use it in GitHub Desktop.
// client
signIn().then((user) => {
axios.post('/users', user, { headers: { authorization: user.accessToken } })
})
// server
server.middleware((req, res) => {
if (req.headers.authorization) {
try {
const decoded = await firebase.verifyIdToken(req.headers.authorization)
req.uid = decoded.uid
}
catch(err) {
return res.status(401).json({ message: 'Unauthenticated' })
}
}
})
server.post('/users', async (req, res) => {
if (!req.uid) {
return res.status(401).json({ message: 'Unauthenticated' })
}
else {
// create user in mongo if needed
}
})
server.get('/books', async (req) => {
// return list regardless of auth
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment