I may be a bit confused on what firebase is trying to do here. I can get a user to sign into my site using the existing firebase code I have in react, but when I try to get any sort of access to the username of that user (so i can print it out on the screen), I don't get anything.
The filestructure of my react app is index.jsx which loads Login.jsx. Login.jsx looks like this:
import React from 'react';
import axios from 'axios';
import * as firebase from 'firebase';
class Login extends React.Component {
constructor(props) {
super(props);
this.state = {email: '', password: ''};
this.engageLogin = this.engageLogin.bind(this);
var config = {
apiKey: "-------------------",
authDomain: "--------.firebaseapp.com",
databaseURL: "https://------.firebaseio.com",
projectId: "-------",
storageBucket: "-------.appspot.com",
messagingSenderId: "------------"
};
firebase.initializeApp(config);
}
engageLogin (event) {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
});
event.preventDefault();
}
render() {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log('This is the user: ', user)
} else {
// No user is signed in.
console.log('There is no logged in user');
}
});
return (
<div>
<div className="container">
<h2 className="title"> Stream Labs </h2>
<form onSubmit={this.engageLogin}>
<div className="form-group row">
<div type="submit" className="g-signin2">Sign in</div>
</div>
</form>
</div>
</div>
);
}
}
export default Login;
From what I understand, that firebase.auth().onAuthStateChanged is supposed to fire off and console.log whenever a user logs in, but I only ever get the 'there is no logged in user' message. Can anyone point me in the right direction here?
GApiwhich provides a sign-in button? Or are you actually using GoogleAuthProvider? and signing in with popup? Try logging the result of signInWithPopup to confirm it is successful.