0

I'm trying to develop a react-spa & django api (DRF), and use the django inbuild session authentication system. For this, both apps while use the same domain, react build files will be served as django static files.

I'm also using the dj-rest-auth library to generate the endpoints for each authentication action. After a bit of work I've been able to correctly create the login/logout logic and use the credentials to fetch the django endpoints.

However, I don't know how to check if the user is already logged in from react. Most of the examples I've found on the internet use the 'ProtectedRoutes' component design: a component that will show the child component if you are logged in, otherwise it will redirect you to the login view.

This is what I have tried:

import isAuth from './services/AuthService'
import { Navigate, Outlet } from 'react-router-dom';

const  ProtectedRoutes=(isAuth) =>{
    const auth = isAuth()
    
    return auth ? <Outlet/> : <Navigate to="/app/login"/>

};

export default ProtectedRoutes

But I don't know how to check when the user is logged in: isAuth() function. So I'm happy to hear your suggestions.

0 Answers0