auth0 by okta problem login in my db with laravel breeze error Invalid state

18 Views Asked by At

I don't know what to do anymore, I'll explain the problem to you. I have a client who requires auth0 okta as login. I would like to integrate it with Laravel 9 and with Breeze since I have to manage multiple types of users, for example: Admin and user. The problem is the cookieSecret which is invalid state. Can anyone help me? here is the code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth0\SDK\Auth0;
use Auth0\SDK\Configuration\SdkConfiguration;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class Auth0Controller extends Controller
{
    public $auth0;

    public function __construct()
    {
        $randomString = Str::random(15);

        $configuration = new SdkConfiguration(
            domain: '***',
            clientId: '***',
            clientSecret: '***',
            cookieSecret: $randomString,
            redirectUri: 'http://localhost:8000/callback'
        );

        $this->auth0 = new Auth0($configuration);
    }

    public function auth0()
    {
        $session = $this->auth0->getCredentials();

        if (null === $session || $session->accessTokenExpired) {

            header('Location: ' . $this->auth0->login());
            exit;
        }
    }

    public function callback(Request $request)
    {
        $input = $request->all();

        if (null !== $this->auth0->getExchangeParameters()) {
            $this->auth0->exchange();
        }

        $user = $this->auth0->getCredentials()?->user;

        $localuser = $user['email'];

        if (User::where('email', $localuser)->exists()) {

            $authenticatedUser = User::where('email', $localuser)->first();

            Auth::login($authenticatedUser);

            $request->session()->regenerate();

            $url = '';

            if ($request->user()->task_app === 'Admin') {

                if ($request->user()->primo_accesso === 'NO') {
                    $url = RouteServiceProvider::HOME;
                } elseif ($request->user()->primo_accesso === 'SI') {
                    $url = RouteServiceProvider::PRIMO;
                };
            } elseif ($request->user()->task_app === 'Viewer') {
                $url = RouteServiceProvider::GEST;
            } elseif ($request->user()->task_app === 'User') {
                if ($request->user()->primo_accesso === 'NO') {
                    $url = RouteServiceProvider::USER;
                } elseif ($request->user()->primo_accesso === 'SI') {
                    $url = RouteServiceProvider::PRIMO;
                };
            } elseif ($request->user()->task_app === 'Supporto') {
                $url = RouteServiceProvider::SUPP;
            }

            return redirect()->intended($url);
        };
    }
}
0

There are 0 best solutions below