nktnet
nktnet
BABetter Auth
Created by nktnet on 1/17/2025 in #help
[Solved] use google auth to access other google apis (e.g. Calendar/Drive)
Hello, Once I've authenticated with better-auth for the Google provider, how can I go about using other Google APIs? (e.g. using googleapis, the Node.JS Client). In Next-Auth/Auth.js, this is possible because we have access to the accessToken and refreshToken: - https://github.com/nextauthjs/next-auth/issues/1162#issuecomment-766331341 But in better-auth, this is all I can see for the session:
"session": {
"id": "*****************************",
"expiresAt": "2025-01-24T03:49:44.053Z",
"token": "*****************************",
"createdAt": "2025-01-17T03:49:44.053Z",
"updatedAt": "2025-01-17T03:49:44.053Z",
"ipAddress": "::ffff:127.0.0.1",
"userAgent": "*****************************",
"userId": "*****************************",
"impersonatedBy": null
},
"session": {
"id": "*****************************",
"expiresAt": "2025-01-24T03:49:44.053Z",
"token": "*****************************",
"createdAt": "2025-01-17T03:49:44.053Z",
"updatedAt": "2025-01-17T03:49:44.053Z",
"ipAddress": "::ffff:127.0.0.1",
"userAgent": "*****************************",
"userId": "*****************************",
"impersonatedBy": null
},
2 replies
BABetter Auth
Created by nktnet on 1/1/2025 in #help
Typescript - database hook type inference for additional fields
This is my current auth.ts:
export const auth = betterAuth({
user: {
additionalFields: {
role: {
fieldName: 'role',
type: 'string',
required: true,
input: false,
},
},
},
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== '/sign-up/email') {
return;
}
if (ctx.body?.registrationToken !== REGISTRATION_TOKEN) {
throw new APIError('UNAUTHORIZED', {
message: 'Incorrect registration token provided. Please contact your server admin',
});
}
}),
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const adminUserExists = await db.query.usersTable.findFirst({
where: (t, { eq }) => eq(t.role, 'ADMIN'),
});
return {
data: {
...user,
role: adminUserExists ? 'USER' : 'ADMIN',
},
};
},
},
},
},
emailAndPassword: {
enabled: true,
minPasswordLength: 4,
maxPasswordLength: 100,
autoSignIn: true,
},
database: drizzleAdapter(db, {
provider: 'pg',
schema: {
user: schema.usersTable,
session: schema.sessionsTable,
account: schema.accountsTable,
verification: schema.verificationsTable,
},
}),
});
export const auth = betterAuth({
user: {
additionalFields: {
role: {
fieldName: 'role',
type: 'string',
required: true,
input: false,
},
},
},
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== '/sign-up/email') {
return;
}
if (ctx.body?.registrationToken !== REGISTRATION_TOKEN) {
throw new APIError('UNAUTHORIZED', {
message: 'Incorrect registration token provided. Please contact your server admin',
});
}
}),
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const adminUserExists = await db.query.usersTable.findFirst({
where: (t, { eq }) => eq(t.role, 'ADMIN'),
});
return {
data: {
...user,
role: adminUserExists ? 'USER' : 'ADMIN',
},
};
},
},
},
},
emailAndPassword: {
enabled: true,
minPasswordLength: 4,
maxPasswordLength: 100,
autoSignIn: true,
},
database: drizzleAdapter(db, {
provider: 'pg',
schema: {
user: schema.usersTable,
session: schema.sessionsTable,
account: schema.accountsTable,
verification: schema.verificationsTable,
},
}),
});
In the database -> user -> create -> before hook, despite setting the "role" additional field as required, there is no issue if I were to remove the "role" field, i.e.
databaseHooks: {
user: {
create: {
before: async (user) => {
return {
data: {
...user,
}
}
}
}
databaseHooks: {
user: {
create: {
before: async (user) => {
return {
data: {
...user,
}
}
}
}
i.e. the role "type" is not picked up at all. I'm also facing a separate type issue client-side, which I've posted here: https://discord.com/channels/1288403910284935179/1323671049413333033/1323857938875420734 Am I doing something wrong or is this an issue with the type system?
2 replies
BABetter Auth
Created by nktnet on 12/31/2024 in #help
Typescript - how to infer additional, non-database fields during registration
No description
4 replies