When to Use
Use the authorization code grant when:- You are building an external customer portal that needs to display or modify user-specific data.
- A third-party application needs to act as a specific user (e.g. a mobile app or a partner platform).
- You need to respect per-user permissions and data visibility rules in external contexts.
How It Works
User initiates authorization
The external application redirects the user to
/oauth/authorize with the app’s client_id and a redirect_uri.User logs in (if needed) and consents
Core’s login page handles authentication. After login, the user sees a consent screen listing the app requesting access. They can Authorize or Deny.
Authorization code is issued
After consent, the user is redirected back to the
redirect_uri with a one-time code parameter.Code is exchanged for a token
The external application exchanges the code for an access token. The token is scoped to the user.
Setting Up
1. Create an App Client
In Developer → Apps, create a new app client with Authorization code as the grant type. Add your application’s callback URL(s) as Redirect URIs.2. Redirect the User to Authorize
Construct the authorization URL and redirect the user:3. Handle the Callback
After the user consents, your application receives acode and state at the redirect_uri:
state matches what you generated, then exchange the code for a token:
4. Make Authenticated Requests
Use the token on/api/v1/* routes:
Defining API Routes
User-authenticated routes are defined inroutes/api.php under the /v1 prefix with the CoreApiAuthenticate middleware, which accepts both user access tokens and client credentials tokens:
routes/api.php
auth()->user() returns the authenticated User model (when using a user token) or null (when using a client credentials token). Use Auth::guard('api')->client() to retrieve the app client when applicable.
The Consent Screen
When a user visits/oauth/authorize, they see a consent screen that:
- Displays the requesting app’s name.
- Lists any requested scopes (if configured).
- Provides Authorize and Deny buttons.
config('brand.icon') and is styled to match your application.
Refreshing Tokens
Use therefresh_token grant to obtain a new access token without requiring user re-authorization:
cURL