feat(api): Automatically register user when connecting with OIDC profile not already existing

This commit is contained in:
Jesse Wierzbinski 2024-06-13 23:05:04 -10:00
parent 70a669a29c
commit 99f14ba114
No known key found for this signature in database
6 changed files with 99 additions and 26 deletions

View file

@ -93,6 +93,7 @@ export const configValidator = z.object({
}),
oidc: z.object({
forced: z.boolean().default(false),
allow_registration: z.boolean().default(true),
providers: z
.array(
z.object({

View file

@ -136,6 +136,14 @@ export class OAuthManager {
};
}
async linkUserInDatabase(userId: string, sub: string): Promise<void> {
await db.insert(OpenIdAccounts).values({
serverId: sub,
issuerId: this.issuer.id,
userId: userId,
});
}
async linkUser(
userId: string,
// Return value of automaticOidcFlow
@ -182,11 +190,7 @@ export class OAuthManager {
}
// Link the account
await db.insert(OpenIdAccounts).values({
serverId: userInfo.sub,
issuerId: this.issuer.id,
userId: userId,
});
await this.linkUserInDatabase(userId, userInfo.sub);
return response(null, 302, {
Location: `${config.http.base_url}${

View file

@ -436,7 +436,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
header?: string;
admin?: boolean;
skipPasswordHash?: boolean;
}): Promise<User | null> {
}): Promise<User> {
const keys = await User.generateKeys();
const newUser = (
@ -472,7 +472,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
const finalUser = await User.fromId(newUser.id);
if (!finalUser) {
return null;
throw new Error("Failed to create user");
}
// Add to Meilisearch