help prisma error

i'm a beginner with better-auth i have an error for the signup and i don't know why even though the prisma schema is well defined. the password field is filled in correctly in the form Please can you help me to set up a login and signup
No description
75 Replies
Blank
Blank5mo ago
show the auth client call
Raven
RavenOP5mo ago
No description
Blank
Blank5mo ago
the part where you call signIn
Raven
RavenOP5mo ago
for login and singup
No description
No description
Blank
Blank5mo ago
I dont see anything immediately wrong here show your prisma schema user is not supposed to have a password field it should be on account
Raven
RavenOP5mo ago
No description
Blank
Blank5mo ago
yeah your schema is wrong
Raven
RavenOP5mo ago
Schema
No description
Blank
Blank5mo ago
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}

model User {
id String @id
name String
email String
emailVerified Boolean
image String?
createdAt DateTime
updatedAt DateTime
Session Session[]
Account Account[]

twoFactorEnabled Boolean?
twoFactorSecret String?
twoFactorBackupCodes String?

@@unique([email])
@@map("user")
}

model Session {
id String @id
expiresAt DateTime
ipAddress String?
userAgent String?
userId String
users User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@map("session")
}

model Account {
id String @id
accountId String
providerId String
userId String
users User @relation(fields: [userId], references: [id], onDelete: Cascade)
accessToken String?
refreshToken String?
idToken String?
expiresAt DateTime?
password String?

@@map("account")
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}

model User {
id String @id
name String
email String
emailVerified Boolean
image String?
createdAt DateTime
updatedAt DateTime
Session Session[]
Account Account[]

twoFactorEnabled Boolean?
twoFactorSecret String?
twoFactorBackupCodes String?

@@unique([email])
@@map("user")
}

model Session {
id String @id
expiresAt DateTime
ipAddress String?
userAgent String?
userId String
users User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@map("session")
}

