Open dwalker93 opened 1 month ago
No session was created in the database, can anyone help solve this?
https://github.com/nextauthjs/next-auth/tree/main/packages/core/src/lib/actions/callback/index.ts
else if (provider.type === "credentials" && method === "POST") {
const credentials = body ?? {}
// TODO: Forward the original request as is, instead of reconstructing it
Object.entries(query ?? {}).forEach(([k, v]) =>
url.searchParams.set(k, v)
)
const userFromAuthorize = await provider.authorize(
credentials,
// prettier-ignore
new Request(url, { headers, method, body: JSON.stringify(body) })
)
const user = userFromAuthorize
if (!user) {
console.error(
"Read more at https://errors.authjs.dev/#credentialssignin"
)
throw new CredentialsSignin()
} else user.id = user.id?.toString() ?? crypto.randomUUID()
const account = {
providerAccountId: user.id,
type: "credentials",
provider: provider.id,
} satisfies Account
const redirect = await handleAuthorized(
{ user, account, credentials },
options
)
if (redirect) return { redirect, cookies }
const defaultToken = {
name: user.name,
email: user.email,
picture: user.image,
sub: user.id,
}
const token = await callbacks.jwt({
token: defaultToken,
user,
account,
isNewUser: false,
trigger: "signIn",
})
// Clear cookies if token is null
if (token === null) {
cookies.push(...sessionStore.clean())
} else {
const salt = options.cookies.sessionToken.name
// Encode token
const newToken = await jwt.encode({ ...jwt, token, salt })
// Set cookie expiry date
const cookieExpires = new Date()
cookieExpires.setTime(cookieExpires.getTime() + sessionMaxAge * 1000)
const sessionCookies = sessionStore.chunk(newToken, {
expires: cookieExpires,
})
cookies.push(...sessionCookies)
}
await events.signIn?.({ user, account })
return { redirect: callbackUrl, cookies }
}
Other providers include the following:
const { user, session, isNewUser } = await handleLoginOrRegister(
sessionStore.value,
userFromProvider,
account,
options
)
https://github.com/nextauthjs/next-auth/tree/main/packages/core/src/lib/actions/session.ts
When accessing “/api/auth/session”, the session could not be read,
let userAndSession = await getSessionAndUser(sessionToken)
and the following operations were performed.
response.cookies?.push(...sessionStore.clean())
@dwalker93
Adapter type
@auth/xata-adapter
Environment
Reproduction URL
https://github.com/dwalker93/xata-auth-test
Describe the issue
Xata adapater not storing session data in the xata database when using credentials provider. But working fine with the github provider as I tested.
How to reproduce
Expected behavior
You should be able to login using the credential provider and be redirected to the app and see the users session information.