From 137f09b8ee88ec91290c5f88fffbba0651777a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laszlo=20Sch=C3=BCrg?= Date: Mon, 9 Sep 2019 14:28:29 +0200 Subject: [PATCH 1/2] feat(authProvider): added login 'mode' with Firebase Auth Email-Link --- src/providers/AuthProvider.ts | 44 +++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/providers/AuthProvider.ts b/src/providers/AuthProvider.ts index 081ef1b..7042dab 100644 --- a/src/providers/AuthProvider.ts +++ b/src/providers/AuthProvider.ts @@ -16,23 +16,57 @@ class AuthClient { private auth: FirebaseAuth; constructor(firebaseConfig: {}, options: RAFirebaseOptions) { - log("Auth Client: initializing...", {firebaseConfig, options}); + log("Auth Client: initializing...", { firebaseConfig, options }); const fireWrapper = new FirebaseWrapper(); fireWrapper.init(firebaseConfig, options); this.auth = fireWrapper.auth(); } public async HandleAuthLogin(params) { - const { username, password } = params; + const { username, password, mode } = params; + let user; try { - const user = await this.auth.signInWithEmailAndPassword( + if (mode === "link") { + if (this.auth.isSignInWithEmailLink(window.location.href)) { + user = await this.auth.signInWithEmailLink( username, - password + window.location.href ); + + // Save email LocalStorage for same browser login + window.localStorage.removeItem("emailForSignIn"); + + // Clear Search Params + const urlWithoutParams = + window.location.origin + + window.location.pathname + + window.location.hash; + window.history.replaceState({}, document.title, urlWithoutParams); + log("HandleAuthLogin: user sucessfully logged in", { user }); + } else { + console.log("Login link request"); + + const result: any = await this.auth.sendSignInLinkToEmail(username, { + url: window.location.href, + handleCodeInApp: true + }); + + // Clear LocalStorage + window.localStorage.setItem("emailForSignIn", username); + + log("HandleAuthLogin: login link sucessfully requested", { + user: result && result.user + }); + } + } else { + // Regular password login + user = await this.auth.signInWithEmailAndPassword(username, password); + log("HandleAuthLogin: user sucessfully logged in", { user }); + } } catch (e) { - log("HandleAuthLogin: invalid credentials", { params }); + log(`HandleAuthLogin: invalid credentials Error ${e}`, { params }); throw new Error("Login error: invalid credentials"); } } From a21c420cca2b6922d3ecbd0084721d46f7b2ae45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laszlo=20Sch=C3=BCrg?= Date: Mon, 9 Sep 2019 14:44:24 +0200 Subject: [PATCH 2/2] removed console.log() --- src/providers/AuthProvider.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/providers/AuthProvider.ts b/src/providers/AuthProvider.ts index 7042dab..90152fb 100644 --- a/src/providers/AuthProvider.ts +++ b/src/providers/AuthProvider.ts @@ -30,9 +30,9 @@ class AuthClient { if (mode === "link") { if (this.auth.isSignInWithEmailLink(window.location.href)) { user = await this.auth.signInWithEmailLink( - username, + username, window.location.href - ); + ); // Save email LocalStorage for same browser login window.localStorage.removeItem("emailForSignIn"); @@ -44,10 +44,8 @@ class AuthClient { window.location.hash; window.history.replaceState({}, document.title, urlWithoutParams); - log("HandleAuthLogin: user sucessfully logged in", { user }); + log("HandleAuthLogin: user sucessfully logged in", { user }); } else { - console.log("Login link request"); - const result: any = await this.auth.sendSignInLinkToEmail(username, { url: window.location.href, handleCodeInApp: true @@ -75,7 +73,7 @@ class AuthClient { await this.auth.signOut(); } - public async HandleAuthError(params) { } + public async HandleAuthError(params) {} public async HandleAuthCheck(params) { try { @@ -89,7 +87,7 @@ class AuthClient { public async getUserLogin() { return new Promise((resolve, reject) => { - this.auth.onAuthStateChanged((user) => { + this.auth.onAuthStateChanged(user => { if (user) { resolve(user); } else { @@ -154,7 +152,10 @@ export function AuthProvider(firebaseConfig: {}, options: RAFirebaseOptions) { }; } -function VerifyAuthProviderArgs(firebaseConfig: {}, options: RAFirebaseOptions) { +function VerifyAuthProviderArgs( + firebaseConfig: {}, + options: RAFirebaseOptions +) { const hasNoApp = !options || !options.app; const hasNoConfig = !firebaseConfig; if (hasNoConfig && hasNoApp) {