Repository: SolidStateGroup/react-native-firebase-auth Branch: master Commit: 9251566a6f4d Files: 6 Total size: 9.8 KB Directory structure: gitextract__dib5akl/ ├── .gitignore ├── CONTRIBUTING.md ├── README.md ├── auth.js ├── index.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .DS_Store .idea *.iml node_modules/ *.log* npm-debug.log *.pyc ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing We're always looking to improve this project, open source contribution is encouraged so long as they adhere to our guidelines. # Pull Requests The Solid State team will be monitoring for pull requests. When we get one, a member of team will test the work against our internal uses and sign off on the changes. From here, we'll either merge the pull request or provide feedback suggesting the next steps. **A couple things to keep in mind:** - If you've changed APIs, update the documentation. - Keep the code style (indents, wrapping) consistent. - If your PR involves a lot of commits, squash them using ```git rebase -i``` as this makes it easier for us to review. - Keep lines under 80 characters. ================================================ FILE: README.md ================================================ **This is the react-native implementation of https://github.com/SolidStateGroup/simple-firebase-auth** # React Native Firebase Auth [![Gitter](https://img.shields.io/gitter/room/gitterHQ/gitter.svg)](https://gitter.im/SolidStateGroup/react-native-firebase-auth?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) Simplified Firebase authentication for React Native projects with support for Facebook & Google login. Using this module alongside Firebase means there is no need to write and host any backend code to handle users logging in to your app. Use our project starter repository (https://github.com/SolidStateGroup/firebase-project-starter) to help you get started setting up your own Firebase project. ## Installation ``` $ npm install --save react-native-firebase-auth ``` **Note:** If you use React Native < `v0.39` or you are already using `react-native-google-signin` then stick with `v0.0.11` (`npm install react-native-firebase-auth@0.0.11 --save`) ## Project Setup ``` $ npm install --save firebase react-native-facebook-login react-native-google-sign-in ``` You will need fully setup both of the below social platform dependencies (react-native-google-sign-in and react-native-facebook-login). https://github.com/joonhocho/react-native-google-sign-in#getting-started https://github.com/magus/react-native-facebook-login#setup You will need to initialise Firebase within your app in the usual way. See https://firebase.google.com/docs/web/setup ## Usage ``` import FireAuth from 'react-native-firebase-auth'; constructor(props) { super(props); FireAuth.init({iosClientId: }); // This is the CLIENT_ID found in your Google services plist. } componentDidMount() { FireAuth.setup(this.onLogin, this.onUserChange, this.onLogout, this.emailVerified, this.onError); } register = () => { const { email, password, firstName, lastName } = this.state; FireAuth.register(email, password, { firstName, lastName }); } login = () => { FireAuth.login(this.state.email, this.state.password); } loginAnonymously = () => { FireAuth.loginAnonymously(); } facebookLogin() { FireAuth.facebookLogin(); } googleLogin() { FireAuth.googleLogin(); } logout() { FireAuth.logout(); } update () => { FireAuth.update({ firstName: this.state.firstName, lastName: this.state.lastName }).then(() => { ... }).catch(err => { ... }); } resetPassword () => { FireAuth.resetPassword(this.state.email) .then(() => { ... }) .catch(err => { ... }); } updatePassword () => { FireAuth.updatePassword(this.state.password) .then(() => { ... }) .catch(err => { ... }); } ``` ## Credits https://github.com/magus/react-native-facebook-login https://github.com/joonhocho/react-native-google-sign-in # Getting Help If you encounter a bug or feature request we would like to hear about it. Before you submit an issue please search existing issues in order to prevent duplicates. # Contributing For more information about contributing PRs, please see our Contribution Guidelines. # Get in touch If you have any questions about our projects you can email projects@solidstategroup.com. ================================================ FILE: auth.js ================================================ import { FBLoginManager } from 'react-native-facebook-login'; import GoogleSignIn from 'react-native-google-sign-in'; const Facebook = { login: (permissions) => { return new Promise((resolve, reject) => { FBLoginManager.loginWithPermissions(permissions || ['email'], (error, data) => { if (!error) { resolve(data.credentials.token); } else { reject(error); } }); }); }, logout: () => { return new Promise((resolve, reject) => { FBLoginManager.logout((error, data) => { if (!error) { resolve(true); } else { reject(error); } }); }); } } const Google = { configure: (options) => { GoogleSignIn.configure(options); }, login: () => { return new Promise((resolve, reject) => { GoogleSignIn.signInPromise() .then((user) => { resolve(user.accessToken); }) .catch((err) => { reject(err); }) .done(); }); }, logout: () => { return new Promise((resolve, reject) => { GoogleSignIn.signOutPromise() .then(() => { resolve(true); }) .catch((err) => { reject(err); }); }); } } const Auth = {Facebook, Google}; export default Auth; ================================================ FILE: index.js ================================================ import * as firebase from 'firebase'; import Auth from './auth'; const FireAuth = class { user = null; profile = null; onUserChange = null; onLogout = null; onEmailVerified = null; onLogin = null; onError = null; init(googleConfig) { Auth.Google.configure(googleConfig); } setup = (onLogin, onUserChange, onLogout, onEmailVerified, onError) => { this.onUserChange = onUserChange; this.onLogout = onLogout; this.onEmailVerified = onEmailVerified; this.onLogin = onLogin; this.onError = onError; firebase.auth().onAuthStateChanged((user)=> { if (user) { // Determine if user needs to verify email var emailVerified = !user.providerData || !user.providerData.length || user.providerData[0].providerId != 'password' || user.emailVerified; // Upsert profile information var profileRef = firebase.database().ref(`profiles/${user.uid}`); profileRef.update({ emailVerified: emailVerified, email: user.email }); profileRef.on('value', (profile)=> { const val = profile.val(); // Email become verified in session if (val.emailVerified && (this.profile && !this.profile.val().emailVerified)) { this.onEmailVerified && this.onEmailVerified(); } if (!this.user) { this.onLogin && this.onLogin(user, val); // On login } else if (val) { this.onUserChange && this.onUserChange(user, val); // On updated } this.profile = profile; // Store profile this.user = user; // Store user }); } else { this.profile = null; this.user = null; // Clear user and logout this.onLogout && this.onLogout(); } }); } login = (email, password) => { try { firebase.auth().signInWithEmailAndPassword(email, password) .catch((err) => this.onError && this.onError(err)); } catch (e) { this.onError && this.onError(e); } } loginAnonymously = () => { try { firebase.auth().signInAnonymously() .catch((err) => this.onError && this.onError(err)); } catch (e) { this.onError && this.onError(e); } } register = (username, password) => { try { firebase.auth().createUserWithEmailAndPassword(username, password) .then((user)=> { user.sendEmailVerification(); }) .catch((err) => this.onError && this.onError(err)); } catch (e) { this.onError && this.onError(e); } } resendVerification = () => { this.user.sendEmailVerification(); } facebookLogin = (permissions) => { Auth.Facebook.login(permissions) .then((token) => ( firebase.auth() .signInWithCredential(firebase.auth.FacebookAuthProvider.credential(token)) )) .catch((err) => this.onError && this.onError(err)); } googleLogin = () => { Auth.Google.login() .then((token) => ( firebase.auth() .signInWithCredential(firebase.auth.GoogleAuthProvider.credential(null, token)) )) .catch((err) => this.onError && this.onError(err)); } logout = () => { firebase.auth().signOut(); } update = (data) => { var profileRef = firebase.database().ref(`profiles/${this.user.uid}`); return profileRef.update(data); } resetPassword = (email) => { firebase.auth().sendPasswordResetEmail(email); } updatePassword = (password) => { this.user.updatePassword(password); } linkWithGoogle = () => { // @TODO } linkWithFacebook = () => { // @TODO } linkWithEmail = () => { // @TODO } }; export default new FireAuth(); ================================================ FILE: package.json ================================================ { "name": "react-native-firebase-auth", "version": "1.0.1", "description": "Simplified Firebase authentication with social platform support", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "react-native", "auth", "social", "facebook", "login", "google", "firebase" ], "author": "SSG", "license": "ISC", "peerDependencies": { "firebase": ">=3.0.0", "react-native": ">=0.30.0", "react-native-facebook-login": ">=1.4.0", "react-native-google-sign-in": ">=0.0.8" }, "repository": { "type": "git", "url": "git+https://github.com/SolidStateGroup/react-native-firebase-auth.git" }, "bugs": { "url": "https://github.com/SolidStateGroup/react-native-firebase-auth/issues" }, "homepage": "https://github.com/SolidStateGroup/react-native-firebase-auth#readme" }