11import * as firebase from "../../firebase" ;
22import { FirebaseEmailLinkActionCodeSettings , LoginType , User } from "../../firebase" ;
33
4- export module auth {
4+ export namespace auth {
55 export class Auth {
66 private authStateChangedHandler ;
77 public currentUser : User ;
88 public languageCode : string | null ;
99
10+ private loginHelper ( options : firebase . LoginOptions ) {
11+ return new Promise ( ( resolve , reject ) => {
12+ firebase . login ( options )
13+ . then ( ( user : User ) => {
14+ this . currentUser = user ;
15+ this . authStateChangedHandler && this . authStateChangedHandler ( user ) ;
16+ resolve ( {
17+ additionalUserInfo : user . additionalUserInfo ,
18+ credential : null ,
19+ operationType : "SignIn" ,
20+ user : user ,
21+ } ) ;
22+ } ) . catch ( err => {
23+ let code = 'auth/exception' ;
24+ let message = err . toString ( ) ;
25+ // Identify code for android. Note that the IOS implementation doesn't return a code.
26+ if ( message . includes ( 'com.google.firebase.auth.FirebaseAuthInvalidCredentialsException' ) ) {
27+ code = 'auth/wrong-password' ;
28+ } else if ( message . includes ( 'com.google.firebase.auth.FirebaseAuthInvalidUserException' ) ) {
29+ code = 'auth/user-not-found' ;
30+ // Note that Android returns one exception for both user not found and invalid email whereas
31+ // the web api returns seperate codes. Therefore the conditional below can never be satisfied
32+ // for android.
33+ // } else if (message.includes('com.google.firebase.auth.FirebaseAuthInvalidUserException')) {
34+ // code = 'auth/invalid-email'
35+ }
36+ reject ( {
37+ code : code ,
38+ message : message
39+ } ) ;
40+ } ) ;
41+ } ) ;
42+ }
43+
1044 public onAuthStateChanged ( handler : ( user : User ) => void ) : void {
1145 this . authStateChangedHandler = handler ;
1246 console . log ( ">> added onAuthStateChanged handler" ) ;
13- } ;
47+ }
1448
1549 public signOut ( ) : Promise < any > {
1650 return new Promise ( ( resolve , reject ) => {
@@ -46,63 +80,53 @@ export module auth {
4680 }
4781
4882 public signInWithEmailAndPassword ( email : string , password : string ) : Promise < any > {
49- return new Promise ( ( resolve , reject ) => {
50- firebase . login ( {
51- type : LoginType . PASSWORD ,
52- passwordOptions : {
53- email : email ,
54- password : password
55- }
56- } ) . then ( ( user : User ) => {
57- this . currentUser = user ;
58- this . authStateChangedHandler && this . authStateChangedHandler ( user ) ;
59- resolve ( {
60- additionalUserInfo : user . additionalUserInfo ,
61- credential : null ,
62- operationType : "SignIn" ,
63- user : user ,
64- } ) ;
65- } ) . catch ( err => {
66- let code = 'auth/exception' ;
67- let message = err . toString ( ) ;
68- // Identify code for android. Note that the IOS implementation doesn't return a code.
69- if ( message . includes ( 'com.google.firebase.auth.FirebaseAuthInvalidCredentialsException' ) ) {
70- code = 'auth/wrong-password' ;
71- } else if ( message . includes ( 'com.google.firebase.auth.FirebaseAuthInvalidUserException' ) ) {
72- code = 'auth/user-not-found' ;
73- // Note that Android returns one exception for both user not found and invalid email whereas
74- // the web api returns seperate codes. Therefore the conditional below can never be satisfied
75- // for android.
76- // } else if (message.includes('com.google.firebase.auth.FirebaseAuthInvalidUserException')) {
77- // code = 'auth/invalid-email'
78- }
79- reject ( {
80- code : code ,
81- message : message
82- } )
83- } ) ;
84- } ) ;
83+ const emailOption : firebase . LoginOptions = {
84+ type : LoginType . PASSWORD ,
85+ passwordOptions : {
86+ email : email ,
87+ password : password
88+ }
89+ } ;
90+ return this . loginHelper ( emailOption ) ;
91+ }
92+
93+ public signInWithCustomToken ( token : string ) : Promise < any > {
94+ const customTokenOption : firebase . LoginOptions = {
95+ type : LoginType . CUSTOM ,
96+ customOptions : {
97+ token : token
98+ }
99+ } ;
100+ return this . loginHelper ( customTokenOption ) ;
101+ }
102+
103+ public signInAnonymously ( ) : Promise < any > {
104+ const anonymousOption : firebase . LoginOptions = {
105+ type : LoginType . ANONYMOUS
106+ } ;
107+ return this . loginHelper ( anonymousOption ) ;
85108 }
86109
87110 public sendSignInLinkToEmail ( email : string , actionCodeSettings : FirebaseEmailLinkActionCodeSettings ) : Promise < any > {
88- return new Promise ( ( resolve , reject ) => {
89- firebase . login ( {
90- type : LoginType . EMAIL_LINK ,
111+ const sendSignInLinklOption : firebase . LoginOptions = {
112+ type : LoginType . EMAIL_LINK ,
91113 emailLinkOptions : {
92114 email : email ,
93115 url : actionCodeSettings . url ,
94116 }
95- } ) . then ( ( user : User ) => {
96- this . currentUser = user ;
97- this . authStateChangedHandler && this . authStateChangedHandler ( user ) ;
98- resolve ( ) ;
99- } , ( err => {
100- reject ( {
101- // code: "",
102- message : err
103- } ) ;
104- } ) ) ;
105- } ) ;
117+ } ;
118+ return this . loginHelper ( sendSignInLinklOption ) ;
119+ }
120+
121+ public signInWithEmailLink ( email : string , emailLink : string ) : Promise < any > {
122+ const signInWithEmailOption : firebase . LoginOptions = {
123+ type : firebase . LoginType . EMAIL_LINK ,
124+ emailLinkOptions : {
125+ email : email ,
126+ url : emailLink
127+ }
128+ } ;
129+ return this . loginHelper ( signInWithEmailOption ) ;
106130 }
107131
108132 public createUserWithEmailAndPassword ( email : string , password : string ) : Promise < User > {
@@ -114,47 +138,30 @@ export module auth {
114138 this . currentUser = user ;
115139 resolve ( user ) ;
116140 } ) . catch ( err => reject ( err ) ) ;
117- } )
141+ } ) ;
118142 }
119143
120144 public updateEmail ( newEmail : string ) : Promise < void > {
121145 return new Promise < void > ( ( resolve , reject ) => {
122146 firebase . updateEmail ( newEmail )
123147 . then ( ( ) => resolve ( ) )
124148 . catch ( err => reject ( err ) ) ;
125- } )
149+ } ) ;
126150 }
127151
128152 public updatePassword ( newPassword : string ) : Promise < void > {
129153 return new Promise < void > ( ( resolve , reject ) => {
130154 firebase . updatePassword ( newPassword )
131155 . then ( ( ) => resolve ( ) )
132156 . catch ( err => reject ( err ) ) ;
133- } )
157+ } ) ;
134158 }
135159
136160 public sendPasswordResetEmail ( email : string ) : Promise < void > {
137161 return new Promise < void > ( ( resolve , reject ) => {
138162 firebase . sendPasswordResetEmail ( email )
139163 . then ( ( ) => resolve ( ) )
140164 . catch ( err => reject ( err ) ) ;
141- } )
142- }
143-
144- public signInAnonymously ( ) : Promise < any > {
145- return new Promise ( ( resolve , reject ) => {
146- firebase . login ( {
147- type : LoginType . ANONYMOUS
148- } ) . then ( ( user : User ) => {
149- this . currentUser = user ;
150- this . authStateChangedHandler && this . authStateChangedHandler ( user ) ;
151- resolve ( ) ;
152- } , ( err => {
153- reject ( {
154- // code: "",
155- message : err
156- } ) ;
157- } ) ) ;
158165 } ) ;
159166 }
160167
0 commit comments