• Stars
    star
    224
  • Rank 177,792 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel SDK for Auth0 Authentication and Management APIs.

Auth0 Laravel SDK

Build Status Code Coverage Total Downloads License

The Auth0 Laravel SDK is a PHP package that integrates Auth0 into your Laravel application. It includes no-code user authentication, extensive Management API support, permissions-based routing access control, and more.

Requirements

Your application must use a supported Laravel version, and your host environment must be running a supported PHP version. Please review our support policy for more information.

SDK Laravel PHP Supported Until
7.5+ 10.x 8.2+ Feb 2025
8.1+ Nov 2024
7.0+ 9.x 8.2+ Feb 2024
8.1+ Feb 2024
8.0+ Nov 2023

You will also need Composer and an Auth0 account.

Getting Started

The following is our recommended approach to getting started with the SDK. Alternatives are available in our expanded installation guide.

1. Install the SDK

  • For new applications, we offer a quickstart template — a version of the default Laravel 9 starter project pre-configured for use with the Auth0 SDK.

    composer create-project auth0-samples/laravel auth0-laravel-app && cd auth0-laravel-app
  • For existing applications, you can install the SDK using Composer.

    composer require auth0/login:^7.9 --update-with-all-dependencies

    In this case, you will also need to generate an SDK configuration file for your application.

    php artisan vendor:publish --tag auth0

2. Install the CLI

  1. Install the Auth0 CLI to manage your account from the command line.

    curl -sSfL https://raw.githubusercontent.com/auth0/auth0-cli/main/install.sh | sh -s -- -b .

    Move the CLI to a directory in your PATH to make it available system-wide.

    sudo mv ./auth0 /usr/local/bin

    💡 If you prefer not to move the CLI, simply substitute `auth0` in the CLI steps below with `./auth0`.

    Using Homebrew (macOS)
    brew tap auth0/auth0-cli && brew install auth0
    Using Scoop (Windows)
    scoop bucket add auth0 https://github.com/auth0/scoop-auth0-cli.git
    scoop install auth0
  2. Authenticate the CLI with your Auth0 account. Choose "as a user" if prompted.

    auth0 login

3. Configure the SDK

  1. Register a new application with Auth0.

    auth0 apps create \
      --name "My Laravel Application" \
      --type "regular" \
      --auth-method "post" \
      --callbacks "http://localhost:8000/callback" \
      --logout-urls "http://localhost:8000" \
      --reveal-secrets \
      --no-input \
      --json > .auth0.app.json
  2. Register a new API with Auth0.

    auth0 apis create \
      --name "My Laravel Application API" \
      --identifier "https://github.com/auth0/laravel-auth0" \
      --offline-access \
      --no-input \
      --json > .auth0.api.json
  3. Add the new files to .gitignore.

    echo ".auth0.*.json" >> .gitignore
    Using Windows PowerShell
    Add-Content .gitignore "`n.auth0.*.json"
    Using Windows Command Prompt
    echo .auth0.*.json >> .gitignore

4. Run the Application

Boot the application using PHP's built-in web server.

php artisan serve

Direct your browser to http://localhost:8000 to experiment with the application.

  • Authentication
    Users can log in or out of the application by visiting the /login or /logout routes, respectively.

  • API Authorization
    For simplicity sake, generate a test token using the CLI.

    auth0 test token \
      --audience %IDENTIFIER% \
      --scopes "read:messages"

    Substitute %IDENTIFIER% with the identifier of the API you created in step 3 above.

    Now you can send requests to the /api endpoints of the application, including the token as a header.

    curl --request GET \
      --url http://localhost:8000/api/example \
      --header 'Accept: application/json' \
      --header 'Authorization: Bearer %TOKEN%'

    Substitute %TOKEN% with the test token returned in the previous step.

    Using Windows PowerShell
    Invoke-WebRequest http://localhost:8000/api/example `
      -Headers @{'Accept' = 'application/json'; 'Authorization' = 'Bearer %TOKEN%'}

When you're ready to deploy your application to production, review our deployment guide for best practices and advice on securing Laravel.

Integration Examples

User Authentication

The SDK automatically registers all the necessary routes and authentication services within the web middleware group of your application to enable users to authenticate without requiring you to write any code.

Route Purpose
/login Initiates the authentication flow.
/logout Logs the user out.
/callback Handles the callback from Auth0.

If these routes conflict with your application architecture, you can override this default behavior by adjusting the SDK configuration.


Route Authorization (Access Control)

The SDK automatically registers its authentication and authorization guards within the web and api middleware groups for your Laravel application, respectively.

For web routes, you can use Laravel's auth middleware to require that a user be authenticated to access a route.

