How to redirect to some other route after successful authentication in react-admin

半城伤御伤魂 提交于 2019-12-10 15:25:35

问题


I have been looking for a solution to redirect to specific url after successful authentication in react-admin,

when I paste http://localhost:1234/#/students/sdf2343afs32 on the url if already signed-in then I'm getting the user detail page but if not singed-in and after singing-in it shows home page instead


回答1:


You can customize the redirect URL after login inside the authProvider as explained in the Checking Credentials During Navigation part of the documentation:

// in authProvider.js
import { AUTH_CHECK } from 'react-admin';

export default (type, params) => {
    // ../
    if (type === AUTH_CHECK) {
        return isLogged
            ? Promise.resolve({ redirectTo: '/custom-url' })
            : Promise.reject({ redirectTo: '/no-access' });
    }
    // ...
};



回答2:


Based on https://stackoverflow.com/a/35715159/986160 using react-admin 2.6.2

What worked for me is a custom Dashboard like that (assuming this is your default landing page):

import React, { Component } from 'react';
import { Redirect } from 'react-router';
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
import CardHeader from '@material-ui/core/CardHeader';

export default class Dashboard extends Component {

    render() {
        if (localStorage.getItem("user_role") !== "special_role") {
            return <Card>
                <CardHeader title="Welcome to Dashboard" />
                <CardContent></CardContent>
            </Card>
        }
        else {
            return (<Redirect to="/route/to/redirect" />);
        }
    }
}


来源:https://stackoverflow.com/questions/52060397/how-to-redirect-to-some-other-route-after-successful-authentication-in-react-adm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!