Many FaunaDB authentication guides promote storing the client secret in as a cookie in order to attach it to the subsequent db queries, but how safe is it? The secret token is, practically, hundred per cent exposed in the browser's cookie storage.
Say this is the /pages/api/login.js handler under a Next.JS app:
import { Client, query as q } from 'faunadb'
import cookie from 'cookie'
const client = new Client({ secret: 'YOUR_SERVER_SECRET_KEY_DO_NOT_EXPOSE_THIS_EVER' })
const serializeFaunaCookie = secret => {
const cookieSerialized = cookie.serialize('FAUNA_SECRET_COOKIE', secret, {
sameSite: 'lax',
secure: process.env.NODE_ENV === 'production',
maxAge: 72576000,
httpOnly: true,
path: '/',
})
return cookieSerialized
}
export default async (req, res) => {
client
.query(q.Login(q.Match(q.Index('userByHandle'), req.body.handle), { password: req.body.password }))
.then(({ secret }) => {
res.setHeader('Set-Cookie', serializeFaunaCookie(secret))
res.status(200).end()
})
}
The result of this rather obvious, the domain will set a cookie like FAUNA_SECRET_COOKIE whateverrandomUpperCaseandlOWERcASEalphanum3r1calstring - which has no encryption.
Thanks!

