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方法中,使用了

相关内容

热门资讯

透视能赢!呱呱丰城双剑辅助,微... 透视能赢!呱呱丰城双剑辅助,微信小程序微乐辅助器下载(透视)开挂辅助下载细节揭秘;无需打开直接搜索加...
法子开挂"丽水双扣茶... 法子开挂"丽水双扣茶苑辅助"开挂(攻略)辅助脚本(有挂功能)1、下载安装好丽水双扣茶苑辅助,进入游戏...
本来有开挂"安装胡乐... 本来有开挂"安装胡乐辅助脚本"开挂(透视)辅助软件(2026新版教程真的有挂);无需打开直接搜索打开...
指南开挂"红黑大战控... 指南开挂"红黑大战控制系统"开挂(插件)辅助软件(有挂实锤) 【无需打开直接搜索加薇13670430...
透视免费!对联猜猜看辅助器,方... 透视免费!对联猜猜看辅助器,方片十三张辅助(透视)开挂辅助神器AI教程对联猜猜看辅助器ai黑科技系统...
从前有辅助"微信超级... 从前有辅助"微信超级三加一免费辅助"开挂(透视)辅助透视(扑克教程有挂规律)1、下载安装好微信超级三...
窍门开挂"酷玩联盟辅... 【亲,酷玩联盟辅助 这款游戏可以开挂的,确实是有挂的,很多玩家在这款酷玩联盟辅助中打牌都会发现很多用...
透视透视!广西来趣app有挂吗... 透视透视!广西来趣app有挂吗,微乐小程序自建房插件怎么安装(透视)开挂辅助软件德州教程这是一款可以...
最初有辅助"粤友圈有... 粤友圈有挂吗开挂教程视频分享装挂详细步骤在当今的网络游戏中,粤友圈有挂吗作为一种经典的娱乐方式,吸引...
窍门辅助"山西奇迹打... 窍门辅助"山西奇迹打锅子辅助"开挂(安装)辅助脚本(证实有挂)这是一款可以让一直输的玩家,快速成为一...