We will be re-building the infamous growler app, this time with firebase and react.JS . Instead of using Auth0 for authentication, we will use Firebases build in authentication. We will use react-router
Firebase has Oauth, and standard authentication built in. Signing up or logging in is as easy as calling a method. However, it will be our responsibility to persist the user on the frontend.
(Tutorialspoint Demo)
Codealong Steps
-
Git Pull from WDI-10
-
Create OAuth Application on Github
-
Log in to my firebase and show the process
-
Go through the mockups
-
create-react-app inside student/instructor directory
-
Delete boilerplate code, create components from mockup, bring over css
// Login.js
import '../bower_components/bootstrap/dist/css/bootstrap.css';
import '../bower_components/font-awesome/css/font-awesome.css';
import '../bower_components/bootstrap-social/bootstrap-social.css';
- Setup react-router-dom inside of index.js, with the Login component & app component
import { BrowserRouter as Router, Route } from 'react-router-dom';
<Router>
<div>
<Route exact path="/" component={Login}/>
<Route path="/app" component={App} />
<Route path="*" render={()=>{
return <Redirect to="/" />
}} />
</div>
</Router>
- Build authentication functions inside login.js
googleSignIn(){
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then((result) => {
var token = result.credential.accessToken;
var user = result.user;
}).catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
});
}
githubSignIn(){
const provider = new firebase.auth.GithubAuthProvider();
firebase.auth().signInWithPopup(provider).then((result) => {
var token = result.credential.accessToken;
var user = result.user;
}).catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
});
}
-
create onClick for auth buttons, get a console.log of the information
-
create localPersist to add localstorage and redirect to app component
-
create addGrowl, showGrowls, and persistGrowls methods:
persistGrowls(){
this.dbRef.set({
growls: this.state.growls
});
}
componentDidMount(){
this.showGrowls()
}
showGrowls(){
this.dbRef.on('value',(snapshot)=>{
const db = snapshot.val();
// console.log('db: ', db)
if(db){
this.setState({growls: db.growls})
}
});
}
addGrowl(msg){
const growls = this.state.growls;
const user = JSON.parse(localStorage.getItem('user'));
growls.unshift({
name: user.displayName,
email: user.email,
pic: user.photoURL,
msg: msg,
id: Date.now()
})
this.setState({growls})
console.log('state: ', this.state.growls)
this.persistGrowls()
}
-
Make AddGrowl a controlled component, and pass it addGrowl method as a prop.
-
When form submits call a method that calles e.preventDefault(), and this.props.addGrowl, and pass it the message.
-
Check out firebase and console.logs and make sure that growls are being added
-
map growls down to GrowlList, and finally growl.
// GrowlList.js
const growls = this.props.growls.map(growl => {
return(
<Growl key={growl.id} name={growl.name} email={growl.email} pic={growl.pic} msg={growl.msg}/>
)
});
- add Signout method and map it to signout button
// App.js
signout() {
firebase.auth().signOut().then(() => {
console.log('Signout Succesfull');
localStorage.removeItem('user');
this.setState({ loggedIn: false })
}, function(error) {
console.error('Signout Failed')
});
}
- add this.state.loggedIn, and then a redirect if it is false
Finish up