5

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

2 Answers2

4

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' });
    }
    // ...
};
Kmaschta
  • 2,369
  • 1
  • 18
  • 36
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" />);
        }
    }
}
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
  • I suppose the redirect-to url is unknown at build time. What he asks is for redirecting to any url that the user pasted on the address bar, right after login. – Thanasis Ioannidis Apr 20 '20 at 14:58
  • this will never work since pasting a url in browser will do a full reload of the application – Michail Michailidis Apr 21 '20 at 11:39
  • What is wrong with a full reload? After full reload the app should read the url. If it is not authenticated, then redirect to login page, and after login should redirect to the url it read in the beginning. – Thanasis Ioannidis Apr 21 '20 at 12:45
  • @ThanasisIoannidis after url copy paste spa starts again and then router is engaged.. your dashboard resides on '/dashboard' and redirection route should be known in advance - so it can't be dynamic for this particular case - of course you could have custom routes anywhere on the react-router that would work as usual – Michail Michailidis Apr 22 '20 at 08:42