|
| 1 | +import { Filters } from "adminforth"; |
| 2 | +import type { IAdminForth } from "adminforth"; |
| 3 | + |
| 4 | +export interface ExternalIdentityResourceOptions { |
| 5 | + resourceId: string; |
| 6 | + adminUserIdField?: string; |
| 7 | + providerField?: string; |
| 8 | + subjectField?: string; |
| 9 | + emailField?: string; |
| 10 | + phoneField?: string; |
| 11 | + fullNameField?: string; |
| 12 | + avatarUrlField?: string; |
| 13 | + externalUserIdField?: string; |
| 14 | + metaField?: string; |
| 15 | +} |
| 16 | + |
| 17 | +export interface OAuthIdentity { |
| 18 | + provider: string; |
| 19 | + subject: string; |
| 20 | + email?: string; |
| 21 | + phone?: string; |
| 22 | + meta?: Record<string, any>; |
| 23 | + fullName?: string; |
| 24 | + profilePictureUrl?: string | null; |
| 25 | + externalUserId?: string | number | null; |
| 26 | +} |
| 27 | + |
| 28 | +type OAuthAdapterDisplay = { |
| 29 | + constructor: { name: string }; |
| 30 | + getName?: () => string; |
| 31 | + getIcon: () => string; |
| 32 | +}; |
| 33 | + |
| 34 | +export class ExternalIdentityStore { |
| 35 | + private primaryKeyField: string; |
| 36 | + private adminUserIdField: string; |
| 37 | + private providerField: string; |
| 38 | + private subjectField: string; |
| 39 | + private externalUserIdField: string; |
| 40 | + |
| 41 | + constructor( |
| 42 | + private adminforth: IAdminForth, |
| 43 | + private config: ExternalIdentityResourceOptions, |
| 44 | + ) { |
| 45 | + this.primaryKeyField = this.resolvePrimaryKey(); |
| 46 | + this.adminUserIdField = config.adminUserIdField ?? 'adminUserId'; |
| 47 | + this.providerField = config.providerField ?? 'provider'; |
| 48 | + this.subjectField = config.subjectField ?? 'subject'; |
| 49 | + this.externalUserIdField = config.externalUserIdField ?? 'externalUserId'; |
| 50 | + } |
| 51 | + |
| 52 | + private resource() { |
| 53 | + return this.adminforth.resource(this.config.resourceId); |
| 54 | + } |
| 55 | + |
| 56 | + private resolvePrimaryKey() { |
| 57 | + const primaryKey = this.adminforth.config.resources |
| 58 | + .find(resource => resource.resourceId === this.config.resourceId) |
| 59 | + ?.columns.find(column => column.primaryKey)?.name; |
| 60 | + |
| 61 | + if (!primaryKey) { |
| 62 | + throw new Error(`External identity resource "${this.config.resourceId}" has no primary key`); |
| 63 | + } |
| 64 | + |
| 65 | + return primaryKey; |
| 66 | + } |
| 67 | + |
| 68 | + private identityRecord(adminUserPk: any, identity: OAuthIdentity) { |
| 69 | + const record: Record<string, any> = { |
| 70 | + [this.adminUserIdField]: adminUserPk, |
| 71 | + [this.providerField]: identity.provider, |
| 72 | + [this.subjectField]: identity.subject, |
| 73 | + }; |
| 74 | + const fields = [ |
| 75 | + [this.config.emailField, identity.email], |
| 76 | + [this.config.phoneField, identity.phone], |
| 77 | + [this.config.fullNameField, identity.fullName], |
| 78 | + [this.config.avatarUrlField, identity.profilePictureUrl], |
| 79 | + [this.externalUserIdField, identity.externalUserId], |
| 80 | + [this.config.metaField, identity.meta], |
| 81 | + ] as const; |
| 82 | + |
| 83 | + for (const [field, value] of fields) { |
| 84 | + if (field && value !== undefined) { |
| 85 | + record[field] = value; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return record; |
| 90 | + } |
| 91 | + |
| 92 | + findByIdentity(identityPayload: OAuthIdentity) { |
| 93 | + return this.resource().get([ |
| 94 | + Filters.EQ(this.providerField, identityPayload.provider), |
| 95 | + Filters.EQ(this.subjectField, identityPayload.subject), |
| 96 | + ]); |
| 97 | + } |
| 98 | + |
| 99 | + linkedAdminUserPk(identity: any) { |
| 100 | + return identity[this.adminUserIdField]; |
| 101 | + } |
| 102 | + |
| 103 | + async createOrUpdate(adminUserPk: any, identityPayload: OAuthIdentity) { |
| 104 | + const existingIdentity = await this.findByIdentity(identityPayload); |
| 105 | + const identityRecord = this.identityRecord(adminUserPk, identityPayload); |
| 106 | + |
| 107 | + if (existingIdentity) { |
| 108 | + if (existingIdentity[this.adminUserIdField] !== adminUserPk) { |
| 109 | + throw new Error('This external account is already connected to another user'); |
| 110 | + } |
| 111 | + await this.resource().update( |
| 112 | + existingIdentity[this.primaryKeyField], |
| 113 | + identityRecord, |
| 114 | + ); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + await this.resource().create(identityRecord); |
| 119 | + } |
| 120 | + |
| 121 | + async disconnect(identityId: string, adminUserPk: any) { |
| 122 | + const identity = await this.resource().get([ |
| 123 | + Filters.EQ(this.primaryKeyField, identityId), |
| 124 | + Filters.EQ(this.adminUserIdField, adminUserPk), |
| 125 | + ]); |
| 126 | + |
| 127 | + if (!identity) { |
| 128 | + return { error: 'Connected account not found' }; |
| 129 | + } |
| 130 | + |
| 131 | + await this.resource().delete(identityId); |
| 132 | + return { ok: true }; |
| 133 | + } |
| 134 | + |
| 135 | + async connectedAccounts(adminUserPk: any, adapters: OAuthAdapterDisplay[]) { |
| 136 | + const identities = await this.resource().list( |
| 137 | + Filters.EQ(this.adminUserIdField, adminUserPk) |
| 138 | + ); |
| 139 | + |
| 140 | + return { |
| 141 | + providers: adapters.map((adapter) => ({ |
| 142 | + provider: adapter.constructor.name, |
| 143 | + name: adapter.getName ? adapter.getName() : adapter.constructor.name, |
| 144 | + icon: adapter.getIcon(), |
| 145 | + connected: identities.some((identity) => identity[this.providerField] === adapter.constructor.name), |
| 146 | + })), |
| 147 | + identities: identities.map((identity) => { |
| 148 | + const adapter = adapters.find((adapter) => adapter.constructor.name === identity[this.providerField]); |
| 149 | + return { |
| 150 | + id: identity[this.primaryKeyField], |
| 151 | + provider: identity[this.providerField], |
| 152 | + providerName: adapter?.getName ? adapter.getName() : identity[this.providerField], |
| 153 | + providerIcon: adapter?.getIcon(), |
| 154 | + subject: identity[this.subjectField], |
| 155 | + email: this.config.emailField ? identity[this.config.emailField] : null, |
| 156 | + phone: this.config.phoneField ? identity[this.config.phoneField] : null, |
| 157 | + fullName: this.config.fullNameField ? identity[this.config.fullNameField] : null, |
| 158 | + avatarUrl: this.config.avatarUrlField ? identity[this.config.avatarUrlField] : null, |
| 159 | + }; |
| 160 | + }), |
| 161 | + }; |
| 162 | + } |
| 163 | +} |
0 commit comments