Authentication Methods

Authentication data is attached to auth, and errors are attached to authError. You can get them within components like so:

import { connect } from 'react-redux'
import { pathToJS } from 'react-redux-firebase'
@connect(
  // Map state to props
  ({ firebase }) => ({
    authError: pathToJS(firebase, 'authError'),
    auth: pathToJS(firebase, 'auth'),
    profile: pathToJS(firebase, 'profile')
  })
)

If you need access to methods that are not available at the top level, you can access Firebase's Full Auth API using this.props.firebase.auth() or getFirebase().auth().

NOTE

All examples below assume you have wrapped your component using firebaseConnect. This will make this.props.firebase available within your component:

Decorators
import React, { Component } from 'react'
import { firebaseConnect } from 'react-redux-firebase'

@firebaseConnect()
export default class SomeComponent extends Component {
  render() {
    // this.props.firebase contains API
  }
}
No Decorators
import React, { Component } from 'react'
import { firebaseConnect } from 'react-redux-firebase'

class SomeComponent extends Component {
  render() {
    // this.props.firebase contains API
  }
}
export default firebaseConnect()(SomeComponent)

login(credentials)

Parameters
  • credentials (String | Object)
    • String - ref.authWithCustomToken(credentials) is used
    • Object - cases:
      • email and password (runs ref.authWithPassword(credentials)) :
        {
            email: String
            password: String
        }
        
      • provider (runs ref.authWithOAuthPopup(provider) or ref.authWithOAuthRedirect(provider)) :
        {
            provider: "facebook | google | twitter",
            type: "popup | redirect", // popup is default
        }
        
      • provider and token (runs ref.authWithOAuthToken(provider, token)) :
        {
            provider: "facebook | google | twitter",
            token : String
        }
        
Returns

Promise with an object containing profile, user, (also credential if using oAuth provider) in case of success or the error otherwise.

Examples

Email

// Call with info
this.props.firebase.login({
  email: 'test@test.com',
  password: 'testest1'
})

OAuth Provider Redirect

 // Call with info
 this.props.firebase.login({
   provider: 'google',
   type: 'redirect'
 })

OAuth Provider Popup

// Call with info
this.props.firebase.login({
  provider: 'google',
  type: 'popup'
})

Token

// Call with info
this.props.firebase.login('someJWTAuthToken')

createUser(credentials, profile)

Similar to Firebase's ref.createUser(credentials) but with support for automatic profile setup (based on your userProfile config).

Parameters
  • credentials Object

    • credentials.email String - User's email
    • credentials.password String - User's password
    • credentials.signIn String - Whether or not to sign in when user is signing up (defaults to true)
  • profile Object

Examples
const createNewUser = ({ email, password, username }) => {
  this.props.firebase.createUser(
    { email, password },
    { username, email }
  )
}

// Call with info
createNewUser({
  email: 'test@test.com',
  password: 'testest1',
  username: 'tester'
})
Returns

Promise with userData

logout()

Logout from Firebase and delete all data from the store (state.firebase.data and state.firebase.auth are set to null).

Examples
// logout and remove profile and auth from state
firebase.logout()

resetPassword(credentials)

Calls Firebase's firebase.auth().resetPassword(). If there is an error, it is added into redux state under state.firebase.authError, which can be loaded using pathToJS(state.firebase, 'authError').

Examples
firebase.resetPassword({
  email: 'test@test.com',
  password: 'testest1',
  username: 'tester'
})
Parameters
  • credentials Object - Credentials same as described in firebase docs
  • profile Object - if initialized with userProfile support then profile will be saved into ${userProfile}/${auth.uid}
Returns

Promise with user's UID in case of success or the error otherwise. Always authenticate the new user in case of success

confirmPasswordReset(code, newPassword)

Calls Firebase's firebase.auth().confirmPasswordReset(). If there is an error, it is added into redux state under state.firebase.authError, which can be loaded using pathToJS(state.firebase, 'authError').

Examples
firebase.confirmPasswordReset('some reset code', 'myNewPassword')
Parameters
  • code String - Password reset code
  • newPassword String - New password to set for user
Returns

Promise

verifyPasswordResetCode(code)

Verify a password reset code from password reset email.

Calls Firebase's firebase.auth().verifyPasswordResetCode(). If there is an error, it is added into redux state under state.firebase.authError, which can be loaded using pathToJS(state.firebase, 'authError').

Examples
firebase.verifyPasswordResetCode('some reset code')
Parameters
  • code String - Password reset code
Returns

Promise - Email associated with reset code

results matching ""

    No results matching ""