Skip to content

Commit 04b1236

Browse files
feat(hook): add new create user hook
1 parent bc32b70 commit 04b1236

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/hooks/auth0-context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
ExchangeNativeSocialOptions,
1919
RevokeOptions,
2020
ResetPasswordOptions,
21+
CreateUserOptions,
2122
} from '../types';
2223
import BaseError from '../utils/baseError';
2324

@@ -138,6 +139,12 @@ export interface Auth0ContextInterface<TUser extends User = User>
138139
*Request an email with instructions to change password of a user {@link Auth#resetPassword}
139140
*/
140141
resetPassword: (parameters: ResetPasswordOptions) => Promise<void>;
142+
143+
144+
/**
145+
*Creates a new user {@link Auth#createUser}
146+
*/
147+
createUser: (parameters: CreateUserOptions) => Promise<Partial<User>>;
141148
}
142149

143150
export interface AuthState<TUser extends User = User> {
@@ -181,6 +188,7 @@ const initialContext = {
181188
authorizeWithExchangeNativeSocial: stub,
182189
revokeRefreshToken: stub,
183190
resetPassword: stub,
191+
createUser: stub,
184192
};
185193

186194
const Auth0Context = createContext<Auth0ContextInterface>(initialContext);

src/hooks/auth0-provider.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
ExchangeNativeSocialOptions,
2525
RevokeOptions,
2626
ResetPasswordOptions,
27+
CreateUserOptions,
2728
} from '../types';
2829
import type { CustomJwtPayload } from '../internal-types';
2930
import { convertUser } from '../utils/userConversion';
@@ -390,6 +391,19 @@ const Auth0Provider = ({
390391
}
391392
}, [client]);
392393

394+
const createUser = useCallback(
395+
async (parameters: CreateUserOptions) => {
396+
try {
397+
const user = await client.auth.createUser(parameters);
398+
return user;
399+
} catch (error) {
400+
dispatch({ type: 'ERROR', error: error as BaseError });
401+
throw error;
402+
}
403+
},
404+
[client]
405+
);
406+
393407
const contextValue = useMemo(
394408
() => ({
395409
...state,
@@ -411,6 +425,7 @@ const Auth0Provider = ({
411425
authorizeWithExchangeNativeSocial,
412426
revokeRefreshToken,
413427
resetPassword,
428+
createUser,
414429
}),
415430
[
416431
state,
@@ -432,6 +447,7 @@ const Auth0Provider = ({
432447
authorizeWithExchangeNativeSocial,
433448
revokeRefreshToken,
434449
resetPassword,
450+
createUser,
435451
]
436452
);
437453

src/hooks/use-auth0.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type { Auth0ContextInterface } from './auth0-context';
3030
* authorizeWithPasswordRealm,
3131
* authorizeWithExchangeNativeSocial,
3232
* revokeRefreshToken
33+
* createUser
3334
* } = useAuth0();
3435
* ```
3536
*

0 commit comments

Comments
 (0)