React And Flask-login - Check If User Logged In

118 Views Asked by At

I'm using react 18.2 on my FrontEnd and flask with flask-login on my Backend. It's a RestAPI setup and the authentication will be using sessions.

I implemented the login itself, React sends login requests Flask handles it and puts a session token in browser's cookies.

My question: When user connected I want to show him login_required components but I'm confused how can I check it with React. Also if user closes the browser and returns later.

I thought of a solution: Every time user makes something I will send a request to Flask and check if this user is authenticated and hence connected.

1

There are 1 best solutions below

0
Solomon Shubaev On

My solution (Not sure if it is the best practices): I have a login_required endpoint in flask. I just check on the FrontEnd if the response is OK.

from flask import Blueprint
from flask_login import login_required

auth_bp = Blueprint('auth_api', __name__)


@auth_bp.route('/auth', methods=['GET'])
@login_required
def is_authenticate():
    return ''