mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
feat(api): ✨ Automatically register user when connecting with OIDC profile not already existing
This commit is contained in:
parent
70a669a29c
commit
99f14ba114
6 changed files with 99 additions and 26 deletions
|
|
@ -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({
|
||||
|
|
|
|||
|
|
@ -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}${
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue