react-native-firebase/lib/modules/auth/index.js

192 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-02-14 11:41:27 +00:00
// @flow
import { NativeModules, NativeEventEmitter } from 'react-native';
import User from './user';
import { Base } from './../base';
import { nativeSDKMissing } from './../../utils';
// providers
2017-03-17 23:40:12 +00:00
import EmailAuthProvider from './providers/Email';
import GoogleAuthProvider from './providers/Google';
import FacebookAuthProvider from './providers/Facebook';
import TwitterAuthProvider from './providers/Twitter';
import GithubAuthProvider from './providers/Github';
2017-02-14 11:41:27 +00:00
2017-03-02 13:09:41 +00:00
const FirebaseAuth = NativeModules.RNFirebaseAuth;
const FirebaseAuthEvt = FirebaseAuth && new NativeEventEmitter(FirebaseAuth);
2017-02-14 11:41:27 +00:00
export default class Auth extends Base {
_user: User | null;
2017-02-14 11:41:27 +00:00
_authResult: AuthResultType | null;
authenticated: boolean;
constructor(firebase: Object, options: Object = {}) {
super(firebase, options);
if (!FirebaseAuth) {
return nativeSDKMissing('auth');
}
2017-02-14 11:41:27 +00:00
this._user = null;
this._authResult = null;
this.authenticated = false;
// start listening immediately for auth changes
FirebaseAuthEvt.addListener('onAuthStateChanged', this._onAuthStateChanged.bind(this));
FirebaseAuth.addAuthStateListener();
2017-02-14 11:41:27 +00:00
}
/**
* Internal auth changed listener
* @param auth
* @param emit
2017-02-14 11:41:27 +00:00
* @private
*/
2017-03-17 23:40:12 +00:00
_onAuthStateChanged(auth: AuthResultType, emit: boolean = true) {
2017-02-14 11:41:27 +00:00
this._authResult = auth;
this.authenticated = auth ? auth.authenticated || false : false;
if (auth && auth.user && !this._user) this._user = new User(this, auth);
else if ((!auth || !auth.user) && this._user) this._user = null;
else if (this._user) this._user._updateValues(auth);
if (emit) this.emit('onAuthStateChanged', this._user);
return auth ? this._user : null;
}
/**
* Remove auth change listener
* @param listener
*/
_offAuthStateChanged(listener: Function) {
this.log.info('Removing onAuthStateChanged listener');
this.removeListener('onAuthStateChanged', listener);
}
/**
* Intercept all user actions and send their results to
* auth state change before resolving
* @param promise
* @returns {Promise.<TResult>|*}
* @private
*/
_interceptUserValue(promise) {
return promise.then((result) => {
if (!result) return this._onAuthStateChanged(null, false);
if (result.user) return this._onAuthStateChanged(result, false);
2017-03-17 23:40:12 +00:00
if (result.uid) return this._onAuthStateChanged({ authenticated: true, user: result }, false);
return result;
});
2017-02-14 11:41:27 +00:00
}
/*
* WEB API
*/
/**
* Listen for auth changes.
* @param listener
*/
onAuthStateChanged(listener: Function) {
this.log.info('Creating onAuthStateChanged listener');
this.on('onAuthStateChanged', listener);
2017-06-13 01:09:12 +00:00
if (this._authResult) listener(this._user || null);
2017-02-14 11:41:27 +00:00
return this._offAuthStateChanged.bind(this, listener);
}
/**
* Sign the current user out
* @return {Promise}
2017-02-14 11:41:27 +00:00
*/
signOut(): Promise<null> {
return this._interceptUserValue(FirebaseAuth.signOut());
2017-02-14 11:41:27 +00:00
}
/**
* Sign a user in anonymously
* @return {Promise} A promise resolved upon completion
*/
signInAnonymously(): Promise<Object> {
return this._interceptUserValue(FirebaseAuth.signInAnonymously());
}
2017-02-14 11:41:27 +00:00
/**
* Create a user with the email/password functionality
* @param {string} email The user's email
* @param {string} password The user's password
* @return {Promise} A promise indicating the completion
*/
createUserWithEmailAndPassword(email: string, password: string): Promise<Object> {
return this._interceptUserValue(FirebaseAuth.createUserWithEmailAndPassword(email, password));
2017-02-14 11:41:27 +00:00
}
/**
* Sign a user in with email/password
* @param {string} email The user's email
* @param {string} password The user's password
* @return {Promise} A promise that is resolved upon completion
*/
signInWithEmailAndPassword(email: string, password: string): Promise<Object> {
return this._interceptUserValue(FirebaseAuth.signInWithEmailAndPassword(email, password));
2017-02-14 11:41:27 +00:00
}
/**
* Sign the user in with a custom auth token
* @param {string} customToken A self-signed custom auth token.
* @return {Promise} A promise resolved upon completion
*/
signInWithCustomToken(customToken: string): Promise<Object> {
return this._interceptUserValue(FirebaseAuth.signInWithCustomToken(customToken));
2017-02-14 11:41:27 +00:00
}
/**
* Sign the user in with a third-party authentication provider
* @return {Promise} A promise resolved upon completion
*/
signInWithCredential(credential: CredentialType): Promise<Object> {
return this._interceptUserValue(FirebaseAuth.signInWithCredential(credential.provider, credential.token, credential.secret));
2017-02-14 11:41:27 +00:00
}
/**
* Send reset password instructions via email
* @param {string} email The email to send password reset instructions
*/
sendPasswordResetEmail(email: string): Promise<Object> {
return FirebaseAuth.sendPasswordResetEmail(email);
2017-02-14 11:41:27 +00:00
}
/**
* Get the currently signed in user
* @return {Promise}
*/
getCurrentUser(): Promise<Object> {
return this._interceptUserValue(FirebaseAuth.getCurrentUser());
2017-02-14 11:41:27 +00:00
}
/**
* Returns a list of authentication providers that can be used to sign in a given user (identified by its main email address).
* @return {Promise}
*/
fetchProvidersForEmail(email: string): Promise<Array<String>> {
return FirebaseAuth.fetchProvidersForEmail(email);
}
2017-02-14 11:41:27 +00:00
/**
* Get the currently signed in user
* @return {Promise}
*/
get currentUser(): User | null {
2017-02-14 11:41:27 +00:00
return this._user;
}
get namespace(): string {
return 'firebase:auth';
}
}
2017-03-27 18:11:26 +00:00
export const statics = {
GoogleAuthProvider,
EmailAuthProvider,
FacebookAuthProvider,
TwitterAuthProvider,
GithubAuthProvider,
};