Add password reset wrappers (#1699). (#1757)

* Add password reset wrappers (#1699).
This commit is contained in:
Nabil Hachicha 2018-04-25 10:23:47 +01:00 committed by GitHub
parent 1c95abbff1
commit b321808f4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 1 deletions

View File

@ -10,7 +10,7 @@ X.Y.Z Release notes
* None. * None.
### Enhancements ### Enhancements
* None. * Add password reset wrappers (#1699).
### Bug fixes ### Bug fixes
* Fixed logout error due to fetch body not being stringified (#1731). * Fixed logout error due to fetch body not being stringified (#1731).

View File

@ -270,6 +270,55 @@ class User {
*/ */
static register(server, username, password, callback) {} static register(server, username, password, callback) {}
/**
* Request a password reset email to be sent to a user's email.
* This will not throw an exception, even if the email doesn't belong to a Realm Object Server user.
*
* This can only be used for users who authenticated with the 'password' provider, and passed a valid email address as a username.
*
* @param {string} server - authentication server
* @param {string} email - The email that corresponds to the user's username.
* @return {Promise<void>} A promise which is resolved when the request has been sent.
*/
static requestPasswordReset(server, email) {}
/**
* Complete the password reset flow by using the reset token sent to the user's email as a one-time authorization token to change the password.
*
* By default, Realm Object Server will send a link to the user's email that will redirect to a webpage where they can enter their new password.
* If you wish to provide a native UX, you may wish to modify the password authentication provider to use a custom URL with deep linking, so you can
* open the app, extract the token, and navigate to a view that allows to change the password within the app.
*
* @param {string} server - authentication server
* @param {string} reset_token - The token that was sent to the user's email address.
* @param {string} new_password - The user's new password.
* @return {Promise<void>} A promise which is resolved when the request has been sent.
*/
static completePasswordReset(server, reset_token, new_password) {}
/**
* Request an email confirmation email to be sent to a user's email.
* This will not throw an exception, even if the email doesn't belong to a Realm Object Server user.
*
* @param {string} server - authentication server
* @param {string} email - The email that corresponds to the user's username.
* @return {Promise<void>} A promise which is resolved when the request has been sent.
*/
static requestEmailConfirmation(server, email) {}
/**
* Complete the email confirmation flow by using the confirmation token sent to the user's email as a one-time authorization token to confirm their email.
*
* By default, Realm Object Server will send a link to the user's email that will redirect to a webpage where they can enter their new password.
* If you wish to provide a native UX, you may wish to modify the password authentication provider to use a custom URL with deep linking, so you can
* open the app, extract the token, and navigate to a view that allows to confirm the email within the app.
*
* @param {string} server - authentication server
* @param {string} confirmation_token - The token that was sent to the user's email address.
* @return {Promise<void>} A promise which is resolved when the request has been sent.
*/
static confirmEmail(server, confirmation_token) {}
/** /**
* Create an admin user for the given authentication server with an existing token * Create an admin user for the given authentication server with an existing token
* @param {string} adminToken - existing admin token * @param {string} adminToken - existing admin token

8
lib/index.d.ts vendored
View File

@ -296,6 +296,14 @@ declare namespace Realm.Sync {
static registerWithProvider(server: string, options: { provider: string, providerToken: string, userInfo: any }, callback: (error: Error | null, user: User | null) => void): void; static registerWithProvider(server: string, options: { provider: string, providerToken: string, userInfo: any }, callback: (error: Error | null, user: User | null) => void): void;
static registerWithProvider(server: string, options: { provider: string, providerToken: string, userInfo: any }): Promise<Realm.Sync.User>; static registerWithProvider(server: string, options: { provider: string, providerToken: string, userInfo: any }): Promise<Realm.Sync.User>;
static requestPasswordReset(server: string, email: string): Promise<void>;
static completePasswordReset(server:string, reset_token:string, new_password:string): Promise<void>;
static requestEmailConfirmation(server:string, email:string): Promise<void>;
static confirmEmail(server:string, confirmation_token:string): Promise<void>;
authenticate(server: string, provider: string, options: any): Promise<Realm.Sync.User>; authenticate(server: string, provider: string, options: any): Promise<Realm.Sync.User>;
logout(): void; logout(): void;
openManagementRealm(): Realm; openManagementRealm(): Realm;

View File

@ -275,6 +275,32 @@ function _authenticate(userConstructor, server, json, callback) {
} }
} }
function _updateAccount(userConstructor, server, json) {
const url = append_url(server, 'auth/password/updateAccount');
const options = {
method: 'POST',
body: JSON.stringify(json),
headers: postHeaders,
open_timeout: 5000
};
return performFetch(url, options)
.then((response) => {
const contentType = response.headers.get('Content-Type');
if (contentType.indexOf('application/json') === -1) {
return response.text().then((body) => {
throw new AuthError({
title: `Could not update user account: Realm Object Server didn't respond with valid JSON`,
body,
});
});
}
if (!response.ok) {
return response.json().then((body) => Promise.reject(new AuthError(body)));
}
});
}
const staticMethods = { const staticMethods = {
get current() { get current() {
const allUsers = this.all; const allUsers = this.all;
@ -379,6 +405,51 @@ const staticMethods = {
return _authenticate(this, server, json) return _authenticate(this, server, json)
}, },
requestPasswordReset(server, email) {
checkTypes(arguments, ['string', 'string']);
const json = {
provider_id: email,
data: { action: 'reset_password' }
};
return _updateAccount(this, server, json);
},
completePasswordReset(server, reset_token, new_password) {
checkTypes(arguments, ['string', 'string']);
const json = {
data: {
action: 'complete_reset',
token: reset_token,
new_password: new_password
}
};
return _updateAccount(this, server, json);
},
requestEmailConfirmation(server, email) {
checkTypes(arguments, ['string', 'string']);
const json = {
provider_id: email,
data: { action: 'request_email_confirmation' }
};
return _updateAccount(this, server, json);
},
confirmEmail(server, confirmation_token) {
checkTypes(arguments, ['string', 'string']);
const json = {
data: {
action: 'confirm_email',
token: confirmation_token
}
};
return _updateAccount(this, server, json);
},
_refreshAccessToken: refreshAccessToken, _refreshAccessToken: refreshAccessToken,
}; };