import { Firebase, firebaseAuth, firebaseRef } from './firebase'

export const login = () => {
  const provider = new firebaseAuth.GithubAuthProvider()
  provider.addScope('repo')
  // TODO on mobile, sign in without redirect
  firebaseAuth().signInWithPopup(provider).then(function(result) {
    const user = result.user
    firebaseRef.child('users').child(user.uid).set({
      displayName: user.displayName,
      email: user.email,
      lastSignedInAt: Firebase.database.ServerValue.TIMESTAMP,
    })
  }).catch(function(error) {
    console.error('login error', error)
    // TODO display the error
  })
}

export const logout = () => firebaseAuth().signOut()

export const onAuthStateChanged = (cb) =>
  firebaseAuth().onAuthStateChanged(cb)

@@ -170,12 +170,12 @@
 pe('
-repo
+user
 ')%0A 
@@ -292,34 +292,226 @@
 %7B%0A  
-  const user = result.user
+%7D).catch(function(error) %7B%0A    console.error('login error', error)%0A    // TODO display the error%0A  %7D)%0A%7D%0A%0Aexport const logout = () =%3E firebaseAuth().signOut()%0A%0AfirebaseAuth().onAuthStateChanged((user) =%3E %7B%0A  if (user) %7B
 %0A   
@@ -558,11 +558,14 @@
 id).
-set
+update
 (%7B%0A 
@@ -702,163 +702,10 @@
 %0A  %7D
-).catch(function(error) %7B%0A    console.error('login error', error)%0A    // TODO display the error%0A  %7D)%0A%7D%0A%0Aexport const logout = () =%3E firebaseAuth().signOut(
+%0A%7D
 )%0A%0Ae

Change auth scope; set user data on auth state changed