momoneko
momoneko
BABetter Auth
Created by momoneko on 5/8/2025 in #help
Create user with custom ID
I am trying to configure better-auth as an alternative to Cognito. We are trying to migrate our users over, but since Cognito doesn't allow us to access the passwords, we need to do it when our user's log in. The idea is to log in with Cognito and sign the user into the better auth database. This means that the IDs should match. Is it possible to create a user with a pre-set ID, so that the old Cognito and the new user IDs match?
7 replies
BABetter Auth
Created by momoneko on 5/6/2025 in #help
Infer type before initializing betterAuth
I'm trying to create a class that to migrate our system to betterAuth, while keeping the current system in place for the time being. I know what plugins are going to be enabled, but things like database password, appName and others will be passed in via configuration options How do I correctly define the type for the authInstance so that I have access to all the plugins? For example:
import { betterAuth, BetterAuthOptions } from "better-auth";
import { jwt } from "better-auth/plugins";


const authInstance = betterAuth({

emailAndPassword: {
enabled: true,
},
advanced: {
cookiePrefix: "pva_",
},
plugins: [jwt()],
});

export class AuthService {
authInstance: ReturnType<typeof betterAuth>;
constructor(o: BetterAuthOptions) {
this.authInstance = betterAuth({
...o,
emailAndPassword: {
enabled: true,
},
advanced: {
cookiePrefix: "pva_",
},
plugins: [jwt()],
});
}

getSession(headers: Headers) {
return this.authInstance.api.getSession({
headers: headers,
});
}
getJwt(headers: Headers) {
// this doesn't work
return this.authInstance.api.getToken({headers});
}
getInstancejwt(headers: Headers) {
// this works
return authInstance.api.getToken({headers})
}
}
import { betterAuth, BetterAuthOptions } from "better-auth";
import { jwt } from "better-auth/plugins";


const authInstance = betterAuth({

emailAndPassword: {
enabled: true,
},
advanced: {
cookiePrefix: "pva_",
},
plugins: [jwt()],
});

export class AuthService {
authInstance: ReturnType<typeof betterAuth>;
constructor(o: BetterAuthOptions) {
this.authInstance = betterAuth({
...o,
emailAndPassword: {
enabled: true,
},
advanced: {
cookiePrefix: "pva_",
},
plugins: [jwt()],
});
}

getSession(headers: Headers) {
return this.authInstance.api.getSession({
headers: headers,
});
}
getJwt(headers: Headers) {
// this doesn't work
return this.authInstance.api.getToken({headers});
}
getInstancejwt(headers: Headers) {
// this works
return authInstance.api.getToken({headers})
}
}
3 replies
BABetter Auth
Created by momoneko on 5/5/2025 in #help
What's the difference between the session and the token?
Heyo, I've been trying to get better auth to work in a Nest environment. I noticed that a call to signInEmail returnes a token, but I can't use the token to authenticate subesequent; If I register better auth as a direct handler into express, the cookie has an additional suffix. The only way I found to get the actual cookie is to add asResponse and get the cookie from the returned headers.
const result = await this.auth.api.signInEmail({
...signInRequest,
});
const rslt = await this.auth.api.signInEmail({
...signInRequest,
asResponse: true,
});
Logger.log(result, 'Sign in successful');
Logger.log(rslt, 'Sign in successful');

--------- Log output ---------
[Nest] 71710 - 05/05/2025, 3:09:59 PM LOG [Sign in successful] Object(4) {
redirect: false,
token: 'GYTdAiXcovZHov8n5WrAJSzUKJnN70Qg',
url: undefined,
user: {
id: 'lu2UF4tHqspBXEt88eWJcI81jJeI50iG',
name: '',
image: '',
emailVerified: false,
createdAt: 2025-05-05T08:50:28.790Z,
updatedAt: 2025-05-05T08:50:28.790Z
}
}
[Nest] 71710 - 05/05/2025, 3:09:59 PM LOG [Sign in successful] Response {
status: 200,
statusText: '',
headers: Headers {
'set-cookie': 'better-auth.session_token=9V35bVng9vcUOVVPrLRVdLNI1Kklkgxw.LgWuyLrRiDaoJ1oCneeKoXLj5fBHcygnL2HTlikKnQA%3D; Max-Age=604800; Path=/; HttpOnly; SameSite=Lax',
'Content-Type': 'application/json'
},
body: ReadableStream {
locked: false,
state: 'readable',
supportsBYOB: true
},
bodyUsed: false,
ok: true,
redirected: false,
type: 'default',
url: ''
}
const result = await this.auth.api.signInEmail({
...signInRequest,
});
const rslt = await this.auth.api.signInEmail({
...signInRequest,
asResponse: true,
});
Logger.log(result, 'Sign in successful');
Logger.log(rslt, 'Sign in successful');

--------- Log output ---------
[Nest] 71710 - 05/05/2025, 3:09:59 PM LOG [Sign in successful] Object(4) {
redirect: false,
token: 'GYTdAiXcovZHov8n5WrAJSzUKJnN70Qg',
url: undefined,
user: {
id: 'lu2UF4tHqspBXEt88eWJcI81jJeI50iG',
name: '',
image: '',
emailVerified: false,
createdAt: 2025-05-05T08:50:28.790Z,
updatedAt: 2025-05-05T08:50:28.790Z
}
}
[Nest] 71710 - 05/05/2025, 3:09:59 PM LOG [Sign in successful] Response {
status: 200,
statusText: '',
headers: Headers {
'set-cookie': 'better-auth.session_token=9V35bVng9vcUOVVPrLRVdLNI1Kklkgxw.LgWuyLrRiDaoJ1oCneeKoXLj5fBHcygnL2HTlikKnQA%3D; Max-Age=604800; Path=/; HttpOnly; SameSite=Lax',
'Content-Type': 'application/json'
},
body: ReadableStream {
locked: false,
state: 'readable',
supportsBYOB: true
},
bodyUsed: false,
ok: true,
redirected: false,
type: 'default',
url: ''
}
Is there a better way of getting the rest of the token? Also wondering about the difference, why give different responses.
3 replies