Auth0在登出后没有重定向到所需的URL
创始人
2024-09-22 13:00:20
0

可以使用下面的代码示例来解决此问题:

  1. 首先,我们需要将下面的代码添加到应用程序的配置文件中,将其命名为auth.js。代码如下:
import auth0 from 'auth0-js';

class Auth {
  constructor() {
    this.auth0 = new auth0.WebAuth({
      domain: '{your-auth0-domain}.auth0.com',
      clientID: '{your-auth0-client-id}',
      redirectUri: '{your-app-redirect-url}',
      responseType: 'token id_token',
      scope: 'openid'
    });

    this.login = this.login.bind(this);
    this.logout = this.logout.bind(this);
    this.handleAuthentication = this.handleAuthentication.bind(this);
    this.isAuthenticated = this.isAuthenticated.bind(this);
  }

  login() {
    this.auth0.authorize();
  }

  handleAuthentication() {
    return new Promise((resolve, reject) => {
      this.auth0.parseHash((err, authResult) => {
        if (authResult && authResult.accessToken && authResult.idToken) {
          this.setSession(authResult);
          resolve();
        } else if (err) {
          reject(err);
        }
      });
    })
  }

  setSession(authResult) {
    // Set the time that the access token will expire at
    let expiresAt = JSON.stringify(
      authResult.expiresIn * 1000 + new Date().getTime()
    );
    localStorage.setItem('access_token', authResult.accessToken);
    localStorage.setItem('id_token', authResult.idToken);
    localStorage.setItem('expires_at', expiresAt);
  }

  logout() {
    localStorage.removeItem('access_token');
    localStorage.removeItem('id_token');
    localStorage.removeItem('expires_at');
    this.auth0.logout({
      returnTo: '{your-app-logout-url}',
      clientID: '{your-auth0-client-id}'
    });
  }

  isAuthenticated() {
    let expiresAt = JSON.parse(localStorage.getItem('expires_at'));
    return new Date().getTime() < expiresAt;
  }

}

export default Auth;
  1. 接下来,在应用程序的某个组件中,您可以调用这个Auth类的实例来进行登出操作。代码如下:
import React, { Component } from 'react';
import Auth from './auth.js';

const auth = new Auth();

class Logout extends Component {
  componentDidMount() {
    auth.logout();
  }

  render() {
    return (
      

You have been logged out.

); } } export default Logout;

请注意,我们在auth.js文件中的logout方法中,使用了

相关内容

热门资讯

今天上午!wepokerplu... 今天上午!wepokerplus透视脚本免费,德普之星透视软件免费入口官网,原来存在有挂(有挂存在)...
经调查!wepoker有没有辅... 经调查!wepoker有没有辅助,德扑之星私人局辅助器,都是真的有挂(有挂分享)1.德扑之星私人局辅...
针对!wepoker私人局怎么... 针对!wepoker私人局怎么玩,德扑之星有透视辅助吗,果然真的是有挂(有挂教学)在进入德扑之星有透...
据相关数据显示!wepoker... 据相关数据显示!wepoker私人局辅助器怎么用,德普辅助器怎么用,总是真的有挂(有挂规律)德普辅助...
出现新变化!德州圈脚本,德扑之... 出现新变化!德州圈脚本,德扑之星有透视辅助吗,一贯真的是有挂(详细教程);1、下载好德扑之星有透视辅...
今年以来!wepoker破解器... 今年以来!wepoker破解器激活码,智星德州插件最新版本更新内容详解,一贯真的有挂(揭秘有挂)1、...
攻略辅助挂!wepoker分析... 攻略辅助挂!wepoker分析,智星德州插件2024最新版,原来确实有挂(有挂分享)亲,关键说明,智...
这一问题亟待解决!wepoke... 这一问题亟待解决!wepoker怎么挂底牌,德扑之心免费透视,切实是真的有挂(详细教程)1、德扑之心...
日前!xpoker辅助神器,德... 日前!xpoker辅助神器,德普之星透视免费,总是是真的有挂(有挂教程)1、让任何用户在无需德普之星...
第三方辅助!aapoker怎么... 第三方辅助!aapoker怎么选牌,德扑之心免费透视,确实确实有挂(有挂详情)1、德扑之心免费透视模...