Route::get('/private', function () {
  return response('Welcome! You are logged in.');
})->middleware('auth');

For api routes, you can use Laravel's auth middleware to require that a request be authenticated with a valid bearer token to access a route.

Route::get('/api/private', function () {
  return response()->json(['message' => 'Hello! You included a valid token with your request.']);
})->middleware('auth');

In addition to requiring that a user be authenticated, you can also require that the user have specific permissions to access a route, using Laravel's can middleware.

Route::get('/scope', function () {
    return response('You have the `read:messages` permission, and can therefore access this resource.');
})->middleware('auth')->can('read:messages');

Permissions require that RBAC be enabled within your API settings.


Users and Tokens

Laravel's Auth Facade can be used to retrieve information about the authenticated user or token associated with a request.

For routes using the web middleware group in routes/web.php.

Route::get('/', function () {
  if (! auth()->check()) {
    return response('You are not logged in.');
  }

  $user = auth()->user();
  $name = $user->name ?? 'User';
  $email = $user->email ?? '';

  return response("Hello {$name}! Your email address is {$email}.");
});

For routes using the api middleware group in routes/api.php.

Route::get('/', function () {
  if (! auth()->check()) {
    return response()->json([
      'message' => 'You did not provide a token.',
    ]);
  }

  return response()->json([
    'message' => 'Your token is valid; you are authorized.',
    'id' => auth()->id(),
    'token' => auth()?->user()?->getAttributes(),
  ]);
});

Management API Calls

Once you've authorized your application to make Management API calls, you'll be able to engage nearly any of the Auth0 Management API endpoints through the SDK.

Each API endpoint has its own SDK class which can be accessed through the Facade's management() factory method. For interoperability, network responses from the API are returned as PSR-7 messages. These can be converted into native arrays using the SDK's json() method.

For example, to update a user's metadata, you can call the management()->users()->update() method.

use Auth0\Laravel\Facade\Auth0;

Route::get('/colors', function () {
  $colors = ['red', 'blue', 'green', 'black', 'white', 'yellow', 'purple', 'orange', 'pink', 'brown'];

  // Update the authenticated user with a randomly assigned favorite color.
  Auth0::management()->users()->update(
    id: auth()->id(),
    body: [
      'user_metadata' => [
        'color' => $colors[random_int(0, count($colors) - 1)]
      ]
    ]
  );

  // Retrieve the user's updated profile.
  $profile = Auth0::management()->users()->get(auth()->id());

  // Convert the PSR-7 response into a native array.
  $profile = Auth0::json($profile);

  // Extract some values from the user's profile.
  $color = $profile['user_metadata']['color'] ?? 'unknown';
  $name = auth()->user()->name;

  return response("Hello {$name}! Your favorite color is {$color}.");
})->middleware('auth');

All the SDK's Management API methods are documented here.

Documentation

  • Installation — Installing the SDK and generating configuration files.
  • Configuration — Configuring the SDK using JSON files or environment variables.
  • Sessions — Guidance on deciding which Laravel Session API driver to use.
  • Cookies — Important notes about using Laravel's Cookie session driver, and alternative options.
  • Management API — Using the SDK to work with the Auth0 Management API.
  • Users — Extending the SDK to support persistent storage and Eloquent models.
  • Events — Hooking into SDK events to respond to specific actions.
  • Deployment — Deploying your application to production.

You may find the following integration guidance useful:

You may also find the following resources helpful:

Contributions to improve our documentation are welcomed.

QuickStarts

Community

The Auth0 Community is where you can get support, ask questions, and share your projects.

Contributing

We appreciate feedback and contributions to this library. Before you get started, please review Auth0's General Contribution guidelines.

The Contribution Guide contains information about our development process and expectations, insight into how to propose bug fixes and improvements, and instructions on how to build and test changes to the library.

To provide feedback or report a bug, please raise an issue.

Code of Conduct

Participants are expected to adhere to Auth0's Code of Conduct when interacting with this project.

Security

If you believe you have found a security vulnerability, we encourage you to responsibly disclose this and not open a public issue. We will investigate all reports. The Responsible Disclosure Program details the procedure for disclosing security issues.

License

This library is open-sourced software licensed under the MIT license.


Auth0 Logo

Auth0 is an easy-to-implement, adaptable authentication and authorization platform.
To learn more, check out "Why Auth0?"

More Repositories

1

node-jsonwebtoken

JsonWebToken implementation for node.js http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html
JavaScript
17,054
star
2

java-jwt

Java implementation of JSON Web Token (JWT)
Java
5,403
star
3

express-jwt

connect/express middleware that validates a JsonWebToken (JWT) and set the req.user with the attributes
TypeScript
4,396
star
4

jwt-decode

