Skip to content

Commit 425563e

Browse files
committed
fblogin: start refactoring to allow multiple auth providers
Still not working, but now there's less to break down the road.
1 parent f8954ec commit 425563e

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

src/components/fbLogin.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ import withFirebaseAuth, { WrappedComponentProps } from 'react-with-firebase-aut
1010
import {store} from '../store.js'
1111
import {compareJournals} from '../utils/utils.js'
1212

13+
14+
function LoginButton (props) {
15+
const dispatch = useDispatch();
16+
async function mySignIn () {
17+
props.providerFn().then( (result) => {
18+
// console.log('STOREATSIGNIN', store.getState())
19+
const newId = result.user?.uid;
20+
21+
newId && dispatch({type: 'CREATE_USER_SUCCESS', payload: newId})
22+
journalRef
23+
.child(newId)
24+
.on('value', (snapshot) => {
25+
compareJournals(store.getState().journal, snapshot.val())
26+
})
27+
})
28+
}
29+
return (
30+
<button onClick={mySignIn}>Sign in with {props.providerName}</button>
31+
)
32+
}
33+
34+
// I'll need these props later on I guess, in an abstracted login form ocmponent
1335
function FbLogin ({
1436
/** These props are provided by withFirebaseAuth HOC */
1537
signInWithEmailAndPassword,
@@ -29,43 +51,31 @@ import {compareJournals} from '../utils/utils.js'
2951
const dispatch = useDispatch();
3052
//const user = useSelector(state => state.user);
3153

32-
async function mySignIn () {
33-
signInWithGoogle().then( (result) => {
34-
// console.log('STOREATSIGNIN', store.getState())
35-
const newId = result.user?.uid;
36-
37-
newId && dispatch({type: 'CREATE_USER_SUCCESS', payload: newId})
38-
journalRef
39-
.child(newId)
40-
.on('value', (snapshot) => {
41-
compareJournals(store.getState().journal, snapshot.val())
42-
})
43-
})
44-
}
45-
4654
async function mySignOut () {
4755
signOut()
4856
dispatch({type: 'LOGOUT'});
4957

5058
}
59+
// const providers = [[signInWithGoogle, "Google"],[signInWithEmailAndPassword, "Email"] ]
60+
61+
const providers = [[signInWithGoogle, "Google"]]
5162
// useEffect (() => {
5263
// console.log('GOTUSERID', user && user.uid)
5364
// })
5465

5566
return (
67+
5668
<>
57-
{
58-
user
59-
? <button className="clear dark" href="#">Hello, {user.displayName}</button>
60-
: <button onClick={mySignIn}>Sign in with Google</button>
69+
{ user ?
70+
<>
71+
<button className="clear dark" href="#">Hello, {user.displayName}</button>
72+
<button className="bg-dark text-light" onClick={mySignOut}>Sign out</button>
73+
</>
74+
: providers.map( ([p, n]) => <LoginButton key={n} providerFn={p} providerName={n}/>)
6175
}
62-
{
63-
user &&
64-
<button className="bg-dark text-light" onClick={mySignOut}>Sign out</button>
65-
}
66-
</>)
67-
68-
}
76+
</>
77+
)
78+
}
6979

7080
export default withFirebaseAuth({
7181
providers: providers,

0 commit comments

Comments
 (0)