@@ -19,75 +19,112 @@ limitations under the License.
1919*/
2020
2121import Matrix from "matrix-js-sdk" ;
22+ import { MatrixClient } from "matrix-js-sdk/src/client" ;
23+ import { IMatrixClientCreds } from "./MatrixClientPeg" ;
24+
25+ interface ILoginOptions {
26+ defaultDeviceDisplayName ?: string ;
27+ }
28+
29+ // TODO: Move this to JS SDK
30+ interface ILoginFlow {
31+ type : string ;
32+ }
33+
34+ // TODO: Move this to JS SDK
35+ /* eslint-disable camelcase */
36+ interface ILoginParams {
37+ identifier ?: string ;
38+ password ?: string ;
39+ token ?: string ;
40+ device_id ?: string ;
41+ initial_device_display_name ?: string ;
42+ }
43+ /* eslint-enable camelcase */
2244
2345export default class Login {
24- constructor ( hsUrl , isUrl , fallbackHsUrl , opts ) {
25- this . _hsUrl = hsUrl ;
26- this . _isUrl = isUrl ;
27- this . _fallbackHsUrl = fallbackHsUrl ;
28- this . _currentFlowIndex = 0 ;
29- this . _flows = [ ] ;
30- this . _defaultDeviceDisplayName = opts . defaultDeviceDisplayName ;
31- this . _tempClient = null ; // memoize
46+ private hsUrl : string ;
47+ private isUrl : string ;
48+ private fallbackHsUrl : string ;
49+ private currentFlowIndex : number ;
50+ // TODO: Flows need a type in JS SDK
51+ private flows : Array < ILoginFlow > ;
52+ private defaultDeviceDisplayName : string ;
53+ private tempClient : MatrixClient ;
54+
55+ constructor (
56+ hsUrl : string ,
57+ isUrl : string ,
58+ fallbackHsUrl ?: string ,
59+ opts ?: ILoginOptions ,
60+ ) {
61+ this . hsUrl = hsUrl ;
62+ this . isUrl = isUrl ;
63+ this . fallbackHsUrl = fallbackHsUrl ;
64+ this . currentFlowIndex = 0 ;
65+ this . flows = [ ] ;
66+ this . defaultDeviceDisplayName = opts . defaultDeviceDisplayName ;
67+ this . tempClient = null ; // memoize
3268 }
3369
34- getHomeserverUrl ( ) {
35- return this . _hsUrl ;
70+ public getHomeserverUrl ( ) : string {
71+ return this . hsUrl ;
3672 }
3773
38- getIdentityServerUrl ( ) {
39- return this . _isUrl ;
74+ public getIdentityServerUrl ( ) : string {
75+ return this . isUrl ;
4076 }
4177
42- setHomeserverUrl ( hsUrl ) {
43- this . _tempClient = null ; // clear memoization
44- this . _hsUrl = hsUrl ;
78+ public setHomeserverUrl ( hsUrl : string ) : void {
79+ this . tempClient = null ; // clear memoization
80+ this . hsUrl = hsUrl ;
4581 }
4682
47- setIdentityServerUrl ( isUrl ) {
48- this . _tempClient = null ; // clear memoization
49- this . _isUrl = isUrl ;
83+ public setIdentityServerUrl ( isUrl : string ) : void {
84+ this . tempClient = null ; // clear memoization
85+ this . isUrl = isUrl ;
5086 }
5187
5288 /**
5389 * Get a temporary MatrixClient, which can be used for login or register
5490 * requests.
5591 * @returns {MatrixClient }
5692 */
57- createTemporaryClient ( ) {
58- if ( this . _tempClient ) return this . _tempClient ; // use memoization
59- return this . _tempClient = Matrix . createClient ( {
60- baseUrl : this . _hsUrl ,
61- idBaseUrl : this . _isUrl ,
93+ public createTemporaryClient ( ) : MatrixClient {
94+ if ( this . tempClient ) return this . tempClient ; // use memoization
95+ return this . tempClient = Matrix . createClient ( {
96+ baseUrl : this . hsUrl ,
97+ idBaseUrl : this . isUrl ,
6298 } ) ;
6399 }
64100
65- getFlows ( ) {
66- const self = this ;
101+ public async getFlows ( ) : Promise < Array < ILoginFlow > > {
67102 const client = this . createTemporaryClient ( ) ;
68- return client . loginFlows ( ) . then ( function ( result ) {
69- self . _flows = result . flows ;
70- self . _currentFlowIndex = 0 ;
71- // technically the UI should display options for all flows for the
72- // user to then choose one, so return all the flows here.
73- return self . _flows ;
74- } ) ;
103+ const { flows } = await client . loginFlows ( ) ;
104+ this . flows = flows ;
105+ this . currentFlowIndex = 0 ;
106+ // technically the UI should display options for all flows for the
107+ // user to then choose one, so return all the flows here.
108+ return this . flows ;
75109 }
76110
77- chooseFlow ( flowIndex ) {
78- this . _currentFlowIndex = flowIndex ;
111+ public chooseFlow ( flowIndex ) : void {
112+ this . currentFlowIndex = flowIndex ;
79113 }
80114
81- getCurrentFlowStep ( ) {
115+ public getCurrentFlowStep ( ) : string {
82116 // technically the flow can have multiple steps, but no one does this
83117 // for login so we can ignore it.
84- const flowStep = this . _flows [ this . _currentFlowIndex ] ;
118+ const flowStep = this . flows [ this . currentFlowIndex ] ;
85119 return flowStep ? flowStep . type : null ;
86120 }
87121
88- loginViaPassword ( username , phoneCountry , phoneNumber , pass ) {
89- const self = this ;
90-
122+ public loginViaPassword (
123+ username : string ,
124+ phoneCountry : string ,
125+ phoneNumber : string ,
126+ password : string ,
127+ ) : Promise < IMatrixClientCreds > {
91128 const isEmail = username . indexOf ( "@" ) > 0 ;
92129
93130 let identifier ;
@@ -113,14 +150,14 @@ export default class Login {
113150 }
114151
115152 const loginParams = {
116- password : pass ,
117- identifier : identifier ,
118- initial_device_display_name : this . _defaultDeviceDisplayName ,
153+ password,
154+ identifier,
155+ initial_device_display_name : this . defaultDeviceDisplayName ,
119156 } ;
120157
121158 const tryFallbackHs = ( originalError ) => {
122159 return sendLoginRequest (
123- self . _fallbackHsUrl , this . _isUrl , 'm.login.password' , loginParams ,
160+ this . fallbackHsUrl , this . isUrl , 'm.login.password' , loginParams ,
124161 ) . catch ( ( fallbackError ) => {
125162 console . log ( "fallback HS login failed" , fallbackError ) ;
126163 // throw the original error
@@ -130,11 +167,11 @@ export default class Login {
130167
131168 let originalLoginError = null ;
132169 return sendLoginRequest (
133- self . _hsUrl , self . _isUrl , 'm.login.password' , loginParams ,
170+ this . hsUrl , this . isUrl , 'm.login.password' , loginParams ,
134171 ) . catch ( ( error ) => {
135172 originalLoginError = error ;
136173 if ( error . httpStatus === 403 ) {
137- if ( self . _fallbackHsUrl ) {
174+ if ( this . fallbackHsUrl ) {
138175 return tryFallbackHs ( originalLoginError ) ;
139176 }
140177 }
@@ -154,11 +191,16 @@ export default class Login {
154191 * @param {string } hsUrl the base url of the Homeserver used to log in.
155192 * @param {string } isUrl the base url of the default identity server
156193 * @param {string } loginType the type of login to do
157- * @param {object } loginParams the parameters for the login
194+ * @param {ILoginParams } loginParams the parameters for the login
158195 *
159196 * @returns {MatrixClientCreds }
160197 */
161- export async function sendLoginRequest ( hsUrl , isUrl , loginType , loginParams ) {
198+ export async function sendLoginRequest (
199+ hsUrl : string ,
200+ isUrl : string ,
201+ loginType : string ,
202+ loginParams : ILoginParams ,
203+ ) : Promise < IMatrixClientCreds > {
162204 const client = Matrix . createClient ( {
163205 baseUrl : hsUrl ,
164206 idBaseUrl : isUrl ,
0 commit comments