README
react-auth-firebase
A React package to simplify firebase authentication. All it has is a single HOC.
This does not work with React Native.
Supported Authorization methods
- Github
Demo
See this codesandbox for demo.
Usage
Install
React HOC for easier firebase authentication
<script type="module">
import reactAuthFirebase from 'https://cdn.skypack.dev/react-auth-firebase';
</script>
A React package to simplify firebase authentication. All it has is a single HOC.
This does not work with React Native.
See this codesandbox for demo.
gt; npm install firebase react-auth-firebase (or) gt; yarn add firebase react-auth-firebase
//firebaseConfig.js
//
import firebase from "firebase";
// https://firebase.google.com/docs/web/setup?authuser=0
// See firebase setup in above google firebase documentation url
export const config = {
apiKey: "----",
authDomain: "----",
databaseURL: "----",
projectId: "----",
storageBucket: "----",
messagingSenderId: "----"
};
firebase.initializeApp(config);
export default firebase;
//App.js
import React, { Component } from "react";
import firebase from "./firebaseConfig"; // Careful to not import from "firebase"
import withFirebaseAuth from "react-auth-firebase";
class App extends Component {
render() {
// user object will have signed in user details if auth state changed
// user will be null if not logged in
// Use only the required methods.
// If a property called 'google' not given in authConfig, signInWithGoogle and googleAccessToken will not be available for use.
// Similar for others.
const {
signInWithEmail,
signUpWithEmail,
signInWithGoogle,
signInWithFacebook,
signInWithGithub,
signInWithTwitter,
googleAccessToken,
facebookAccessToken,
githubAccessToken,
twitterAccessToken,
twitterSecret,
signOut,
user,
error
} = this.props;
const { email, password } = this.state;
return (
<div>
// For Sign In
<form onSubmit={e => e.preventDefault()}>
...form input to take email and password for sign in
<button
type="submit"
onClick={() => signInWithEmail(email, password)}
>
SignIn
</button>
</form>
// For Sign Up
<form onSubmit={e => e.preventDefault()}>
...form input to take email and password for sign up
<button
type="submit"
onClick={() => signUpWithEmail(email, password)}
>
SignUp
</button>
</form>
<button onClick={signInWithGoogle}>Signin with Google</button>
<button onClick={signInWithFacebook}>Signin with Facebook</button>
<button onClick={signInWithGithub}>Signin with Github</button>
<button onClick={signInWithTwitter}>Signin with Twitter</button>
</div>
);
}
}
// Important
// See authConfig api for all available options
// Add only the required auth types.
// Only their related props will be added
// For ex: signInWithGoogle will be added only when there is google object in authConfig
// At least an empty object required to enable that method
const authConfig = {
email: {
verifyOnSignup: false, // Sends verification email to user upon sign up
saveUserInDatabase: true // Saves user in database at /users ref
},
google: {
// redirect: true, // Opens a pop up by default
returnAccessToken: true, // Returns an access token as googleAccessToken prop
saveUserInDatabase: true // Saves user in database at /users ref
},
facebook: {
// redirect: true, // Opens a pop up by default
returnAccessToken: true, // Returns an access token as googleAccessToken prop
saveUserInDatabase: true // Saves user in database at /users ref
},
github: {
// redirect: true,
returnAccessToken: true,
saveUserInDatabase: true
},
twitter: {
// redirect: true,
returnAccessToken: true,
returnSecret: true,
saveUserInDatabase: true
}
};
export default withFirebaseAuth(App, firebase, authConfig);
returns
arguments
verifyOnSignup: Boolean
saveUserInDatabase: Boolean
NOTE: Make sure your domain is authorized for oAuth at Firebase console -> Authentication -> Sign-in method -> Authorized Domains
customParams: Object
redirect: Boolean
returnAccessToken: Boolean
saveUserInDatabase: Boolean
NOTE: Set up facebook application at Facebook Developers with Facebook Login product enabled. Add App ID and App Secret from Facebook App in Firebase console. Copy the Redirect URI shown in Firebase console to Facebook App -> Products -> Facebook Login -> Valid OAuth redirect URIs
customParams: Object
redirect: Boolean
returnAccessToken: Boolean
saveUserInDatabase: Boolean
github
NOTE: Set up github application at Github Applications with callback URL shown at Firebase Console. Add the generated Client ID and Client Secret to Firebase Console.
customParams: Object
redirect: Boolean
returnAccessToken: Boolean
saveUserInDatabase: Boolean
NOTE: Set up twitter application at Twiiter Applications with callback URL shown at Firebase Console. Add the generated API Key and API Secret to Firebase Console.
customParams: Object
redirect: Boolean
returnAccessToken: Boolean
returnSecret: Boolean
Should return a twitter secret token as twitterSecret prop ?
default: false
saveUserInDatabase: Boolean
NOTE: Twitter by default doesn't give back any email, so it will be saved as null. Check this Stackoverflow Question for further information on getting email.
signInWithEmail: Function
signUpWithEmail: Function
signInWithGoogle: Function
signInWithFacebook: Function
signInWithGithub: Function
signInWithTwitter: Function
googleAccessToken: String
facebookAccessToken: String
githubAccessToken: String
twitterAccessToken: String
twitterSecret: String
signOut: Function
user: Object
error: Object