model Account {
id String @id
accountId String
providerId String
userId String
users User @relation(fields: [userId], references: [id], onDelete: Cascade)
accessToken String?
refreshToken String?
idToken String?
expiresAt DateTime?
password String?

@@map("account")
}
this is an example schma does your's look like this?
Raven
RavenOP5mo ago
yes, but with a password field in the User
Blank
Blank5mo ago
remove that
Raven
RavenOP5mo ago
if I delete the password field in the prisma schema as the user creates an account and login, how do I do that?
Blank
Blank5mo ago
what do you mean? a user will have a account linked to them that would hold their password or oauth info
Raven
RavenOP5mo ago
I will authenticate by email and password
Blank
Blank5mo ago
yea that would be a credential account in your account table user table doesnt need to have password
Raven
RavenOP5mo ago
so if there's no password, how does the user log in?
Blank
Blank5mo ago
the password is stored in the linked account with user its created when you sign up
Raven
RavenOP5mo ago
okay, I'll send you the repo link so you can check it out. and show me how it works
Blank
Blank5mo ago
just remove the password filed from user do prism migrate and it would work
Raven
RavenOP5mo ago
but I keep the password on the front side of the form and the data sent.
Blank
Blank5mo ago
oh god can you just do what I say and tell me if it works or not remove password filed from user table in your prisma schema then migrate your prisma db and try
Raven
RavenOP5mo ago
All right, I'll go like this and let you know
Blank
Blank5mo ago
yeah once it works, then ask whatever you want to know
Raven
RavenOP5mo ago
okay, I'm a boring junior but thanks
Raven
RavenOP5mo ago
here's the error I get when I try to signup
No description
No description
Blank
Blank5mo ago
why is it even asking for user.password, it should not even exist do this, I will take a look
Raven
RavenOP5mo ago
but in the form there is always the password field but if I remove this field the user can no longer register or log in or I'm wrong
Blank
Blank5mo ago
i think you are missing fundamental details about how auth works you take the values from form pass them into signUp.email those get send to better auth and it handles the rest
Raven
RavenOP5mo ago
i've already made a poc with next-auth but with better-auth i have the impression of not controlling authentication
Raven
RavenOP5mo ago
GitHub
GitHub - AristideDongo/poc
Contribute to AristideDongo/poc development by creating an account on GitHub.
Blank
Blank5mo ago
give me like 5 min to check
Raven
RavenOP5mo ago
no problem
Blank
Blank5mo ago
you just need to do a migration
pnpm prisma migrate dev --name fix_tables
pnpm prisma migrate reset
pnpm prisma migrate dev --name fix_tables
pnpm prisma migrate reset
then run your app and it should work
Blank
Blank5mo ago
No description
Blank
Blank5mo ago
No description
Blank
Blank5mo ago
No description
Blank
Blank5mo ago
it works before this fix your tabels make sure your schema has all these fields defined in this schmea
Raven
RavenOP5mo ago
the connection goes through but I can't get the res even though I'm in the browser's network and localstorage
Blank
Blank5mo ago
did you even read what I said I just ran did a migration and it worked on my local do it on your machine and it will work for you
Raven
RavenOP5mo ago
it works on my local computer
Blank
Blank5mo ago
ok so when it doesnt work
Raven
RavenOP5mo ago
ah okay but in my case the token is returned in the cookie
Blank
Blank5mo ago
it is also returned as a cookie
Blank
Blank5mo ago
here
No description
Raven
RavenOP5mo ago
ah okay, can you help me with the context? ?
Blank
Blank5mo ago
what context?
Raven
RavenOP5mo ago
I see thank you
Blank
Blank5mo ago
???
Raven
RavenOP5mo ago
I wanted to create a context to receive the token and be able to use the role throughout the app.
Blank
Blank5mo ago
the session token?
Raven
RavenOP5mo ago
yeah
Blank
Blank5mo ago
why would you want to be able to access that its in your cookies, its http only, you will use the auth checks on your server to verify auth status
Raven
RavenOP5mo ago
ah right,but why when I try to put the role I get an error
No description
Raven
RavenOP5mo ago
okay better-auth use middleware for token ?
Blank
Blank5mo ago
what? what do you even mean
Raven
RavenOP5mo ago
to protect routes as in next-authto protect routes as in next-auth
Blank
Blank5mo ago
in your middleware file, use auth.api.getSession if no session exists redirect
Raven
RavenOP5mo ago
i don't have middleware how to do this in better-auth
Blank
Blank5mo ago
its a nextjs feature its a very basic and universal feature, please go and learn that
Raven
RavenOP5mo ago
i know but i'm not used to backend in nextjs
Blank
Blank5mo ago
so go and learn that
Raven
RavenOP5mo ago
do you have an example of app faire in better-auth? I want to check something because the better-auth documentation is difficult to understand.
Blank
Blank5mo ago
GitHub
better-auth/demo/nextjs at main · better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
Raven
RavenOP5mo ago
I'm back please, can you tell me how to recover a user? because in the better-auth doc I don't see it
Blank
Blank5mo ago
getSession?
Raven
RavenOP5mo ago
yes, but when I do that I get nothing
Blank
Blank5mo ago
are you passing in the headers?
Raven
RavenOP5mo ago
const session = await auth.api.getSession({ headers: await headers() })
Blank
Blank5mo ago
log the headers and check if the cookie is there
Raven
RavenOP5mo ago
I found the error
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
const cookies = getSessionCookie(request);
if (!cookies) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}

export const config = {
matcher: ["/dashboard"],
};
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
const cookies = getSessionCookie(request);
if (!cookies) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}

export const config = {
matcher: ["/dashboard"],
};
it's my middleware but it doesn't work
Blank
Blank5mo ago
is your middleware running on the routes you want it to'
Raven
RavenOP5mo ago

import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
try {
const session = getSessionCookie(request);

if (!session) {
const loginUrl = new URL("/auth/login", request.url);
loginUrl.searchParams.set("redirect", request.nextUrl.pathname);
return NextResponse.redirect(loginUrl);
}

return NextResponse.next();
} catch (error) {
console.error("Erreur dans le middleware d'authentification :", error);
return NextResponse.redirect(new URL("/auth/login", request.url));
}
}

export const config = {
matcher: [
"/dashboard/:path*", // Dashboard + sous-routes
"/bookings/:path*", // Bookings + éventuelles sous-routes
"/rooms/:path*", // Détail des rooms (si protégé)
],
};

import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
try {
const session = getSessionCookie(request);

if (!session) {
const loginUrl = new URL("/auth/login", request.url);
loginUrl.searchParams.set("redirect", request.nextUrl.pathname);
return NextResponse.redirect(loginUrl);
}

return NextResponse.next();
} catch (error) {
console.error("Erreur dans le middleware d'authentification :", error);
return NextResponse.redirect(new URL("/auth/login", request.url));
}
}

export const config = {
matcher: [
"/dashboard/:path*", // Dashboard + sous-routes
"/bookings/:path*", // Bookings + éventuelles sous-routes
"/rooms/:path*", // Détail des rooms (si protégé)
],
};
now that's correct?
Blank
Blank5mo ago
it is a nextjs thing, I dont use nextjs enough to answe that
Raven
RavenOP5mo ago
ah okay

Did you find this page helpful?