Vue.js 2 和 auth0 身份验证导致 'nonce'

Vue.js 2 and auth0 authentication resulting with 'nonce'

我正在尝试在我的 Vue.js 2 应用程序中实施 auth0

我按照这个 link 来实现 auth0 锁: https://github.com/auth0-samples/auth0-vue-samples/tree/master/01-Login

这是我在Login.vue中的应用:

HTML:

<div v-show="authenticated">
    <button @click="logout()">Logout</button>
</div>
<div v-show="!authenticated">
    <button @click="login()">Login</button>
</div>

Javascript:

  function checkAuth() {
    return !!localStorage.getItem('id_token');
  }

  export default {
    name: 'login',
    data() {
      return {
        localStorage,
        authenticated: false,
        secretThing: '',
        lock: new Auth0Lock('clientId', 'domain')
      }
    },
    events: {
      'logout': function() {
        this.logout();
      }
    },
    mounted() {
      console.log('mounted');
      var self = this;
      Vue.nextTick(function() {
        self.authenticated = checkAuth();
        self.lock.on('authenticated', (authResult) => {
          console.log(authResult);
          console.log('authenticated');
          localStorage.setItem('id_token', authResult.idToken);
          self.lock.getProfile(authResult.idToken, (error, profile) => {
            if (error) {
              console.log(error);
              return;
            } else {
              console.log('no error');
            }
            localStorage.setItem('profile', JSON.stringify(profile));
            self.authenticated = true;
          });
        });
        self.lock.on('authorization_error', (error) => {
          console.log(error);
        });
    });
    },
    methods: {
      login() {
        this.lock.show();
      },
      logout() {
        localStorage.removeItem('id_token');
        localStorage.removeItem('profile');
        this.authenticated = false;
      }
    }
  }

我很确定它已经起作用了,但突然间它不再起作用了。

我在 auth0 中定义的回调:http://127.0.0.1:8080/#/backend/login 这也是我在浏览器中打开登录的方式。

当我登录它时,我只在我的 localStorage 中得到这个:

Key: com.auth0.auth.14BK0_jsJtUZMxjiy~3HBYNg27H4Xyp

Value: {"nonce":"eKGLcD14uEduBS-3MUIQdupDrRWLkKuv"}

我也被重定向到 http://127.0.0.1:8080/#/,所以我没有看到任何网络请求。

有人知道问题出在哪里吗? 我 运行 来自 auth0 的演示和我的 Domain/Client 并且它没有任何问题。

很明显,我的控制台没有收到任何错误。

经过研究,我终于找到了问题的答案。

它不起作用的原因是因为我的 vue-router 没有使用 HTML5 历史模式 (http://router.vuejs.org/en/essentials/history-mode.html).

为了让它在没有历史模式的情况下工作,我不得不在我的锁定选项中禁用重定向并禁用自动解析哈希:

lock: new Auth0Lock(
    'clientId', 
    'domain', { 
         auth: { 
             autoParseHash: false, 
             redirect: false 
         }
    }
)

参考:https://github.com/auth0/lock