At what point during the Auth0 Authentication proc...
# prisma-whats-new
d
At what point during the Auth0 Authentication process are you supposed to run the createUser mutation? Right now I’ve got this in my main App container but I know this has to be the wrong direction.
Copy code
componentWillMount() {
    const auth = this.props.route.auth
    auth.on('profile_updated', (profile) => {
      this.createUser(profile)
    })
  }
n
@dkh you need the accessToken/idToken from Auth0 to create a new user. Here's an example: https://github.com/graphcool-examples/react-apollo-auth0-example
d
Hey @nilan I had that setup, figured it out:
Copy code
// App.js

componentWillMount() {
    const auth = this.props.route.auth
    const user = this.props.data.user
    const auth0IdToken = window.localStorage.getItem('auth0IdToken')

    auth.on('profile_updated', (profile) => {
      if (user || auth0IdToken === null) {
        console.log('user exists')
      } else if (!user && auth0IdToken === null) {
        // user exists, but not logged in. login user?
      }
       else {
         // user does not exits, create please
        this.createUser(profile)
      }
    })
  }
auth
is a helper class that I’m instantiating in my index.js file and passing down through my / route:
Copy code
import { EventEmitter } from 'events'
import { isTokenExpired } from './jwtHelper'
import Auth0Lock from 'auth0-lock'
import { browserHistory } from 'react-router'

export default class AuthService extends EventEmitter {
  constructor(clientId, domain) {
    super()
    // Configure Auth0
    this.lock = new Auth0Lock(clientId, domain, {
      auth: {
        redirectUrl: `${window.location.origin}/`,
        responseType: 'token'
      }
    })
    // Add callback for lock `authenticated` event
    this.lock.on('authenticated', this._doAuthentication.bind(this))
    // Add callback for lock `authorization_error` event
    this.lock.on('authorization_error', this._authorizationError.bind(this))
    // binds login functions to keep this context
    this.login = this.login.bind(this)
  }

  _doAuthentication(authResult){
    // Saves the user token
    this.setToken(authResult.idToken)
    // navigate to the home route
    browserHistory.replace('/')
    // Async loads the user profile data
    this.lock.getProfile(authResult.idToken, (error, profile) => {
      if (error) {
        console.log('Error loading the Profile', error)
      } else {
        this.setProfile(profile)
      }
    })
  }

  _authorizationError(error){
    // Unexpected authentication error
    console.log('Authentication Error', error)
  }

  login() {
    // Call the show method to display the widget.
    this.lock.show()
  }

  loggedIn(){
    // Checks if there is a saved token and it's still valid
    const token = this.getToken()
    return !!token && !isTokenExpired(token)
  }

  setProfile(profile){
    // Saves profile data to localStorage
    localStorage.setItem('profile', JSON.stringify(profile))
    // Triggers profile_updated event to update the UI
    this.emit('profile_updated', profile)
  }

  getProfile(){
    // Retrieves the profile data from localStorage
    const profile = localStorage.getItem('profile')
    return profile ? JSON.parse(localStorage.profile) : {}
  }

  setToken(idToken){
    // Saves user token to localStorage
    localStorage.setItem('auth0IdToken', idToken)
  }

  getToken(){
    // Retrieves the user token from localStorage
    return localStorage.getItem('auth0IdToken')
  }

  logout(){
    // Clear user token and profile data from localStorage
    localStorage.removeItem('auth0IdToken')
    localStorage.removeItem('profile')
    window.location.replace('/')
  }
}
n
great you figured it out 🙂