From 8ecb47022c7381101be4139a07523986d392864f Mon Sep 17 00:00:00 2001 From: Scott Prue Date: Thu, 28 May 2020 13:34:55 -0400 Subject: [PATCH] v0.9.4 * fix(app): add back logic for adding account on first login * chore(app): prevent infinite reloading on local when version is changed --- package.json | 2 +- src/components/VersionChangeReloader/index.js | 7 ++- .../Login/components/LoginPage/LoginPage.js | 53 +++++++++++++++---- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index e784c4fe..2b72fb7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fireadmin", - "version": "0.9.3", + "version": "0.9.4", "description": "Application for Managing Firebase Applications. Includes support for multiple environments and data migrations.", "scripts": { "clean": "rimraf build", diff --git a/src/components/VersionChangeReloader/index.js b/src/components/VersionChangeReloader/index.js index 7a74f375..1c99d6ad 100644 --- a/src/components/VersionChangeReloader/index.js +++ b/src/components/VersionChangeReloader/index.js @@ -31,7 +31,12 @@ export default function VersionChangeReloader() { const refreshHasOccurred = currentRemoteVersion === sessionVersion // Refresh if session contains different version than database - if (versionDiscrepencyExists && !refreshHasOccurred) { + if ( + versionDiscrepencyExists && + !refreshHasOccurred && + // refresh not enabled locally since DB update happens in deploy + !window.location.host.includes('localhost') + ) { window.location.reload(true) } }, [versionInfo]) diff --git a/src/routes/Login/components/LoginPage/LoginPage.js b/src/routes/Login/components/LoginPage/LoginPage.js index c6c0cc48..fe97ec01 100644 --- a/src/routes/Login/components/LoginPage/LoginPage.js +++ b/src/routes/Login/components/LoginPage/LoginPage.js @@ -1,39 +1,72 @@ import React, { useState } from 'react' import firebase from 'firebase/app' // imported for auth provider -import { useAuth } from 'reactfire' +import { useAuth, useFirestore } from 'reactfire' import GoogleButton from 'react-google-button' import { makeStyles } from '@material-ui/core/styles' import Container from '@material-ui/core/Container' import Paper from '@material-ui/core/Paper' +import Typography from '@material-ui/core/Typography' +import * as Sentry from '@sentry/browser' +import { USERS_COLLECTION } from 'constants/firebasePaths' import useNotifications from 'modules/notification/useNotifications' import { LIST_PATH } from 'constants/paths' import LoadingSpinner from 'components/LoadingSpinner' import styles from './LoginPage.styles' -import { Typography } from '@material-ui/core' const useStyles = makeStyles(styles) function LoginPage() { const classes = useStyles() const auth = useAuth() + const firestore = useFirestore() const { showError } = useNotifications() const [isLoading, changeLoadingState] = useState(false) - function googleLogin() { + async function googleLogin() { const provider = new firebase.auth.GoogleAuthProvider() changeLoadingState(true) const authMethod = window.isMobile && window.isMobile.any ? 'signInWithRedirect' : 'signInWithPopup' + let authData + try { + authData = await auth[authMethod](provider) + } catch (err) { + console.error('Error with login:', err) // eslint-disable-line no-console + showError(err.message) + Sentry.captureException(err) + } - return auth[authMethod](provider) - .then(() => { - // NOTE: history.push sometimes does not trigger - // history.push(LIST_PATH) - window.location = LIST_PATH - }) - .catch((err) => showError(err.message)) + try { + // Load user account to see if it exists + const userSnap = await firestore + .doc(`${USERS_COLLECTION}/${authData.user.uid}`) + .get() + // Save new user account if it doesn't already exist + if (!userSnap.exists) { + const { + email, + displayName, + providerData, + lastLoginAt + } = authData.user.toJSON() + const userObject = { email, displayName, lastLoginAt } + if (providerData) { + userObject.providerData = providerData + } + await firestore + .doc(`${USERS_COLLECTION}/${authData.user.uid}`) + .set(userObject, { merge: true }) + } + // NOTE: history.push sometimes does not trigger + // history.push(LIST_PATH) + window.location = LIST_PATH + } catch (err) { + console.error('Error setting user profile:', err) // eslint-disable-line no-console + showError(err.message) + Sentry.captureException(err) + } } return (