Skip to content

Commit abf8bac

Browse files
author
Kadi Kraman
committed
Move logic to getLoginMethodAndParams
1 parent 4e23d65 commit abf8bac

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

src/actions/auth.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -443,35 +443,31 @@ export const login = (dispatch, firebase, credentials) => {
443443

444444
const { method, params } = getLoginMethodAndParams(firebase, credentials)
445445

446-
const firebaseAuth = firebase.auth()
447-
448-
let firebaseMethod = firebaseAuth[method]
449-
450-
// support older versions of firebase that still use signInWithCustomToken & signInWithEmailAndPassword
451-
switch (method) {
452-
case 'signInAndRetrieveDataWithCustomToken':
453-
firebaseMethod = firebaseAuth.signInWithCustomToken
454-
break
455-
case 'signInAndRetrieveDataWithEmailAndPassword':
456-
firebaseMethod = firebaseAuth.signInWithEmailAndPassword
457-
break
458-
default:
459-
break
460-
}
461-
462-
return firebaseMethod(...params)
446+
return firebase
447+
.auth()
448+
[method](...params)
463449
.then(userData => {
464450
// Handle null response from getRedirectResult before redirect has happened
465451
if (!userData) return Promise.resolve(null)
466452

467453
// For email auth return uid (createUser is used for creating a profile)
468-
if (method === 'signInAndRetrieveDataWithEmailAndPassword') {
454+
if (
455+
[
456+
'signInWithEmailAndPassword',
457+
'signInAndRetrieveDataWithEmailAndPassword'
458+
].includes(method)
459+
) {
469460
return { user: userData }
470461
}
471462
// TODO: Only call createUserProfile once, and just pass different settings
472463

473464
// For token auth, the user key doesn't exist. Instead, return the JWT.
474-
if (method === 'signInAndRetrieveDataWithCustomToken') {
465+
if (
466+
[
467+
'signInWithCustomToken',
468+
'signInAndRetrieveDataWithCustomToken'
469+
].includes(method)
470+
) {
475471
if (!firebase._.config.updateProfileOnLogin) {
476472
return { user: userData }
477473
}

src/utils/auth.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ export const getLoginMethodAndParams = (firebase, creds) => {
9797
return { method: 'signInWithRedirect', params: [authProvider] }
9898
}
9999
if (token) {
100-
return { method: 'signInAndRetrieveDataWithCustomToken', params: [token] }
100+
const tokenAuth = firebase.auth().signInAndRetrieveDataWithCustomToken
101+
102+
if (tokenAuth) {
103+
return { method: 'signInAndRetrieveDataWithCustomToken', params: [token] }
104+
}
105+
106+
return { method: 'signInWithCustomToken', params: [token] }
101107
}
102108
if (phoneNumber) {
103109
if (!applicationVerifier) {
@@ -110,10 +116,18 @@ export const getLoginMethodAndParams = (firebase, creds) => {
110116
params: [phoneNumber, applicationVerifier]
111117
}
112118
}
113-
return {
114-
method: 'signInAndRetrieveDataWithEmailAndPassword',
115-
params: [email, password]
119+
120+
const emailPasswordAuth = firebase.auth()
121+
.signInAndRetrieveDataWithEmailAndPassword
122+
123+
if (emailPasswordAuth) {
124+
return {
125+
method: 'signInAndRetrieveDataWithEmailAndPassword',
126+
params: [email, password]
127+
}
116128
}
129+
130+
return { method: 'signInWithEmailAndPassword', params: [email, password] }
117131
}
118132

119133
/**

0 commit comments

Comments
 (0)