-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathdatabase.ts
More file actions
208 lines (198 loc) · 5.84 KB
/
database.ts
File metadata and controls
208 lines (198 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { InitOverride, JSONApiResponse, TextApiResponse } from "../lib/models.js";
import { validateRequiredRequestParams } from "../lib/runtime.js";
import { BaseAuthAPI } from "./base-auth-api.js";
export interface SignUpRequest {
/**
* The client_id of your client.
* Use if you want to override the class's `clientId`
*/
client_id?: string;
/**
* The user's email address.
*/
email: string;
/**
* The user's desired password.
*/
password: string;
/**
* The name of the database configured to your client.
*/
connection: string;
/**
* The user's username. Only valid if the connection requires a username.
*/
username?: string;
/**
* The user's given name(s).
*/
given_name?: string;
/**
* The user's family name(s).
*/
family_name?: string;
/**
* The user's full name.
*/
name?: string;
/**
*The user's nickname.
*/
nickname?: string;
/**
* A URI pointing to the user's picture.
*/
picture?: string;
/**
* The user metadata to be associated with the user. If set, the field must be an object containing no more than ten properties. Property names can have a maximum of 100 characters, and property values must be strings of no more than 500 characters.
*/
user_metadata?: { [key: string]: unknown };
}
export interface SignUpResponse {
/**
* Email address of the new user.
*/
email: string;
/**
* Indicates whether the email has been verified or not.
*/
email_verified: boolean;
/**
* The server can return `_id`, `id` or `user_id` depending on various factors.
* For convenience we expose it here as just `id`.
*/
id: string;
/**
* Username of this user.
*/
username?: string;
/**
* The user's given name(s).
*/
given_name?: string;
/**
* The user's family name(s).
*/
family_name?: string;
/**
* The user's full name.
*/
name?: string;
/**
*The user's nickname.
*/
nickname?: string;
/**
* A URI pointing to the user's picture.
*/
picture?: string;
/**
* The user metadata to be associated with the user. If set, the field must be an object containing no more than ten properties. Property names can have a maximum of 100 characters, and property values must be strings of no more than 500 characters.
*/
user_metadata?: { [key: string]: unknown };
}
export interface ChangePasswordRequest {
/**
* The client_id of your client.
* Use if you want to override the class's `clientId`
*/
client_id?: string;
/**
* The user's email address.
*/
email: string;
/**
* The name of the database configured to your client.
*/
connection: string;
/**
* The organization_id of the Organization associated with the user.
*/
organization?: string;
}
/**
* Sign-up and change-password for Database & Active Directory authentication services.
*/
export class Database extends BaseAuthAPI {
/**
* Given a user's credentials, and a connection, this endpoint will create a new user using active authentication.
*
* This endpoint only works for database connections.
*
* See: https://auth0.com/docs/api/authentication#signup
*
* @example
* ```js
* var data = {
* email: '{EMAIL}',
* password: '{PASSWORD}',
* connection: 'Username-Password-Authentication'
* };
*
* await auth0.database.signUp(data);
* ```
*/
async signUp(
bodyParameters: SignUpRequest,
initOverrides?: InitOverride,
): Promise<JSONApiResponse<SignUpResponse>> {
// TODO: call this `validateRequiredParams` so we can use with bodyParameters in the auth api
validateRequiredRequestParams(bodyParameters, ["email", "password", "connection"]);
const response = await this.request(
{
path: "/dbconnections/signup",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: { client_id: this.clientId, ...bodyParameters },
},
initOverrides,
);
// Transform the response to ensure id field is always available
const jsonResponse = await JSONApiResponse.fromResponse(response);
if (jsonResponse.data) {
const data = jsonResponse.data as any;
// Map _id or user_id to id
if (!data.id && (data._id || data.user_id)) {
data.id = data._id || data.user_id;
}
}
return jsonResponse as JSONApiResponse<SignUpResponse>;
}
/**
* Given a user's email address and a connection, Auth0 will send a change password email.
*
* This endpoint only works for database connections.
*
* See: https://auth0.com/docs/api/authentication#change-password
*
* @example
* ```js
* var data = {
* email: '{EMAIL}',
* connection: 'Username-Password-Authentication'
* };
*
* await auth0.database.changePassword(data);
* ```
*/
async changePassword(
bodyParameters: ChangePasswordRequest,
initOverrides?: InitOverride,
): Promise<TextApiResponse> {
validateRequiredRequestParams(bodyParameters, ["email", "connection"]);
const response = await this.request(
{
path: "/dbconnections/change_password",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: { client_id: this.clientId, ...bodyParameters },
},
initOverrides,
);
return TextApiResponse.fromResponse(response);
}
}