Hello RedwoodJS Community,
I’m having some trouble with dbAuth after generating the authentication code using the RedwoodJS CLI. Specifically, I can generate a token and return it to the user, but I’m unable to start the session from my custom signIn
resolver.
Here’s a summary of my setup and the issue:
Setup:
- I used the RedwoodJS CLI to generate the dbAuth files.
- I added a custom
signIn
resolver to myAuthentication
entity. - I’m able to validate the user and generate a token successfully.
Problem:
- I cannot properly instantiate
DbAuthHandler
to manage the session within my custom resolver. - As a result, the session is not being initiated, and the session cookie is not being set.
Code Details:
Here’s my custom signIn
resolver in api/src/services/authentications/authentications.ts
:
import { db } from 'src/lib/db'
import bcrypt from 'bcrypt'
import { generateToken } from 'src/lib/auth'
export const signIn: MutationResolvers['signIn'] = async ({ input }) => {
const { username, password } = input
const authRegistry = await db.authentication.findUnique({ where: { username }})
if (!authRegistry) {
throw new Error('Invalid username')
}
const passwordValid = await bcrypt.compare(password, authRegistry.hashedPassword)
if (!passwordValid) {
throw new Error('Invalid password')
}
const token = generateToken(authRegistry)
// Missing code to start session
return {
token,
}
}
And here’s the auth.ts
file generated by the RedwoodJS CLI (simplified for brevity):
import { DbAuthHandler } from '@redwoodjs/auth-dbauth-api'
import { db } from 'src/lib/db'
export const handler = async (event, context) => {
const authHandler = new DbAuthHandler(event, context, {
db: db,
authModelAccessor: 'authentication',
// Other configurations...
})
return await authHandler.invoke()
}
Question: How can I properly instantiate DbAuthHandler
within my signIn
resolver to handle the session initiation and set the session cookie?
I feel like I’m missing a crucial piece to tie everything together. Any guidance or examples would be greatly appreciated!
Thank you for your help!