How to redirect to some other route after successf

2019-07-04 15:18发布

问题:

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" />);
        }
    }
}


标签: react-admin