amaurymartiny amaurymartiny - 2 months ago 20
React JSX Question

Dispatch action on Auth0's lock.on('authenticated') event

I want to implement the new Auth0 Lock 10 in my React/Redux app.

I've checked on the internet, but nothing matches my question. There's a tutorial here, but it uses the Popup mode instead of the Redirect (default now) mode. Another one parses the url, which is useless in Lock 10.

Here's the flow:

  • The Auth0Lock gets instantiated when my app starts

  • When the user clicks on the login button, it shows the Lock widget (
    ) and dispatches LOGIN_REQUEST

  • The lock does its authentication on (redirects out of my localhost)

  • Redirect back to my localhost after successful login, the Auth0Lock get instantiated again

  • I wait for an
    event to dispatch LOGIN_SUCCESS

And here is my actions/index.js code:

import Auth0Lock from 'auth0-lock'

export const LOGIN_ERROR = 'LOGIN_ERROR'

function loginRequest() {
return {

function loginSuccess(profile) {
return {

function loginError(error) {
return {

// import AuthService to deal with all the actions related to auth
const lock = new Auth0Lock('secret', 'secret', {
auth: {
redirectUrl: 'http://localhost:3000/callback',
responseType: 'token'

lock.on('authenticated', authResult => {
console.log('Im authenticated')
return dispatch => {
return dispatch(loginSuccess({}))

lock.on('authorization_error', error => {
return dispatch => dispatch(loginError(error))

export function login() {
return dispatch => {return dispatch(loginRequest())}

Now when I click on the login button, redux logger shows me LOGIN_REQUEST action dispatched, I see the lock widget, I can login, it redirects to then back to my
with a pretty token. Everything is fine, I see the
Im authenticated
message in my console, but redux logger doesn't show me that the LOGIN_SUCCESS action has been dispatched.

I'm new to Redux, and I guess I'm missing one thing, but I cannot get grab of it. Thanks!


I finally put in inside actions.js, I created a new function called checkLogin()

// actions.js

const authService = new AuthService(process.env.AUTH0_CLIENT_ID, process.env.AUTH0_DOMAIN)

// Listen to authenticated event from AuthService and get the profile of the user
// Done on every page startup
export function checkLogin() {
  return (dispatch) => {
    // Add callback for lock's `authenticated` event
    authService.lock.on('authenticated', (authResult) => {
      authService.lock.getProfile(authResult.idToken, (error, profile) => {
        if (error)
          return dispatch(loginError(error))
        AuthService.setToken(authResult.idToken) // static method
        AuthService.setProfile(profile) // static method
        return dispatch(loginSuccess(profile))
    // Add callback for lock's `authorization_error` event
    authService.lock.on('authorization_error', (error) => dispatch(loginError(error)))

And in the constructor of my App component, I call it

import React from 'react'
import HeaderContainer from '../../containers/HeaderContainer'

class App extends React.Component {
  constructor(props) {
    this.props.checkLogin() // check is Auth0 lock is authenticating after login callback

  render() {
        <HeaderContainer />

App.propTypes = {
  children: React.PropTypes.element.isRequired,
  checkLogin: React.PropTypes.func.isRequired

export default App

See here for full source code: