|
9 | 9 | ClientSideIdentityOptions, |
10 | 10 | isClientSideIdentityOptionsOrThrow, |
11 | 11 | } from './clientSideIdentityOptions'; |
12 | | -import { normalizeEmail } from './diiNormalization'; |
| 12 | +import { isNormalizedPhone, normalizeEmail } from './diiNormalization'; |
13 | 13 | import { isBase64Hash } from './hashedDii'; |
14 | 14 | import { PromiseHandler } from './promiseHandler'; |
15 | 15 | import { StorageManager } from './storageManager'; |
@@ -108,6 +108,29 @@ export abstract class SdkBase { |
108 | 108 | await this.callCstgAndSetIdentity({ emailHash: emailHash }, opts); |
109 | 109 | } |
110 | 110 |
|
| 111 | + public async setIdentityFromPhone(phone: string, opts: ClientSideIdentityOptions) { |
| 112 | + this.throwIfInitNotComplete('Cannot set identity before calling init.'); |
| 113 | + isClientSideIdentityOptionsOrThrow(opts, this._product.name); |
| 114 | + |
| 115 | + if (!isNormalizedPhone(phone)) { |
| 116 | + throw new Error('Invalid phone number'); |
| 117 | + } |
| 118 | + |
| 119 | + const phoneHash = await hashAndEncodeIdentifier(phone); |
| 120 | + await this.callCstgAndSetIdentity({ phoneHash: phoneHash }, opts); |
| 121 | + } |
| 122 | + |
| 123 | + public async setIdentityFromPhoneHash(phoneHash: string, opts: ClientSideIdentityOptions) { |
| 124 | + this.throwIfInitNotComplete('Cannot set identity before calling init.'); |
| 125 | + isClientSideIdentityOptionsOrThrow(opts, this._product.name); |
| 126 | + |
| 127 | + if (!isBase64Hash(phoneHash)) { |
| 128 | + throw new Error('Invalid hash'); |
| 129 | + } |
| 130 | + |
| 131 | + await this.callCstgAndSetIdentity({ phoneHash: phoneHash }, opts); |
| 132 | + } |
| 133 | + |
111 | 134 | public setIdentity(identity: Identity | OptoutIdentity) { |
112 | 135 | if (this._apiClient) this._apiClient.abortActiveRequests(); |
113 | 136 | const validatedIdentity = this.validateAndSetIdentity(identity); |
|
0 commit comments