Skip to content

Commit d208d28

Browse files
queervioletqueerviolet
queerviolet
authored and
queerviolet
committed
Better comments for the login ratchet.
1 parent a82c182 commit d208d28

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

app/main.jsx

+34-10
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,59 @@ import {render} from 'react-dom'
55
import {connect, Provider} from 'react-redux'
66

77
import store from './store'
8-
import Jokes from './components/Jokes'
9-
import Login from './components/Login'
108
import WhoAmI from './components/WhoAmI'
119
import NotFound from './components/NotFound'
1210

1311
import firebase from 'APP/fire'
12+
13+
// -- // Demo components // -- //
1414
import Scratchpad from 'APP/demos/scratchpad'
1515

16+
// Get the auth API from Firebase.
1617
const auth = firebase.auth()
17-
, db = firebase.database()
1818

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.
2342

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}) =>
2546
<div>
2647
<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 */}
2851
<WhoAmI auth={auth}/>
2952
</nav>
53+
{/* Render our children (whatever the router gives us) */}
3054
{children}
3155
</div>
3256

3357
render(
3458
<Provider store={store}>
3559
<Router history={browserHistory}>
36-
<Route path="/" component={ExampleApp}>
60+
<Route path="/" component={App}>
3761
<IndexRedirect to="scratchpad/welcome"/>
3862
<Route path="scratchpad/:title" component={Scratchpad}/>
3963
</Route>

0 commit comments

Comments
 (0)