|
1 | 1 | import React, { useState } from 'react'
|
2 | 2 | import firebase from 'firebase/app' // imported for auth provider
|
3 |
| -import { useAuth } from 'reactfire' |
| 3 | +import { useAuth, useFirestore } from 'reactfire' |
4 | 4 | import GoogleButton from 'react-google-button'
|
5 | 5 | import { makeStyles } from '@material-ui/core/styles'
|
6 | 6 | import Container from '@material-ui/core/Container'
|
7 | 7 | import Paper from '@material-ui/core/Paper'
|
| 8 | +import Typography from '@material-ui/core/Typography' |
| 9 | +import * as Sentry from '@sentry/browser' |
| 10 | +import { USERS_COLLECTION } from 'constants/firebasePaths' |
8 | 11 | import useNotifications from 'modules/notification/useNotifications'
|
9 | 12 | import { LIST_PATH } from 'constants/paths'
|
10 | 13 | import LoadingSpinner from 'components/LoadingSpinner'
|
11 | 14 | import styles from './LoginPage.styles'
|
12 |
| -import { Typography } from '@material-ui/core' |
13 | 15 |
|
14 | 16 | const useStyles = makeStyles(styles)
|
15 | 17 |
|
16 | 18 | function LoginPage() {
|
17 | 19 | const classes = useStyles()
|
18 | 20 | const auth = useAuth()
|
| 21 | + const firestore = useFirestore() |
19 | 22 | const { showError } = useNotifications()
|
20 | 23 | const [isLoading, changeLoadingState] = useState(false)
|
21 | 24 |
|
22 |
| - function googleLogin() { |
| 25 | + async function googleLogin() { |
23 | 26 | const provider = new firebase.auth.GoogleAuthProvider()
|
24 | 27 | changeLoadingState(true)
|
25 | 28 | const authMethod =
|
26 | 29 | window.isMobile && window.isMobile.any
|
27 | 30 | ? 'signInWithRedirect'
|
28 | 31 | : 'signInWithPopup'
|
| 32 | + let authData |
| 33 | + try { |
| 34 | + authData = await auth[authMethod](provider) |
| 35 | + } catch (err) { |
| 36 | + console.error('Error with login:', err) // eslint-disable-line no-console |
| 37 | + showError(err.message) |
| 38 | + Sentry.captureException(err) |
| 39 | + } |
29 | 40 |
|
30 |
| - return auth[authMethod](provider) |
31 |
| - .then(() => { |
32 |
| - // NOTE: history.push sometimes does not trigger |
33 |
| - // history.push(LIST_PATH) |
34 |
| - window.location = LIST_PATH |
35 |
| - }) |
36 |
| - .catch((err) => showError(err.message)) |
| 41 | + try { |
| 42 | + // Load user account to see if it exists |
| 43 | + const userSnap = await firestore |
| 44 | + .doc(`${USERS_COLLECTION}/${authData.user.uid}`) |
| 45 | + .get() |
| 46 | + // Save new user account if it doesn't already exist |
| 47 | + if (!userSnap.exists) { |
| 48 | + const { |
| 49 | + email, |
| 50 | + displayName, |
| 51 | + providerData, |
| 52 | + lastLoginAt |
| 53 | + } = authData.user.toJSON() |
| 54 | + const userObject = { email, displayName, lastLoginAt } |
| 55 | + if (providerData) { |
| 56 | + userObject.providerData = providerData |
| 57 | + } |
| 58 | + await firestore |
| 59 | + .doc(`${USERS_COLLECTION}/${authData.user.uid}`) |
| 60 | + .set(userObject, { merge: true }) |
| 61 | + } |
| 62 | + // NOTE: history.push sometimes does not trigger |
| 63 | + // history.push(LIST_PATH) |
| 64 | + window.location = LIST_PATH |
| 65 | + } catch (err) { |
| 66 | + console.error('Error setting user profile:', err) // eslint-disable-line no-console |
| 67 | + showError(err.message) |
| 68 | + Sentry.captureException(err) |
| 69 | + } |
37 | 70 | }
|
38 | 71 |
|
39 | 72 | return (
|
|
0 commit comments