Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

qinmori's avatar
Level 20

How to implement OAuth server with frontend and backend separation?

Out of the box, passport is front-end and back-end together.

But if you want to change to separate the front and back ends.

How will the whole process change?

I've been thinking for a long time but still can't figure it out.

0 likes
3 replies
LaryAI's avatar
Level 58

To implement an OAuth server with a separation between the frontend and backend, you need to follow a few key steps. This involves setting up your backend to handle OAuth logic and your frontend to interact with the backend for authentication purposes. Here's a general approach:

Backend Setup

  1. Install Laravel Passport: First, ensure that your backend is set up with Laravel and install Passport for OAuth2 authentication.

    composer require laravel/passport
    
  2. Migrate and Install Passport: Run the migrations and install Passport.

    php artisan migrate
    php artisan passport:install
    
  3. Configure AuthServiceProvider: In your AuthServiceProvider, add the Passport::routes() method within the boot method.

    use Laravel\Passport\Passport;
    
    public function boot()
    {
        $this->registerPolicies();
    
        Passport::routes();
    }
    
  4. Set API Guard: In config/auth.php, set the API guard to use Passport.

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
    
  5. Create OAuth Clients: Create a client for your frontend application. This can be done using the command line or directly in the database.

    php artisan passport:client --password
    

Frontend Setup

  1. Obtain Access Token: Your frontend will need to obtain an access token by sending a request to the backend with user credentials.

    async function login(username, password) {
        const response = await fetch('https://your-backend.com/oauth/token', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                grant_type: 'password',
                client_id: 'your-client-id',
                client_secret: 'your-client-secret',
                username: username,
                password: password,
                scope: '',
            }),
        });
    
        const data = await response.json();
        return data.access_token;
    }
    
  2. Store and Use Access Token: Once you have the access token, store it securely (e.g., in local storage or a state management library) and use it to authenticate API requests.

    async function fetchUserData(token) {
        const response = await fetch('https://your-backend.com/api/user', {
            headers: {
                'Authorization': `Bearer ${token}`,
            },
        });
    
        const data = await response.json();
        return data;
    }
    

Additional Considerations

  • CORS Configuration: Ensure your backend is configured to handle CORS requests from your frontend domain.

  • Security: Always secure your client secrets and tokens. Consider using HTTPS to encrypt data in transit.

  • Refresh Tokens: Implement refresh tokens to allow users to stay logged in without re-entering credentials.

By following these steps, you can effectively separate your frontend and backend while implementing OAuth authentication using Laravel Passport.

martinbean's avatar

@qinmori Passport will add an OAuth server to your application. You need views to allow a user to approve or deny the authorisation request. These views need to live in the same repository. Once the user has approved the request, you’d then redirect to whatever application needed the OAuth token.

qinmori's avatar
Level 20

Sorry, I didn't make it clear.

What I mean by separation of front-end and back-end is that they have their own projects and the domains will be different.

So you can't use /oauth/authorize directly. I may need to write another one.

Should the other content first determine whether the user is logged in? If logged in, how to tell the front end to display the authorization page? If not logged in, how to tell the front end to display the login page. But can the front end judge this by itself?

Please or to participate in this conversation.