├── .gitignore ├── CONTRIBUTING.md ├── README.md ├── auth.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | *.iml 4 | node_modules/ 5 | *.log* 6 | npm-debug.log 7 | *.pyc 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | We're always looking to improve this project, open source contribution is encouraged so long as they adhere to our guidelines. 3 | 4 | # Pull Requests 5 | 6 | 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. 7 | 8 | **A couple things to keep in mind:** 9 | 10 | - If you've changed APIs, update the documentation. 11 | - Keep the code style (indents, wrapping) consistent. 12 | - If your PR involves a lot of commits, squash them using ```git rebase -i``` as this makes it easier for us to review. 13 | - Keep lines under 80 characters. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **This is the react-native implementation of https://github.com/SolidStateGroup/simple-firebase-auth** 2 | 3 | # React Native Firebase Auth 4 | 5 | [![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) 6 | 7 | Simplified Firebase authentication for React Native projects with support for Facebook & Google login. 8 | 9 | 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. 10 | 11 | Use our project starter repository (https://github.com/SolidStateGroup/firebase-project-starter) to help you get started setting up your own Firebase project. 12 | 13 | 14 | ## Installation 15 | 16 | ``` 17 | $ npm install --save react-native-firebase-auth 18 | ``` 19 | 20 | **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`) 21 | 22 | ## Project Setup 23 | 24 | ``` 25 | $ npm install --save firebase react-native-facebook-login react-native-google-sign-in 26 | ``` 27 | 28 | You will need fully setup both of the below social platform dependencies (react-native-google-sign-in and react-native-facebook-login). 29 | 30 | https://github.com/joonhocho/react-native-google-sign-in#getting-started 31 | https://github.com/magus/react-native-facebook-login#setup 32 | 33 | You will need to initialise Firebase within your app in the usual way. See https://firebase.google.com/docs/web/setup 34 | 35 | ## Usage 36 | 37 | ``` 38 | import FireAuth from 'react-native-firebase-auth'; 39 | 40 | constructor(props) { 41 | super(props); 42 | FireAuth.init({iosClientId: }); // This is the CLIENT_ID found in your Google services plist. 43 | } 44 | 45 | componentDidMount() { 46 | FireAuth.setup(this.onLogin, this.onUserChange, this.onLogout, this.emailVerified, this.onError); 47 | } 48 | 49 | register = () => { 50 | const { email, password, firstName, lastName } = this.state; 51 | FireAuth.register(email, password, { firstName, lastName }); 52 | } 53 | 54 | login = () => { 55 | FireAuth.login(this.state.email, this.state.password); 56 | } 57 | 58 | loginAnonymously = () => { 59 | FireAuth.loginAnonymously(); 60 | } 61 | 62 | facebookLogin() { 63 | FireAuth.facebookLogin(); 64 | } 65 | 66 | googleLogin() { 67 | FireAuth.googleLogin(); 68 | } 69 | 70 | logout() { 71 | FireAuth.logout(); 72 | } 73 | 74 | update () => { 75 | FireAuth.update({ 76 | firstName: this.state.firstName, 77 | lastName: this.state.lastName 78 | }).then(() => { 79 | ... 80 | }).catch(err => { 81 | ... 82 | }); 83 | } 84 | 85 | resetPassword () => { 86 | FireAuth.resetPassword(this.state.email) 87 | .then(() => { 88 | ... 89 | }) 90 | .catch(err => { 91 | ... 92 | }); 93 | } 94 | 95 | updatePassword () => { 96 | FireAuth.updatePassword(this.state.password) 97 | .then(() => { 98 | ... 99 | }) 100 | .catch(err => { 101 | ... 102 | }); 103 | } 104 | 105 | ``` 106 | 107 | ## Credits 108 | 109 | https://github.com/magus/react-native-facebook-login 110 | 111 | https://github.com/joonhocho/react-native-google-sign-in 112 | 113 | # Getting Help 114 | 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. 115 | 116 | # Contributing 117 | For more information about contributing PRs, please see our Contribution Guidelines. 118 | 119 | 120 | # Get in touch 121 | If you have any questions about our projects you can email projects@solidstategroup.com. 122 | -------------------------------------------------------------------------------- /auth.js: -------------------------------------------------------------------------------- 1 | import { FBLoginManager } from 'react-native-facebook-login'; 2 | import GoogleSignIn from 'react-native-google-sign-in'; 3 | 4 | const Facebook = { 5 | login: (permissions) => { 6 | return new Promise((resolve, reject) => { 7 | FBLoginManager.loginWithPermissions(permissions || ['email'], (error, data) => { 8 | if (!error) { 9 | resolve(data.credentials.token); 10 | } else { 11 | reject(error); 12 | } 13 | }); 14 | }); 15 | }, 16 | logout: () => { 17 | return new Promise((resolve, reject) => { 18 | FBLoginManager.logout((error, data) => { 19 | if (!error) { 20 | resolve(true); 21 | } else { 22 | reject(error); 23 | } 24 | }); 25 | }); 26 | } 27 | } 28 | 29 | const Google = { 30 | configure: (options) => { 31 | GoogleSignIn.configure(options); 32 | }, 33 | login: () => { 34 | return new Promise((resolve, reject) => { 35 | GoogleSignIn.signInPromise() 36 | .then((user) => { 37 | resolve(user.accessToken); 38 | }) 39 | .catch((err) => { 40 | reject(err); 41 | }) 42 | .done(); 43 | }); 44 | }, 45 | logout: () => { 46 | return new Promise((resolve, reject) => { 47 | GoogleSignIn.signOutPromise() 48 | .then(() => { 49 | resolve(true); 50 | }) 51 | .catch((err) => { 52 | reject(err); 53 | }); 54 | }); 55 | } 56 | } 57 | 58 | const Auth = {Facebook, Google}; 59 | 60 | export default Auth; 61 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import * as firebase from 'firebase'; 2 | import Auth from './auth'; 3 | 4 | const FireAuth = class { 5 | user = null; 6 | profile = null; 7 | onUserChange = null; 8 | onLogout = null; 9 | onEmailVerified = null; 10 | onLogin = null; 11 | onError = null; 12 | 13 | init(googleConfig) { 14 | Auth.Google.configure(googleConfig); 15 | } 16 | 17 | setup = (onLogin, onUserChange, onLogout, onEmailVerified, onError) => { 18 | this.onUserChange = onUserChange; 19 | this.onLogout = onLogout; 20 | this.onEmailVerified = onEmailVerified; 21 | this.onLogin = onLogin; 22 | this.onError = onError; 23 | 24 | firebase.auth().onAuthStateChanged((user)=> { 25 | 26 | if (user) { 27 | // Determine if user needs to verify email 28 | var emailVerified = !user.providerData || !user.providerData.length || user.providerData[0].providerId != 'password' || user.emailVerified; 29 | 30 | // Upsert profile information 31 | var profileRef = firebase.database().ref(`profiles/${user.uid}`); 32 | profileRef.update({ emailVerified: emailVerified, email: user.email }); 33 | 34 | profileRef.on('value', (profile)=> { 35 | const val = profile.val(); 36 | 37 | // Email become verified in session 38 | if (val.emailVerified && (this.profile && !this.profile.val().emailVerified)) { 39 | this.onEmailVerified && this.onEmailVerified(); 40 | } 41 | 42 | if (!this.user) { 43 | this.onLogin && this.onLogin(user, val); // On login 44 | } else if (val) { 45 | this.onUserChange && this.onUserChange(user, val); // On updated 46 | } 47 | 48 | this.profile = profile; // Store profile 49 | this.user = user; // Store user 50 | }); 51 | 52 | } else { 53 | this.profile = null; 54 | this.user = null; // Clear user and logout 55 | this.onLogout && this.onLogout(); 56 | } 57 | 58 | }); 59 | } 60 | 61 | login = (email, password) => { 62 | try { 63 | firebase.auth().signInWithEmailAndPassword(email, password) 64 | .catch((err) => this.onError && this.onError(err)); 65 | } catch (e) { 66 | this.onError && this.onError(e); 67 | } 68 | } 69 | 70 | loginAnonymously = () => { 71 | try { 72 | firebase.auth().signInAnonymously() 73 | .catch((err) => this.onError && this.onError(err)); 74 | } catch (e) { 75 | this.onError && this.onError(e); 76 | } 77 | } 78 | 79 | register = (username, password) => { 80 | try { 81 | firebase.auth().createUserWithEmailAndPassword(username, password) 82 | .then((user)=> { 83 | user.sendEmailVerification(); 84 | }) 85 | .catch((err) => this.onError && this.onError(err)); 86 | } catch (e) { 87 | this.onError && this.onError(e); 88 | } 89 | } 90 | 91 | resendVerification = () => { 92 | this.user.sendEmailVerification(); 93 | } 94 | 95 | facebookLogin = (permissions) => { 96 | Auth.Facebook.login(permissions) 97 | .then((token) => ( 98 | firebase.auth() 99 | .signInWithCredential(firebase.auth.FacebookAuthProvider.credential(token)) 100 | )) 101 | .catch((err) => this.onError && this.onError(err)); 102 | } 103 | 104 | googleLogin = () => { 105 | Auth.Google.login() 106 | .then((token) => ( 107 | firebase.auth() 108 | .signInWithCredential(firebase.auth.GoogleAuthProvider.credential(null, token)) 109 | )) 110 | .catch((err) => this.onError && this.onError(err)); 111 | } 112 | 113 | logout = () => { 114 | firebase.auth().signOut(); 115 | } 116 | 117 | update = (data) => { 118 | var profileRef = firebase.database().ref(`profiles/${this.user.uid}`); 119 | return profileRef.update(data); 120 | } 121 | 122 | resetPassword = (email) => { 123 | firebase.auth().sendPasswordResetEmail(email); 124 | } 125 | 126 | updatePassword = (password) => { 127 | this.user.updatePassword(password); 128 | } 129 | 130 | linkWithGoogle = () => { 131 | // @TODO 132 | } 133 | 134 | linkWithFacebook = () => { 135 | // @TODO 136 | } 137 | 138 | linkWithEmail = () => { 139 | // @TODO 140 | } 141 | }; 142 | 143 | export default new FireAuth(); 144 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-firebase-auth", 3 | "version": "1.0.1", 4 | "description": "Simplified Firebase authentication with social platform support", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "react-native", 11 | "auth", 12 | "social", 13 | "facebook", 14 | "login", 15 | "google", 16 | "firebase" 17 | ], 18 | "author": "SSG", 19 | "license": "ISC", 20 | "peerDependencies": { 21 | "firebase": ">=3.0.0", 22 | "react-native": ">=0.30.0", 23 | "react-native-facebook-login": ">=1.4.0", 24 | "react-native-google-sign-in": ">=0.0.8" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "git+https://github.com/SolidStateGroup/react-native-firebase-auth.git" 29 | }, 30 | "bugs": { 31 | "url": "https://github.com/SolidStateGroup/react-native-firebase-auth/issues" 32 | }, 33 | "homepage": "https://github.com/SolidStateGroup/react-native-firebase-auth#readme" 34 | } 35 | --------------------------------------------------------------------------------