Decode JWT tokens; useful for browser applications.
JavaScript
2,913
star
5

angular2-jwt

Helper library for handling JWTs in Angular apps
TypeScript
2,606
star
6

nextjs-auth0

Next.js SDK for signing in with Auth0
TypeScript
2,009
star
7

angular-jwt

Library to help you work with JWTs on AngularJS
JavaScript
1,259
star
8

lock

Auth0's signin solution
JavaScript
1,121
star
9

go-jwt-middleware

A Middleware for Go Programming Language to check for JWTs on HTTP requests
Go
978
star
10

auth0.js

Auth0 headless browser sdk
JavaScript
949
star
11

auth0-spa-js

Auth0 authentication for Single Page Applications (SPA) with PKCE
TypeScript
900
star
12

auth0-react

Auth0 SDK for React Single Page Applications (SPA)
TypeScript
864
star
13

node-jwks-rsa

A library to retrieve RSA public keys from a JWKS (JSON Web Key Set) endpoint.
JavaScript
824
star
14

node-jws

JSON Web Signatures
JavaScript
698
star
15

angular-storage

A storage library for AngularJS done right
JavaScript
644
star
16

repo-supervisor

Scan your code for security misconfiguration, search for passwords and secrets. 🔍
JavaScript
634
star
17

node-auth0

Node.js client library for the Auth0 platform.
JavaScript
586
star
18

cosmos

🔭 Auth0 Design System
JavaScript
545
star
19

JWTDecode.swift

A JWT decoder for iOS, macOS, tvOS, and watchOS
Swift
545
star
20

nginx-jwt

Lua script for Nginx that performs reverse proxy auth using JWT's
JavaScript
534
star
21

SimpleKeychain

A simple Keychain wrapper for iOS, macOS, tvOS, and watchOS
Swift
488
star
22

react-native-auth0

React Native toolkit for Auth0 API
TypeScript
477
star
23

express-openid-connect

An Express.js middleware to protect OpenID Connect web applications.
JavaScript
464
star
24

auth0-python

Auth0 SDK for Python
Python
445
star
25

JWTDecode.Android

A library to help you decode JWTs for Android
Java
437
star
26

docs

Auth0 documentation
JavaScript
366
star
27

auth0-PHP

PHP SDK for Auth0 Authentication and Management APIs.
PHP
355
star
28

wt-cli

Webtask CLI - all you need is code
JavaScript
322
star
29

auth0.net

.NET client for the Auth0 Authentication & Management APIs.
C#
296
star
30

passport-auth0

Auth0 authentication strategy for Passport.js
JavaScript
283
star
31

rules

Rules are code snippets written in JavaScript that are executed as part of the authentication pipeline in Auth0
JavaScript
283
star
32

react-native-lock

[DEPRECATED] A wrapper of Lock to use with React Native (iOS & Android)
Java
277
star
33

Auth0.swift

Auth0 SDK for Apple platforms
Swift
275
star
34

auth0-java

Java client library for the Auth0 platform
Java
265
star
35

Lock.swift

A Swift & iOS framework to authenticate using Auth0 and with a Native Look & Feel
Swift
251
star
36

auth0-cli

Build, manage and test your Auth0 integrations from the command line
Go
243
star
37

auth0-deploy-cli

The Auth0 Deploy CLI is a tool that helps you manage your Auth0 tenant configuration. It integrates into your development workflows as a standalone CLI or as a node module.
JavaScript
242
star
38

Auth0.Android

Android toolkit for Auth0 API
Kotlin
213
star
39

jwks-rsa-java

Java
194
star
40

ruby-auth0

Ruby toolkit for Auth0 API
Ruby
189
star
41

cxn

cXn: extensible open-source CDN
Ruby
178
star
42

passport-windowsauth

Windows Authentication strategy for Passport.js
JavaScript
175
star
43

auth0-angular

Auth0 SDK for Angular Single Page Applications
TypeScript
175
star
44

terraform-provider-auth0

The Auth0 Terraform Provider is the official plugin for managing Auth0 tenant configuration through the Terraform tool.
Go
161
star
45

styleguide

🖌 Conjunction of design patterns, components and resources used across our products.
Stylus
160
star
46

jwt-handbook-samples

JWT Handbook code samples
JavaScript
143
star
47

Lock.Android

Android Library to authenticate using Auth0 and with a Native Look & Feel
Java
140
star
48

wordpress

WordPress Plugin for Auth0 Authentication
PHP
133
star
49

node-samlp

SAML Protocol support for node (only IdP for now)
JavaScript
129
star
50

go-auth0

Go SDK for the Auth0 Management API.
Go
122
star
51

passport-linkedin-oauth2

