@nymphjs/tilmeld-client 1.0.0-beta.10 → 1.0.0-beta.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/User.d.ts ADDED
@@ -0,0 +1,257 @@
1
+ import { Nymph, Entity } from '@nymphjs/client';
2
+ import type Group from './Group.js';
3
+ import type { AdminGroupData, CurrentGroupData } from './Group.js';
4
+ export type EventType = 'register' | 'login' | 'logout';
5
+ export type RegisterCallback = (user: User & CurrentUserData) => void;
6
+ export type LoginCallback = (user: User & CurrentUserData) => void;
7
+ export type LogoutCallback = (user: User & UserData) => void;
8
+ export type ClientConfig = {
9
+ regFields: string[];
10
+ userFields: string[];
11
+ emailUsernames: boolean;
12
+ allowRegistration: boolean;
13
+ allowUsernameChange: boolean;
14
+ pwRecovery: boolean;
15
+ verifyEmail: boolean;
16
+ unverifiedAccess: boolean;
17
+ };
18
+ export type UserData = {
19
+ /**
20
+ * The user's username.
21
+ */
22
+ username?: string;
23
+ /**
24
+ * The user's first name.
25
+ */
26
+ nameFirst?: string;
27
+ /**
28
+ * The user's middle name.
29
+ */
30
+ nameMiddle?: string;
31
+ /**
32
+ * The user's last name.
33
+ */
34
+ nameLast?: string;
35
+ /**
36
+ * The user's full name.
37
+ */
38
+ name?: string;
39
+ /**
40
+ * The user's avatar URL. (Use $getAvatar() to support Gravatar.)
41
+ */
42
+ avatar?: string;
43
+ /**
44
+ * Whether the user can log in.
45
+ */
46
+ enabled?: boolean;
47
+ };
48
+ export type CurrentUserData = UserData & {
49
+ /**
50
+ * The abilities granted to the user.
51
+ */
52
+ abilities?: string[];
53
+ /**
54
+ * The user's email address.
55
+ */
56
+ email?: string;
57
+ /**
58
+ * The user's telephone number.
59
+ */
60
+ phone?: string;
61
+ /**
62
+ * The user's primary group.
63
+ */
64
+ group?: Group & CurrentGroupData;
65
+ /**
66
+ * The user's secondary groups.
67
+ */
68
+ groups?: (Group & CurrentGroupData)[];
69
+ /**
70
+ * Whether the user should inherit the abilities of his groups.
71
+ */
72
+ inheritAbilities?: boolean;
73
+ /**
74
+ * If the user has changed their email address, this is the new one, awaiting
75
+ * verification.
76
+ */
77
+ newEmailAddress?: string;
78
+ };
79
+ export type AdminUserData = CurrentUserData & {
80
+ /**
81
+ * The user's primary group.
82
+ */
83
+ group?: Group & AdminGroupData;
84
+ /**
85
+ * The user's secondary groups.
86
+ */
87
+ groups?: (Group & AdminGroupData)[];
88
+ /**
89
+ * A verification secret.
90
+ */
91
+ secret?: string;
92
+ /**
93
+ * The timestamp of when the email address was last changed.
94
+ */
95
+ emailChangeDate?: number;
96
+ /**
97
+ * An email change proceed secret.
98
+ */
99
+ newEmailSecret?: string;
100
+ /**
101
+ * The new email address.
102
+ */
103
+ newEmailAddress?: string;
104
+ /**
105
+ * An email change cancellation secret.
106
+ */
107
+ cancelEmailSecret?: string;
108
+ /**
109
+ * The old email address.
110
+ */
111
+ cancelEmailAddress?: string;
112
+ /**
113
+ * A recovery secret.
114
+ */
115
+ recoverSecret?: string;
116
+ /**
117
+ * The timestamp of when the recovery secret was issued.
118
+ */
119
+ recoverSecretDate?: number;
120
+ /**
121
+ * Used by admins to change a user's password. Not saved to the database.
122
+ */
123
+ passwordTemp?: string;
124
+ /**
125
+ * If set, this timestamp is the cutoff point for JWT issue dates. Any token
126
+ * issued before this date will not authenticate the user.
127
+ */
128
+ revokeTokenDate?: number;
129
+ };
130
+ type InstanceStore = {
131
+ registerCallbacks: RegisterCallback[];
132
+ loginCallbacks: LoginCallback[];
133
+ logoutCallbacks: LogoutCallback[];
134
+ clientConfig?: ClientConfig;
135
+ clientConfigPromise?: Promise<ClientConfig>;
136
+ removeNymphResponseListener?: () => void;
137
+ currentToken?: string;
138
+ };
139
+ export default class User extends Entity<UserData> {
140
+ protected static stores: WeakMap<Nymph, InstanceStore>;
141
+ static class: string;
142
+ static init(nymph: Nymph): void;
143
+ static factoryUsername(username?: string): Promise<User & UserData>;
144
+ static getDomainUsers(domain: string, options?: {
145
+ limit?: number;
146
+ offset?: number;
147
+ sort?: string;
148
+ reverse?: boolean;
149
+ }): Promise<(User & UserData)[]>;
150
+ constructor();
151
+ $checkUsername(): Promise<{
152
+ result: boolean;
153
+ message: string;
154
+ }>;
155
+ $checkEmail(): Promise<{
156
+ result: boolean;
157
+ message: string;
158
+ }>;
159
+ $checkPhone(): Promise<{
160
+ result: boolean;
161
+ message: string;
162
+ }>;
163
+ $getAvatar(): Promise<string>;
164
+ $register(data: {
165
+ password: string;
166
+ additionalData?: {
167
+ [k: string]: any;
168
+ };
169
+ }): Promise<{
170
+ result: boolean;
171
+ loggedin: boolean;
172
+ message: string;
173
+ }>;
174
+ $switchUser(data?: {
175
+ additionalData?: {
176
+ [k: string]: any;
177
+ };
178
+ }): Promise<{
179
+ result: boolean;
180
+ message: string;
181
+ }>;
182
+ $logout(): Promise<{
183
+ result: boolean;
184
+ message: string;
185
+ }>;
186
+ $gatekeeper(ability?: string): Promise<boolean>;
187
+ $changePassword(data: {
188
+ newPassword: string;
189
+ currentPassword: string;
190
+ revokeCurrentTokens?: boolean;
191
+ }): Promise<{
192
+ result: boolean;
193
+ message: string;
194
+ }>;
195
+ $revokeCurrentTokens(data: {
196
+ password: string;
197
+ }): Promise<{
198
+ result: boolean;
199
+ message: string;
200
+ }>;
201
+ $hasTOTPSecret(): Promise<boolean>;
202
+ $getNewTOTPSecret(): Promise<{
203
+ uri: string;
204
+ qrcode: string;
205
+ secret: string;
206
+ }>;
207
+ $saveTOTPSecret(data: {
208
+ password: string;
209
+ secret: string;
210
+ code: string;
211
+ }): Promise<{
212
+ result: boolean;
213
+ message: string;
214
+ }>;
215
+ $removeTOTPSecret(data?: {
216
+ password: string;
217
+ code: string;
218
+ }): Promise<{
219
+ result: boolean;
220
+ message: string;
221
+ }>;
222
+ static current(returnObjectIfNotExist: true): Promise<User & CurrentUserData>;
223
+ static current(returnObjectIfNotExist?: false): Promise<(User & CurrentUserData) | null>;
224
+ static loginUser(data: {
225
+ username: string;
226
+ password: string;
227
+ code?: string;
228
+ additionalData?: {
229
+ [k: string]: any;
230
+ };
231
+ }): Promise<{
232
+ result: boolean;
233
+ message: string;
234
+ needTOTP?: true;
235
+ user?: User & CurrentUserData;
236
+ }>;
237
+ static sendRecovery(data: {
238
+ recoveryType: 'username' | 'password';
239
+ account: string;
240
+ }): Promise<{
241
+ result: boolean;
242
+ message: string;
243
+ }>;
244
+ static recover(data: {
245
+ username: string;
246
+ secret: string;
247
+ password: string;
248
+ }): Promise<{
249
+ result: boolean;
250
+ message: string;
251
+ }>;
252
+ static getClientConfig(): Promise<ClientConfig>;
253
+ private static handleToken;
254
+ static on<T extends EventType>(event: T, callback: T extends 'register' ? RegisterCallback : T extends 'login' ? LoginCallback : T extends 'logout' ? LogoutCallback : never): () => boolean;
255
+ static off<T extends EventType>(event: T, callback: T extends 'register' ? RegisterCallback : T extends 'login' ? LoginCallback : T extends 'logout' ? LogoutCallback : never): boolean;
256
+ }
257
+ export {};
package/dist/User.js ADDED
@@ -0,0 +1,263 @@
1
+ import { Entity } from '@nymphjs/client';
2
+ export default class User extends Entity {
3
+ static stores = new WeakMap();
4
+ // The name of the server class
5
+ static class = 'User';
6
+ static init(nymph) {
7
+ let store = {
8
+ registerCallbacks: [],
9
+ loginCallbacks: [],
10
+ logoutCallbacks: [],
11
+ };
12
+ if (User.stores.has(nymph)) {
13
+ const storeVal = User.stores.get(nymph);
14
+ if (storeVal) {
15
+ store = storeVal;
16
+ }
17
+ }
18
+ User.stores.set(nymph, store);
19
+ if (store.removeNymphResponseListener) {
20
+ store.removeNymphResponseListener();
21
+ }
22
+ store.removeNymphResponseListener = nymph.on('response', (response) => this.handleToken(response));
23
+ this.handleToken();
24
+ }
25
+ static async factoryUsername(username) {
26
+ const entity = new this();
27
+ if (username != null) {
28
+ const entity = await this.nymph.getEntity({
29
+ class: this,
30
+ }, {
31
+ type: '&',
32
+ ilike: ['username', username.replace(/([\\%_])/g, (s) => `\\${s}`)],
33
+ });
34
+ if (entity != null) {
35
+ return entity;
36
+ }
37
+ }
38
+ return entity;
39
+ }
40
+ static async getDomainUsers(domain, options) {
41
+ return await this.serverCallStatic('getDomainUsers', [domain, options]);
42
+ }
43
+ constructor() {
44
+ super();
45
+ this.$data.enabled = true;
46
+ this.$data.abilities = [];
47
+ this.$data.groups = [];
48
+ this.$data.inheritAbilities = true;
49
+ }
50
+ async $checkUsername() {
51
+ return await this.$serverCall('$checkUsername', [], true);
52
+ }
53
+ async $checkEmail() {
54
+ return await this.$serverCall('$checkEmail', [], true);
55
+ }
56
+ async $checkPhone() {
57
+ return await this.$serverCall('$checkPhone', [], true);
58
+ }
59
+ async $getAvatar() {
60
+ return await this.$serverCall('$getAvatar', [], true);
61
+ }
62
+ async $register(data) {
63
+ const store = User.stores.get(this.$nymph);
64
+ if (store == null) {
65
+ throw new Error('This user class was never initialized with an instance of Nymph');
66
+ }
67
+ const response = await this.$serverCall('$register', [data]);
68
+ if (response.result) {
69
+ for (let i = 0; i < store.registerCallbacks.length; i++) {
70
+ store.registerCallbacks[i] && store.registerCallbacks[i](this);
71
+ }
72
+ }
73
+ if (response.loggedin) {
74
+ this.constructor.handleToken();
75
+ for (let i = 0; i < store.loginCallbacks.length; i++) {
76
+ store.loginCallbacks[i] && store.loginCallbacks[i](this);
77
+ }
78
+ }
79
+ return response;
80
+ }
81
+ async $switchUser(data) {
82
+ const store = User.stores.get(this.$nymph);
83
+ if (store == null) {
84
+ throw new Error('This user class was never initialized with an instance of Nymph');
85
+ }
86
+ const response = await this.$serverCall('$switchUser', [data]);
87
+ if (response.result) {
88
+ this.constructor.handleToken();
89
+ for (let i = 0; i < store.loginCallbacks.length; i++) {
90
+ store.loginCallbacks[i] && store.loginCallbacks[i](this);
91
+ }
92
+ }
93
+ return response;
94
+ }
95
+ async $logout() {
96
+ const store = User.stores.get(this.$nymph);
97
+ if (store == null) {
98
+ throw new Error('This user class was never initialized with an instance of Nymph');
99
+ }
100
+ const response = await this.$serverCall('$logout', []);
101
+ if (response.result) {
102
+ this.constructor.handleToken();
103
+ for (let i = 0; i < store.logoutCallbacks.length; i++) {
104
+ store.logoutCallbacks[i] && store.logoutCallbacks[i](this);
105
+ }
106
+ }
107
+ return response;
108
+ }
109
+ async $gatekeeper(ability) {
110
+ return await this.$serverCall('$gatekeeper', [ability], true);
111
+ }
112
+ async $changePassword(data) {
113
+ return await this.$serverCall('$changePassword', [data]);
114
+ }
115
+ async $revokeCurrentTokens(data) {
116
+ return await this.$serverCall('$revokeCurrentTokens', [data]);
117
+ }
118
+ async $hasTOTPSecret() {
119
+ return await this.$serverCall('$hasTOTPSecret', [], true);
120
+ }
121
+ async $getNewTOTPSecret() {
122
+ return await this.$serverCall('$getNewTOTPSecret', [], true);
123
+ }
124
+ async $saveTOTPSecret(data) {
125
+ return await this.$serverCall('$saveTOTPSecret', [data]);
126
+ }
127
+ async $removeTOTPSecret(data) {
128
+ return await this.$serverCall('$removeTOTPSecret', [
129
+ ...(data ? [data] : []),
130
+ ]);
131
+ }
132
+ static async current(returnObjectIfNotExist) {
133
+ const currentUser = await this.serverCallStatic('current', [false]);
134
+ if (currentUser == null) {
135
+ return returnObjectIfNotExist ? this.factorySync() : null;
136
+ }
137
+ return currentUser;
138
+ }
139
+ static async loginUser(data) {
140
+ const store = User.stores.get(this.nymph);
141
+ if (store == null) {
142
+ throw new Error('This user class was never initialized with an instance of Nymph');
143
+ }
144
+ const response = await this.serverCallStatic('loginUser', [data]);
145
+ if (response.result) {
146
+ this.handleToken();
147
+ for (let i = 0; i < store.loginCallbacks.length; i++) {
148
+ store.loginCallbacks[i] && store.loginCallbacks[i](response.user);
149
+ }
150
+ }
151
+ return response;
152
+ }
153
+ static async sendRecovery(data) {
154
+ return await this.serverCallStatic('sendRecovery', [data]);
155
+ }
156
+ static async recover(data) {
157
+ return await this.serverCallStatic('recover', [data]);
158
+ }
159
+ static async getClientConfig() {
160
+ const store = User.stores.get(this.nymph);
161
+ if (store == null) {
162
+ throw new Error('This user class was never initialized with an instance of Nymph');
163
+ }
164
+ if (store.clientConfig) {
165
+ return store.clientConfig;
166
+ }
167
+ if (!store.clientConfigPromise) {
168
+ store.clientConfigPromise = this.serverCallStatic('getClientConfig', []).then((config) => {
169
+ store.clientConfig = config;
170
+ store.clientConfigPromise = undefined;
171
+ return config;
172
+ });
173
+ }
174
+ return await store.clientConfigPromise;
175
+ }
176
+ static handleToken(response) {
177
+ const store = User.stores.get(this.nymph);
178
+ if (store == null) {
179
+ throw new Error('This user class was never initialized with an instance of Nymph');
180
+ }
181
+ let token = null;
182
+ let switchToken = null;
183
+ let hasNoCookie = typeof document === 'undefined' || typeof document.cookie === 'undefined';
184
+ const authCookiePattern = /(?:(?:^|.*;\s*)TILMELDAUTH\s*=\s*([^;]*).*$)|^.*$/;
185
+ const switchCookiePattern = /(?:(?:^|.*;\s*)TILMELDSWITCH\s*=\s*([^;]*).*$)|^.*$/;
186
+ if (response && response.headers.has('X-TILMELDAUTH')) {
187
+ token = response.headers.get('X-TILMELDAUTH');
188
+ switchToken = response.headers.get('X-TILMELDSWITCH');
189
+ }
190
+ else if (typeof document !== 'undefined' &&
191
+ document.cookie.match(authCookiePattern)) {
192
+ token = document.cookie.replace(authCookiePattern, '$1');
193
+ switchToken = document.cookie.replace(switchCookiePattern, '$1');
194
+ }
195
+ else {
196
+ return;
197
+ }
198
+ if (store.currentToken != token) {
199
+ if (token == null || token === '') {
200
+ if (store.currentToken != null) {
201
+ delete this.nymph.headers['X-Xsrf-Token'];
202
+ delete this.nymph.headers['X-TILMELDAUTH'];
203
+ delete this.nymph.headers['X-TILMELDSWITCH'];
204
+ if (this.nymph.pubsub) {
205
+ this.nymph.pubsub.authenticate(null);
206
+ }
207
+ delete store.currentToken;
208
+ }
209
+ }
210
+ else {
211
+ const base64Url = token.split('.')[1];
212
+ const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
213
+ const json = typeof atob === 'undefined'
214
+ ? Buffer.from(base64, 'base64').toString('binary') // node
215
+ : atob(base64); // browser
216
+ const jwt = JSON.parse(json);
217
+ this.nymph.headers['X-Xsrf-Token'] = jwt.xsrfToken;
218
+ if (hasNoCookie) {
219
+ this.nymph.headers['X-TILMELDAUTH'] = token;
220
+ delete this.nymph.headers['X-TILMELDSWITCH'];
221
+ if (switchToken != null) {
222
+ this.nymph.headers['X-TILMELDSWITCH'] = switchToken;
223
+ }
224
+ }
225
+ if (this.nymph.pubsub) {
226
+ this.nymph.pubsub.authenticate(token, switchToken);
227
+ }
228
+ store.currentToken = token;
229
+ }
230
+ }
231
+ }
232
+ static on(event, callback) {
233
+ const store = User.stores.get(this.nymph);
234
+ if (store == null) {
235
+ throw new Error('This user class was never initialized with an instance of Nymph');
236
+ }
237
+ const prop = (event + 'Callbacks');
238
+ if (!(prop in store)) {
239
+ throw new Error('Invalid event type.');
240
+ }
241
+ // @ts-ignore: The callback should always be the right type here.
242
+ store[prop].push(callback);
243
+ return () => this.off(event, callback);
244
+ }
245
+ static off(event, callback) {
246
+ const store = User.stores.get(this.nymph);
247
+ if (store == null) {
248
+ throw new Error('This user class was never initialized with an instance of Nymph');
249
+ }
250
+ const prop = (event + 'Callbacks');
251
+ if (!(prop in store)) {
252
+ return false;
253
+ }
254
+ // @ts-ignore: The callback should always be the right type here.
255
+ const i = store[prop].indexOf(callback);
256
+ if (i > -1) {
257
+ // @ts-ignore: The callback should always be the right type here.
258
+ store[prop].splice(i, 1);
259
+ }
260
+ return true;
261
+ }
262
+ }
263
+ //# sourceMappingURL=User.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"User.js","sourceRoot":"","sources":["../src/User.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAmJhD,MAAM,CAAC,OAAO,OAAO,IAAK,SAAQ,MAAgB;IACtC,MAAM,CAAC,MAAM,GAAG,IAAI,OAAO,EAAwB,CAAC;IAE9D,+BAA+B;IACxB,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC;IAEtB,MAAM,CAAC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KAAQ,EACR,QAMa;QAEb,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC1C,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CACb,iEAAiE,CAClE,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,GAAG,CAAC,KAAK,GAAG,WAAW,CAMpB,CAAC;QACd,IAAI,CAAC,CAAC,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;YACrB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,iEAAiE;QACjE,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACxC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YACX,iEAAiE;YACjE,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC3B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC"}
@@ -0,0 +1,58 @@
1
+ import type UserClass from './User.js';
2
+ import type { ClientConfig, CurrentUserData } from './User.js';
3
+ export type RegistrationDetails = {
4
+ /**
5
+ * New user's username.
6
+ */
7
+ username: string;
8
+ /**
9
+ * New user's password.
10
+ */
11
+ password: string;
12
+ /**
13
+ * Repeat the password.
14
+ */
15
+ password2: string;
16
+ /**
17
+ * Whether the username passed verification.
18
+ */
19
+ usernameVerified: boolean;
20
+ /**
21
+ * The new user's email address.
22
+ */
23
+ email?: string;
24
+ /**
25
+ * The new user's given name.
26
+ */
27
+ nameFirst?: string;
28
+ /**
29
+ * The new user's surname.
30
+ */
31
+ nameLast?: string;
32
+ /**
33
+ * The new user's phone number.
34
+ */
35
+ phone?: string;
36
+ /**
37
+ * Additional data to be included in the request.
38
+ */
39
+ additionalData?: {
40
+ [k: string]: any;
41
+ };
42
+ };
43
+ export declare class NeedTOTPError extends Error {
44
+ }
45
+ export declare function login(User: typeof UserClass, username: string, password: string, code?: string, additionalData?: {
46
+ [k: string]: any;
47
+ }): Promise<{
48
+ message: string;
49
+ user?: UserClass & CurrentUserData;
50
+ }>;
51
+ export declare function register(User: typeof UserClass, userDetails: RegistrationDetails, clientConfig?: ClientConfig): Promise<{
52
+ loggedin: boolean;
53
+ message: string;
54
+ user?: UserClass & CurrentUserData;
55
+ }>;
56
+ export declare function checkUsername(User: typeof UserClass, username: string, clientConfig?: ClientConfig): Promise<{
57
+ message: string;
58
+ }>;
@@ -0,0 +1,98 @@
1
+ export class NeedTOTPError extends Error {
2
+ }
3
+ export async function login(User, username, password, code, additionalData) {
4
+ if (username === '') {
5
+ throw new Error('You need to enter a username.');
6
+ }
7
+ if (password === '') {
8
+ throw new Error('You need to enter a password');
9
+ }
10
+ try {
11
+ const { result, needTOTP, ...response } = await User.loginUser({
12
+ username,
13
+ password,
14
+ code,
15
+ ...(additionalData ? { additionalData } : {}),
16
+ });
17
+ if (!result) {
18
+ if (needTOTP) {
19
+ throw new NeedTOTPError(response.message);
20
+ }
21
+ throw new Error(response.message);
22
+ }
23
+ return response;
24
+ }
25
+ catch (e) {
26
+ if (e instanceof NeedTOTPError) {
27
+ throw e;
28
+ }
29
+ throw new Error(e?.message ?? 'An error occurred.');
30
+ }
31
+ }
32
+ export async function register(User, userDetails, clientConfig) {
33
+ if (userDetails.username === '') {
34
+ throw new Error('You need to enter a username.');
35
+ }
36
+ if (!userDetails.usernameVerified) {
37
+ throw new Error('The username you entered is not valid.');
38
+ }
39
+ if (userDetails.password !== userDetails.password2) {
40
+ throw new Error('Your passwords do not match.');
41
+ }
42
+ if (userDetails.password === '') {
43
+ throw new Error('You need to enter a password');
44
+ }
45
+ // Create a new user.
46
+ let user = User.factorySync();
47
+ user.username = userDetails.username;
48
+ const config = clientConfig || (await User.getClientConfig());
49
+ if (config.regFields.includes('email')) {
50
+ if (config.emailUsernames) {
51
+ user.email = userDetails.username;
52
+ }
53
+ else {
54
+ user.email = userDetails.email;
55
+ }
56
+ }
57
+ if (config.regFields.includes('name')) {
58
+ user.nameFirst = userDetails.nameFirst;
59
+ user.nameLast = userDetails.nameLast;
60
+ }
61
+ if (config.regFields.includes('phone')) {
62
+ user.phone = userDetails.phone;
63
+ }
64
+ try {
65
+ const { result, ...response } = await user.$register({
66
+ password: userDetails.password,
67
+ ...(userDetails.additionalData
68
+ ? { additionalData: userDetails.additionalData }
69
+ : {}),
70
+ });
71
+ if (!result) {
72
+ throw new Error(response.message);
73
+ }
74
+ return { ...response, user };
75
+ }
76
+ catch (e) {
77
+ throw new Error(e?.message ?? 'An error occurred.');
78
+ }
79
+ }
80
+ export async function checkUsername(User, username, clientConfig) {
81
+ let user = User.factorySync();
82
+ user.username = username;
83
+ const config = clientConfig || (await User.getClientConfig());
84
+ if (config.emailUsernames) {
85
+ user.email = username;
86
+ }
87
+ try {
88
+ const { result, ...response } = await user.$checkUsername();
89
+ if (!result) {
90
+ throw new Error(response.message);
91
+ }
92
+ return response;
93
+ }
94
+ catch (e) {
95
+ throw new Error(e?.message ?? 'An error occurred.');
96
+ }
97
+ }
98
+ //# sourceMappingURL=helpers.js.map