@@ -5,35 +5,59 @@ import {render} from 'react-dom'
5
5
import { connect , Provider } from 'react-redux'
6
6
7
7
import store from './store'
8
- import Jokes from './components/Jokes'
9
- import Login from './components/Login'
10
8
import WhoAmI from './components/WhoAmI'
11
9
import NotFound from './components/NotFound'
12
10
13
11
import firebase from 'APP/fire'
12
+
13
+ // -- // Demo components // -- //
14
14
import Scratchpad from 'APP/demos/scratchpad'
15
15
16
+ // Get the auth API from Firebase.
16
17
const auth = firebase . auth ( )
17
- , db = firebase . database ( )
18
18
19
- auth . onAuthStateChanged ( user => {
20
- console . log ( user )
21
- user || auth . signInAnonymously ( )
22
- } )
19
+ // Ensure that we have (almost) always have a user ID, by creating
20
+ // an anonymous user if nobody is signed in.
21
+ auth . onAuthStateChanged ( user => user || auth . signInAnonymously ( ) )
22
+
23
+ // Further explanation:
24
+ //
25
+ // Whenever someone signs in or out (that's an "authStateChange"),
26
+ // firebase calls our callback with the user. If nobody is signed in,
27
+ // firebase calls us with null. Otherwise, we get a user object.
28
+ //
29
+ // This line of code thus keeps us logged in. If the user isn't signed
30
+ // in, we sign them in anonymously. This is useful, because when the user
31
+ // is signed in (even anonymously), they'll have a user id which you can
32
+ // access with auth.currentUser.uid, and you can use that id to create
33
+ // paths where you store their stuff. (Typically you'll put it somewhere
34
+ // like /users/${currentUser.uid}).
35
+ //
36
+ // Note that the user will still be momentarily null, so your components
37
+ // must be prepared to deal with that. This is unavoidable—with Firebase,
38
+ // the user will always be null for a moment when the app starts, before
39
+ // the authentication information is fetched.
40
+ //
41
+ // If you don't want this behavior, just remove the line above.
23
42
24
- const ExampleApp = ( { children} ) =>
43
+ // Our root App component just renders a little frame with a nav
44
+ // and whatever children the router gave us.
45
+ const App = ( { children} ) =>
25
46
< div >
26
47
< nav >
27
- { /* We pass in the firebase authentication object to WhoAmI */ }
48
+ { /* WhoAmI takes a firebase auth API and renders either a
49
+ greeting and a logout button, or sign in buttons, depending
50
+ on if anyone's logged in */ }
28
51
< WhoAmI auth = { auth } />
29
52
</ nav >
53
+ { /* Render our children (whatever the router gives us) */ }
30
54
{ children }
31
55
</ div >
32
56
33
57
render (
34
58
< Provider store = { store } >
35
59
< Router history = { browserHistory } >
36
- < Route path = "/" component = { ExampleApp } >
60
+ < Route path = "/" component = { App } >
37
61
< IndexRedirect to = "scratchpad/welcome" />
38
62
< Route path = "scratchpad/:title" component = { Scratchpad } />
39
63
</ Route >
0 commit comments