Passport Strategy for LinkedIn OAuth 2.0
JavaScript
115
star
52

omniauth-auth0

OmniAuth strategy to login with Auth0
Ruby
112
star
53

symfony

Symfony SDK for Auth0 Authentication and Management APIs.
PHP
110
star
54

node-odata-parser

OData query string parser for node.js.
JavaScript
106
star
55

node-jwa

JSON Web Algorithms
JavaScript
98
star
56

express-jwt-authz

Validate the JWT scope to authorize access to an endpoint
JavaScript
97
star
57

auth0-vue

Auth0 authentication SDK for Vue.js apps
TypeScript
95
star
58

lock-passwordless

Auth0 Lock Passwordless [DEPRECATED]
JavaScript
93
star
59

auth0-aspnetcore-authentication

SDK for integrating Auth0 in ASPNET Core
C#
93
star
60

node-oauth2-jwt-bearer

Monorepo for libraries that protect Node APIs with OAuth2 Bearer JWTs
TypeScript
88
star
61

open-source-template

A template for open source projects at Auth0
85
star
62

auth0-angular2

84
star
63

auth0-oidc-client-net

OIDC Client for .NET Desktop and Mobile applications
C#
84
star
64

auth0-authorization-extension

Auth0 Extension that adds authorization features to your account
JavaScript
82
star
65

react-browserify-spa-seed

Seed / Boilerplate project to create your own SPA using React, Browserify and ReworkCSS
JavaScript
80
star
66

node-baas

Node.js implementation of Bcrypt as a micro service.
JavaScript
79
star
67

password-sheriff

Password policies made easy.
JavaScript
77
star
68

idtoken-verifier

Lightweight RSA JWT verification
JavaScript
76
star
69

sharelock-android

Sharelock Android app
Java
76
star
70

auth0-spring-security-api

Spring Security integration with Auth0 to secure your API with JWTs
Java
76
star
71

ad-ldap-connector

Auth0 AD and LDAP connector
JavaScript
70
star
72

nodejs-msi

Build an MSI Windows Installer for a node.js application using WIX Toolset.
PowerShell
70
star
73

id-generator

Generates random ids with a prefix (a la Stripe)
JavaScript
66
star
74

node-saml

SAML assertion creation for node
JavaScript
65
star
75

webtask-scripts

JavaScript
61
star
76

TouchIDAuth

A library for passwordless authentication using TouchID & JWT
Objective-C
60
star
77

passport-azure-ad-oauth2

OAuth 2.0 authentication Passport strategies for Windows Azure Active Directory
JavaScript
59
star
78

spa-pkce

JavaScript
58
star
79

discourse-plugin

Discourse plugin to authenticate with auth0.
Ruby
58
star
80

auth0-flutter

Auth0 SDK for Flutter
Dart
58
star
81

auth0-multitenant-spa-api-sample

JQuery SPA + Node.js API with multi-tenant support
JavaScript
58
star
82

sandboxjs

Sandbox node.js code like a boss
JavaScript
58
star
83

multitenant-jwt-auth

This sample shows how to implement an API that authenticates using JWTs. It supports mutiple tenants and JWT blacklisting.
JavaScript
54
star
84

coreos-mongodb

CoreOS MongoDB units
52
star
85

shiny-auth0

Auth0 shiny proxy
JavaScript
51
star
86

auth0-cordova

Auth0 integration for Cordova apps
JavaScript
49
star
87

disyuntor

A circuit-breaker implementation for node.js
TypeScript
48
star
88

sharelock-osx

Swift
47
star
89

passport-wsfed-saml2

passport strategy for both WS-fed and SAML2 protocol
JavaScript
47
star
90

php-jwt-example

Php JWT example.
PHP
46
star
91

auth0-aspnet-owin

Auth0 ASP.NET 4.5 Owin/Katana Authentication Handler
JavaScript
46
star
92

webauthn.me

webauthn.me, learn more about the Web Authentication API or try the debugger.
JavaScript
46
star
93

auth0-dotnet-templates

Auth0 Templates for .NET
C#
44
star
94

auth0-java-mvc-common

Contains common helper classes and api client logic that are used across our Java MVC libraries
Java
43
star
95

auth0-custom-password-reset-hosted-page

An example on how to do a custom reset password hosted page.
HTML
41
star
96

express-oauth2-bearer

Experimental Middleware for express.js to validate access tokens.
JavaScript
40
star
97

magic

Auth0 Cryptography Toolkit
JavaScript
40
star
98

single-page-app-seed

A very opinionated seed for creating Single Page Apps that uses NO framework at all. Just a bunch of component libs
JavaScript
39
star
99

kbd

Styles for <kbd> tags.
HTML
37
star
100

webtask-workshop

36
star