D
D
Damir Shaniyazov2022-03-09 19:31:13
React
Damir Shaniyazov, 2022-03-09 19:31:13

How to redirect user after authentication?

Hello. Tell me how you can redirect the user after authentication to the page that he wanted.

App.jsx

<Route path='/movies/new' element={<AuthWare component={NewMovieForm} />} />


AuthWare.jsx
const AuthWare = ({ component: Component }) => {
    if (!authService.getCurrentUser()) return <Navigate to='/login' />
    return <Component/>;
}


loginForm.jsx button click
doSumbit = async () => {
        try {
            const { data } = this.state;
            await auth.login(data.username, data.password);
            window.location.href = '/';
        } catch (ex) {
            if (ex.response && ex.response.status === 400) {
                const errors = {...this.state.errors};
                errors.username = ex.response.data;
                this.setState({ errors });
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2022-03-09
@firedragon

Add the backUrl parameter to the login form call, in the form redirect the user to this address if it is defined

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question