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

相关内容

热门资讯

开挂辅助神器"pok... 开挂辅助神器"pokemmo脚本辅助器下载"开挂(透视)辅助平台(有挂讲解);打开点击测试直接进入微...
开挂辅助安装"wep... 开挂辅助安装"wepoker游戏下载"开挂(透视)辅助插件(有挂存在);无需打开直接搜索打开薇:13...
开挂辅助脚本"wep... wepoker辅助软件价格 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大...
开挂辅助平台"大菠萝... 您好:大菠萝789辅助器下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
开挂辅助插件"wpk... 开挂辅助插件"wpk辅助软件"开挂(透视)辅助插件(了解有挂)【无需打开直接搜索加薇13670430...
开挂辅助神器"wep... 您好:wepoker透视有没有这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
开挂辅助下载"菠萝德... 开挂辅助下载"菠萝德州透视脚本"开挂(透视)辅助工具(有挂秘笈) >>您好:软件加薇13670430...
开挂辅助插件"约局吧... 约局吧是否有挂开挂教程视频分享装挂详细步骤在当今的网络游戏中,约局吧是否有挂作为一种经典的娱乐方式,...
开挂辅助软件"pok... 开挂辅助软件"pokermaster破解版"开挂(透视)辅助脚本(揭秘有挂);无需打开直接搜索加薇1...
开挂辅助插件"pok... 开挂辅助插件"pokemmo脚本辅助器下载"开挂(透视)辅助脚本(有挂秘籍)>>您好:软件加薇136...