This tutorial shows PHP developers how to integrate passkeys into a PHP Laravel app. Besides passkey authentication, session management is also implemented.
Lukas
Created: November 14, 2023
Updated: September 20, 2024
We aim to make the Internet a safer place using passkeys. That's why we want to support developers with tutorials on how to implement passkeys.
1. Introduction: PHP Laravel Passkeys
2. PHP Laravel Project Prerequisites
3. Repository Structure for PHP Laravel Passkey Project
4. Set Up Your PHP Laravel Project
5. Set Up Corbado Passkey Authentication
5.1 Set Up Your Corbado Account and Project
5.3 Create the Homepage to Handle Sign-ups and Logins
5.4 Create a Profile Route using the Corbado PHP SDK
In this blog post, we’ll explore how to implement passkeys for secure authentication using Corbado. We'll cover the setup of the Corbado web-js package for the frontend part as well as the Corbado PHP SDK for the PHP backend part.
If you want to see the finished code, please have a look at our sample application GitHub repository.
The result looks as follows:
This tutorial assumes basic familiarity with PHP Laravel, JavaScript and HTML. Also make sure to have PHP as well as composer installed and accessible from your shell. Lets dive in!
Recent Articles
A Laravel project contains many files, but the important ones for us in this tutorial are the following:
... ├── .env # Contains all environment variables ├── app | ├── Http | | └── Controllers | | ├── ProfileController.php # Responsible to retrieve profile information in backend | | └── Controller.php | | | └── Providers | └── AppServiceProvider.php # Used to inject the Corbado project ID into all views ├── config | └── app.php # Make environment variables available ├── resources | └── views | ├── welcome.blade.php # The login page | └── profile.blade.php # The profile page └── routes └── web.php # The routes for our frontend pages
Become part of our Passkeys Community for updates and support.
JoinLet's start by creating our PHP Laravel app:
In the installation guide steps, we select the following:
Pest
That's already it for now.
To set up Corbado passkey authentication in your PHP Laravel project, follow the next steps.
Visit the Corbado developer panel to sign up and create your account (you'll see the passkey sign-up in action here!).
After sign-up, a project wizard will guide you through the necessary steps to get everything up and running:
Application URL
and Relying Party ID
. The Application URL
is the URL where you embed the Corbado UI component. In this example, we set it to http://localhost:8000
. The Relying Party ID
is the domain (no protocol, no port and no path) where passkeys should be bound to. Here, it's localhost
(you can define both values als in the Settings > General > URLs of the Corbado developer panel).Afterwards, you'll see the relevant html / javascript code snippets you need to integrate into the project. The subsequent sections of this article will explain them in detail.
As another step, we create an API secret
which will be needed to request user data from the
Corbado backend. Please create an API secret
in Settings > Credentials > API secrets.
Afterwards, we need to also get the Frontend API
and Backend API
URLs which we can find
in Settings > General.
Next, you need to go back to your terminal and should navigate to the root directory of the project:
Open the .env
file at the root of your project.
Copy and paste the Project ID
, API secret
, Frontend API URL
and Backend API URL
(you can obtain it from the Corbado developer panel from Settings > General > Project info above into it. The .env
file should look like this now:
First, install the Corbado PHP SDK:
Next, head into the config/app.php
file and add the following line right at the bottom of the returned array:
That's it for the setup.
Our Homepage will use the Corbado web-js package to handle passkey authentication.
We include the relevant assets in the head of the page and create a div with the id of corbado-auth
.
In a separate script tag, we'll be loading our Corbado project with our project id
and set the setShortSessionCookie
option to true, so a jwt session-cookie is created on login.
Lastly, we'll inject the authentication UI in our div.
Head into the resources/views/welcome.blade.php
file and replace the skeleton with the following code:
The route for this page is already preconfigured by the skeleton project, so we can now see this page
under http:localhost:8000
.
To try it out, run your application with the following command:
Note: The component will only work properly when the page is visited on localhost
.
Create a ProfileController with the following command:
It will also need a cache for the jwks, which we'll install like this:
Our controller will make user information available to our template by using the Corbado PHP SDK.
Now, head into the created Controller file under
app/Http/Controllers/ProfileController.php
.
In the controller's constructor, we'll instantiate the PHP SDK:
Now, add a showProfile method, that will make user information available to our template:
Next, we'll create the template under resources/views/profile.blade.php
.
It will render some user information and use the Corbado web-js package again to handle logout button functionality.
We add an event listener to the button that uses the Corbado.logout
utility.
Lastly, we'll crate the route for this view under routes/web.php
.
The final route file should look like this:
That's already it, we now have a fully functioning profile page using information obtained in our PHP server and Corbado web-js for frontend functionality like a logout.
Finally, take a look at the result by running:
After signup, you'll be redirected to the profile page. Here, you can logout and return to the homepage again.
This tutorial showed how easy it is to add passwordless authentication with passkeys to a PHP Laravel application using Corbado. Besides the passkey-first authentication, Corbado provides simple session management, that we used for a retrieval of basic user data. If you want to read more about how Corbado's session management please check the docs here.
Table of Contents
Enjoyed this read?
🤝 Join our Passkeys Community
Share passkeys implementation tips and get support to free the world from passwords.
🚀 Subscribe to Substack
Get the latest news, strategies, and insights about passkeys sent straight to your inbox.
We provide UI components, SDKs and guides to help you add passkeys to your app in <1 hour
Start for free