@@ -19,6 +19,8 @@ import { createClient, IRequestTokenResponse, MatrixClient } from 'matrix-js-sdk
19
19
20
20
import { _t } from './languageHandler' ;
21
21
22
+ const CHECK_EMAIL_VERIFIED_POLL_INTERVAL = 2000 ;
23
+
22
24
/**
23
25
* Allows a user to reset their password on a homeserver.
24
26
*
@@ -29,9 +31,9 @@ import { _t } from './languageHandler';
29
31
export default class PasswordReset {
30
32
private client : MatrixClient ;
31
33
private clientSecret : string ;
32
- private password : string ;
33
- private sessionId : string ;
34
- private logoutDevices : boolean ;
34
+ private password = "" ;
35
+ private sessionId = "" ;
36
+ private logoutDevices = false ;
35
37
36
38
/**
37
39
* Configure the endpoints for password resetting.
@@ -74,6 +76,47 @@ export default class PasswordReset {
74
76
} ) ;
75
77
}
76
78
79
+ /**
80
+ * Request a password reset token.
81
+ * This will trigger a side-effect of sending an email to the provided email address.
82
+ */
83
+ public requestResetToken ( emailAddress : string ) : Promise < IRequestTokenResponse > {
84
+ return this . client . requestPasswordEmailToken ( emailAddress , this . clientSecret , 1 ) . then ( ( res ) => {
85
+ this . sessionId = res . sid ;
86
+ return res ;
87
+ } , function ( err ) {
88
+ if ( err . errcode === 'M_THREEPID_NOT_FOUND' ) {
89
+ err . message = _t ( 'This email address was not found' ) ;
90
+ } else if ( err . httpStatus ) {
91
+ err . message = err . message + ` (Status ${ err . httpStatus } )` ;
92
+ }
93
+ throw err ;
94
+ } ) ;
95
+ }
96
+
97
+ public async setNewPassword ( password : string ) : Promise < void > {
98
+ this . password = password ;
99
+ await this . checkEmailLinkClicked ( ) ;
100
+ }
101
+
102
+ public async retrySetNewPassword ( password : string ) : Promise < void > {
103
+ this . password = password ;
104
+ return new Promise ( ( resolve ) => {
105
+ this . tryCheckEmailLinkClicked ( resolve ) ;
106
+ } ) ;
107
+ }
108
+
109
+ private tryCheckEmailLinkClicked ( resolve : Function ) : void {
110
+ this . checkEmailLinkClicked ( )
111
+ . then ( ( ) => resolve ( ) )
112
+ . catch ( ( ) => {
113
+ setTimeout (
114
+ ( ) => this . tryCheckEmailLinkClicked ( resolve ) ,
115
+ CHECK_EMAIL_VERIFIED_POLL_INTERVAL ,
116
+ ) ;
117
+ } ) ;
118
+ }
119
+
77
120
/**
78
121
* Checks if the email link has been clicked by attempting to change the password
79
122
* for the mxid linked to the email.
@@ -98,7 +141,7 @@ export default class PasswordReset {
98
141
threepid_creds : creds ,
99
142
threepidCreds : creds ,
100
143
} , this . password , this . logoutDevices ) ;
101
- } catch ( err ) {
144
+ } catch ( err : any ) {
102
145
if ( err . httpStatus === 401 ) {
103
146
err . message = _t ( 'Failed to verify email address: make sure you clicked the link in the email' ) ;
104
147
} else if ( err . httpStatus === 404 ) {
0 commit comments