auth0.js

PHOTO EMBED

Tue Feb 27 2024 04:47:47 GMT+0000 (Coordinated Universal Time)

Saved by @minhgiau998

import auth0 from 'auth0-js'
class Auth0JS {
  constructor(config) {
    this.config = config
    this.webAuth = new auth0.WebAuth({
      domain: config.auth0.domain,
      clientID: config.auth0.clientID,
      redirectUri: config.baseURL,
      responseType: 'id_token',
      scope: 'openid profile email',
    })
  }

  login(language) {
    if (language) {
      const uiLocales = language === 'zh' ? 'zh-CN' : language
      this.webAuth.authorize({ ui_locales: uiLocales, responseType: 'code', redirectUri: `${this.config.apiURL}/login/auth0` })
    } else this.webAuth.authorize()
  }
  
  signup(lang) {
    const uiLocales = lang === 'zh' ? 'zh-CN' : lang
    if (uiLocales)
      this.webAuth.authorize({
        ui_locales: uiLocales,
        screen_hint: 'signup',
        redirectUri: `${this.config.apiURL}/login/auth0`
      })
    else
      webAuth.authorize({
        screen_hint: 'signup',
        redirectUri: `${this.config.apiURL}/login/auth0`
      })
  }

  logout() {
    this.webAuth.logout({
      returnTo: this.config.baseURL, // Allowed logout URL listed in dashboard
      clientID: this.config.auth0.clientID, // Your client ID process.env.AUTH0_CLIENT_ID
    })
  }

  checkSession() {
    return new Promise((resolve, reject) => {
      this.webAuth.checkSession({}, async (err, authResult) => {
        if (authResult) {
          resolve(true)
        } else {
          resolve(false)
        }
      })
    })
  }
}

export default function (context, inject) {
  const runtimeConfig = context.$config
  const auth0JS = new Auth0JS(runtimeConfig)
  inject('auth0JS', auth0JS)
}
content_copyCOPY