@cobuildlab/auth0-utils
Version:
This is package to deal with common scenarios working with auth0 platform
203 lines (197 loc) • 7.84 kB
TypeScript
import { Response } from 'node-fetch';
import { Auth0UserUpdate } from './types';
export declare type Auth0ClientParams = {
domain: string;
clientId: string;
clienSecret: string;
};
export declare type Auth0User = {
created_at: string;
email: string;
email_verified: false;
identities: [
{
connection: string;
user_id: string;
provider: string;
isSocial: boolean;
}
];
name: string;
nickname: string;
picture: string;
updated_at: string;
user_id: string;
};
export declare type SendVerificationEmailResponse = {
type: string;
status: string;
created_at: string;
id: string;
};
declare class Auth0Client {
private domain;
private clientId;
private clienSecret;
private accessToken;
constructor(params: Auth0ClientParams);
private setupAccesToken;
/**.
* This function creates a user in the auth0 database if the users already exits it fetch it and return it.
* this method need the following scopes from the M2M acces token "create:users" & "read:users"
*
* @param email - User email.
* @param connection - Database connection on Auth0.
* @param options - options
* @param options.sendVerificationEmail - if should send a verification email.
* @returns User.
*/
createOrReturnUser(email: string, connection: string, options?: {
sendVerificationEmail: boolean;
}): Promise<Auth0User>;
/**.
*
* This function send a verification email to the .
* This method need the following scopes from the M2M acces token "update:users".
*
* If you want to change the "redirect to url" please check this doc.
* https://auth0.com/docs/brand-and-customize/email/customize-email-templates#configuring-the-redirect-to-url
*
* @param userId - The ID of the user.
* @returns Response of the email sent.
*/
sendAuth0EmailVerification(userId: string): Promise<SendVerificationEmailResponse>;
/**.
* This function creates a user in the auth0 database.
* this method need the following scopes from the M2M acces token "create:users"
*
* @param email - User email.
* @param connection - Database connection on Auth0.
* @param options - options
* @param options.sendVerificationEmail - if should send a verification email.
* @returns User.
*/
createAuth0User(email: string, connection: string, options?: {
sendVerificationEmail: boolean;
}): Promise<Auth0User>;
/**.
* This function creates a user in the auth0 database.
* This method need the following scopes from the M2M acces token "create:users"
*
* @param params - Params
* @param params.email - User email.
* @param params.password - USer Password.
* @param params.connection - Database connection on Auth0.
* @param params.options - Params options.
* @param params.options.sendVerificationEmail - if should send a verification email.
* @returns User.
*/
createAuth0UserWithEmailAndPassword(params: {
email: string;
password: string;
connection: string;
options?: {
sendVerificationEmail: boolean;
};
}): Promise<Auth0User>;
/**.
* This method returs a link for the user to reset the password.
* this method need the following scopes from the M2M acces token "create:user_tickets"
* @param params - User email.
* @param params.connectionId - the connectio id of the database. example: "con_131231231321"
* ID of the connection. If provided, allows the user to be specified using email instead of user_id. If you set this value, you must also send the email parameter. You cannot send user_id when specifying a connection_id.
* @param params.resultUrl - Url to redirect the user after the the user chage their password. example "https://myapp.com/callback/"
* @param params.email - the user email
* @param params.ttlSec - Number of seconds for which the ticket is valid before expiration. If unspecified or set to 0, this value defaults to 432000 seconds (5 days).
* @param params.clientId - ID of the client. If provided for tenants using New Universal Login experience, the user will be prompted to redirect to the default login route of the corresponding application once the ticket is used. See Configuring Default Login Routes for more details.
Conflicts with: result_url
* @param params.userId - user_id of for whom the ticket should be created.
Conflicts with: connection_id, email
* @returns User.
*/
getResetPasswordLink(params: {
connectionId?: string;
userId?: string;
email?: string;
resultUrl?: string;
ttlSec?: number;
clientId?: string;
}): Promise<{
ticket: string;
}>;
/**.
* GET /api/v2/users
* this method need the following scopes from the M2M acces token "read:users, read:user_idp_tokens"
- Specify a search criteria for users
- Sort the users to be returned
- Select the fields to be returned
- Specify the number of users to retrieve per page and the page index.
The q query parameter can be used to get users that match the specified criteria using query string syntax.
Learn more about searching for users.
See https://auth0.com/docs/users/user-search/user-search-query-syntax
*
* @param params - Params.
* @param params.page - Page of the records.
* @param params.perPage - Size of the response.
* @param params.query - Query string to filter the users.
* @returns A list of users.
*/
getUserList(params?: {
page?: number;
perPage?: number;
query?: string;
}): Promise<Auth0User[]>;
/**.
*DELETE /api/v2/users/{id}
Delete a user.
* This method need the following scopes from the M2M acces token "delete:users"
*
* @param id - Id of the user.
* @returns Success response.
*/
deleteUser(id: string): Promise<Response>;
/**.
* GET /api/v2/users-by-email
Get user by its email.
* This method need the following scopes from the M2M acces token "read:users"
*
* @param email - Email of the user.
* @returns {Auth0User} The user.
*/
getUserByEmail(email: string): Promise<Auth0User>;
/**.
*PATCH /api/v2/users/USER_ID
Change password a user.
* This method need the following scopes from the M2M acces token "read:users", "update:users" & "update:users_app_metadata"
*
* @param email - Email of the user.
* @param password - New password.
* @returns {Auth0User} The id of the user to be blocked.
*/
changePassword(email: string, password: string): Promise<Auth0User>;
/**.
* Check user password.
*
* This method requires that the m2m application have "password" as a valid grant.
*
* @param email - Email of the user.
* @param password - New password.
* @returns {boolean} Boolean If are valid password.
*/
validateUserCredentials(email: string, password: string): Promise<boolean>;
/**.
* @param id - Email of the user.
* @param info - New info.
* @returns {Auth0User} User Update Response.
*/
updateUserData(id: string, info: Auth0UserUpdate): Promise<Auth0User>;
}
/**
* @param params - Params tu create the client.
* @param params.domain - Auth0 tenant domain..
* @param params.clientId - Auth0 app client id.
* @param params.clienSecret - Auth0 app client secret.
* @returns Client with methods to interacts with Auth0.
*/
export declare function createAuth0Client(params: Auth0ClientParams): Auth0Client;
export {};