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

troccoli's avatar

OAuth2 provider and Laravel Breeze

I am working on a project to refactor our apps, and one par of it is a centralised identity and access management system. A user will be able to login (in any of the apps) using a username/password combination, or either Microsoft or Google. Another requirement is, of course, that once a user is logged in in one app the are logged in in all apps. I think I can do this with a shared DB session, but that's not the issue here.

So, my plan is to build the CIAM (Customer Identity and Access Management) as a Laravel app using Passport. Then each of the separate apps will be a Laravel app as well, but using Breeze (API only).

The way how I envisage this to work is:

  • in one of the app the user clicks on login
  • the app sends a request to the CIAM: POST oauth/authorize
  • CIAM then shows the login form with button for Microsoft and Google
  • once the user is authenticated (however they choose) CIAM calls the app's callback with a authorisation token
  • the app sends a request to the CIAM with the necessary scopes: POST oauth/scopes (I think)
  • CIAM responds with a JWT
  • the apps redirects the user to where they should go

As you can see I would not want to store the users in the app's database, so I guess I will need to create my own user provider, as explained here: https://laravel.com/docs/11.x/authentication#adding-custom-user-providers.

My very first question would be, am I right in using Breeze for the apps?

And secondly, is that flow how it should work?

Finally, am I correct is thinking I need a custom user provider?

0 likes
1 reply
LaryAI's avatar
Level 58

To address your questions regarding the integration of OAuth2 with Laravel Breeze and the setup of a centralized identity and access management system using Laravel Passport, let's break down the solution:

1. Using Laravel Breeze for the Apps

Is using Breeze appropriate?

Laravel Breeze is a simple and minimal implementation of Laravel's authentication features. It provides a basic starting point for authentication, which can be extended to support OAuth2. Since you are planning to use OAuth2 for authentication via a centralized CIAM system, Breeze can be a good choice for the individual apps as it allows you to quickly set up the basic authentication scaffolding. However, you will need to extend it to handle OAuth2 flows.

2. Proposed Authentication Flow

Is the flow correct?

Your proposed flow is generally correct for an OAuth2 implementation. Here's a refined version of the flow:

  1. User Initiates Login: The user clicks on a login button in one of the apps.
  2. Redirect to CIAM: The app redirects the user to the CIAM's authorization endpoint (/oauth/authorize).
  3. CIAM Handles Authentication: The CIAM app presents a login form with options for Microsoft and Google.
  4. Authorization Code Grant: Once authenticated, CIAM redirects back to the app with an authorization code.
  5. Token Exchange: The app exchanges the authorization code for an access token (and optionally a refresh token) by making a request to the CIAM's token endpoint (/oauth/token).
  6. Access Resource: The app uses the access token to authenticate API requests to the CIAM or other services.
  7. User Session Management: The app manages the user session using the access token, possibly storing it in a session or a cookie.

3. Custom User Provider

Do you need a custom user provider?

Yes, if you do not want to store users in the app's database and instead rely on the CIAM for user information, you will need a custom user provider. This custom provider will authenticate users based on the access token and fetch user details from the CIAM.

Here's a basic outline of how you might implement a custom user provider:

Conclusion

  • Breeze: Suitable for setting up basic authentication scaffolding.
  • Flow: Your flow is mostly correct; ensure you handle token exchange and session management properly.
  • Custom User Provider: Necessary if you want to avoid storing user data in each app's database.

By following these steps, you should be able to implement a centralized authentication system using Laravel Passport and integrate it with your Laravel apps using Breeze.

Please or to participate in this conversation.