Skip to content
Advertisement

How can I redirect newly created users to their “logged-in” homepage?

I am running a Django Rest backend with a React front-end.

JSON data during user registration is being pushed per normal. When users submit the register form, they are sent to a login page. I realize how annoying this is for the users, however I’m not so sure how automatically send the user to the logged-in version of the homepage.

I would like to accomplish this in the frontend (I have multiple ways to verify, thanks to social logins, so just seems easier to code this once in the front-end for all methods of registration)

register.js post

you can see below I push the user to /login, however I just want to push them to /home in an already verified and authorized state.

JavaScript

I have access_tokens and refresh_tokens generated by Django, which I can access thanks to my axios instances.

my main axios.js

JavaScript

I think I can copy some code from my login.js file to get users tokens directly?

login.js

JavaScript

Sorry if things seem a little obscure, I’m new to react, I appreciate any type of assistance.

Advertisement

Answer

First, You need to manage states of users (logged in or not). So you can use [Redux][1] for manage states of users.

Next thing is redirect users to /home page. React in default only support single page web applications. So you can’t use window.location.href.You need to use [React-Router][2] for do that.

If you need example, see:: https://github.com/krypto-i9/fixbit/tree/main/src. (this repo use redux to manage user states and react-router for route between pages)
[1]: https://redux.js.org/introduction/getting-started [2]: https://reactrouter.com/web/guides/quick-start

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement