[Python] Problem of redirect when trying to authenticate user using supabase

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    Hi im trying to create an app that allow user to login using third party services, more specifically i am using supabase. I wrote the code it seem to work, it allows me to login without problems until after the login is dove i get a google error page at this link http://localhost:3000/#access_token=eyJhbGc... instead of being redirected to the correct page.

    supabase: Client = create_client(supabase_url, supabase_key)

    # Decorator personalizzato login_required
    def login_required(f):
    def wrapper(*args, **kwargs):
    if 'user' not in session:
    return redirect(url_for('login'))
    return f(*args, **kwargs)
    return wrapper

    @app.route('/')
    def home():
    if 'user' in session:
    return redirect('/dashboard')
    return '''
    <a href="/login/github">Login with GitHub</a><br>
    <a href="/login/google">Login with Google</a><br>
    <a href="/login/microsoft">Login with Microsoft</a>
    ''' # Pulsanti di login per ogni provider

    @app.route('/login/<provider>')
    def login(provider):
    # Reindirizza l'utente al link di login di Supabase con il provider scelto
    redirect_uri = url_for('callback', _external=True)
    auth_url = f"{supabase_url}/auth/v1/authorize?provider={provider}&redirect_to={redirect_uri}"
    logging.info(f"Redirect URL: {auth_url}") # Aggiungi il log per vedere l'URL di redirect
    return redirect(auth_url)

    @app.route('/callback')
    def callback():
    # Gestione del callback dopo il login con il provider (GitHub, Google, Microsoft)
    code = request.args.get('code')
    if code:
    try:
    # Completa il login utilizzando il codice ricevuto
    auth_response = supabase.auth.exchange_code_for_session(code)
    user = supabase.auth.user()

    # Salva i dati dell'utente nella sessione
    session['user'] = user
    logging.info(f"User info: {user}") # Aggiungi log per vedere i dati dell'utente

    # Salva o aggiorna l'utente nel database
    user_data = {
    'email': user['email'],
    'name': user['user_metadata']['full_name'],
    }

    # Puoi anche inserire o aggiornare l'utente nel database Supabase
    # supabase.table('users').upsert([user_data]).execute()

    return redirect('/dashboard') # Reindirizza alla dashboard dopo il login
    except Exception as e:
    logging.error(f"Error during authentication: {str(e)}") # Log degli errori
    return "Authentication failed", 400
    return "Authentication failed", 400

    @app.route('/dashboard')
    def dashboard():
    if 'user' in session:
    # Mostra un messaggio di benvenuto all'utente loggato
    return f'Sei loggato, {session["user"]["user_metadata"]["full_name"]}! <a href="/logout">Logout</a>'
    return redirect('/') # Se l'utente non è loggato, reindirizza alla home

    @app.route('/logout')
    def logout():
    session.clear()
    return redirect('/')

    if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO) # Imposta il livello di log
    app.run(debug=True, port=5000)


    i tryed to remove cookies, cache, but still got this problem

    Continue reading...

Compartilhe esta Página