Closed clubside closed 2 months ago
You need to use encapsulation:
app.register(async function (app) {
app.register(fastifyPassport.initialize())
app.register(fastifyPassport.secureSession())
fastifyPassport.registerUserSerializer(
async (user, request) => {
console.log('registerUserSerializer', { user }, { json: user._json })
const userJson = user._json
const { id, displayName } = user
const userForSession = { id, displayName, email: userJson.email }
// User object sent it from Google.
// Here you want to call your DB and get the user info from there and store it in the session.
// The session is encrypted but if you dont want to store all the user info in the session just store the DB id in the session
return userForSession
}
)
fastifyPassport.registerUserDeserializer(async (userFromSession, request) => {
console.log('registerUserDeserializer', { userFromSession })
// Here you decrypt the session object and read the user info from there.
// If the whole user object is stored just return it. If only an ID is stored, look up the user in the DB using the ID and return that user
return userFromSession
})
})
app.register(fastifyStatic, {
root: path.join(__dirname, 'public')
})
@mcollina I wrapped all of my Fastify objects with in the app.register(async function (app) { })
except for app.listen()
and am still getting the same result.
As I put in the code above, you need to have fastifyStatic
registered outside.
Sorry I missed that @mcollina that fixed everything, thanks so much!
I am using Fastify Passport to handle Google logins to the site. Everything works great except for the fact that every request for a static file by a logged-in user passes through
registerUserDeserializer
.Here are some sample log lines from the server when a user is logged it. I assume the non-304 ones are the ones triggering
registerUserDeserializer
.Your Environment