English
  • Deutsch
  • English
  • Français
  • Español
  • Português
  • 日本語
vue-3 logo
typescript logo
laravel logo
php logo
Authentication
Authorization

Hello World Full-Stack Security: Vue 3 + Laravel

Authentication
Authorization
Dan Arias
By Dan Arias, R&D Engineering ManagerLast update on November 19, 2021

Developers can easily secure a full-stack application using Auth0. This code sample demonstrates how to implement authentication in a client application built with Vue 3 and TypeScript, as well as how to implement authorization in an API server built with Laravel and PHP. You'll connect the client and server applications to see the full security flow in action!

Let's get started!

Quick Auth0 Set Up

To get started, create an Auth0 account to connect your application with the Auth0 Identity Platform. You can also use any of your existing Auth0 accounts.

Get the Auth0 domain and client ID

  • Open the Applications section of the Auth0 Dashboard.

  • Click on the Create Application button and fill out the form with the following values:

    • Name: Hello World Client

    • Application Type: Single Page Web Applications

  • Click on the Create button.

Visit the "Register Applications" document for more details.

An Auth0 Application page loads up.

As such, click on the "Settings" tab of your Auth0 Application page, locate the "Application URIs " section, and fill in the following values:

  • Allowed Callback URLs: http://localhost:4040/callback

  • Allowed Logout URLs: http://localhost:4040

  • Allowed Web Origins: http://localhost:4040

Scroll down and click the "Save Changes" button.

Next, locate the "Basic Information" section.

Auth0 application settings to enable user authentication

When you enter a value in the input fields present on this page, any code snippet that uses such value updates to reflect it. Using the input fields makes it easy to copy and paste code as you follow along. For security, these values are stored in memory and only used locally. They are gone as soon as you refresh the page! As an extra precaution, you should use values from an Auth0 test application instead of a production one.

As such, enter the "Domain" and "Client ID" values in the following fields to set up your single-page application in the next section:

Get the Auth0 audience

  • Open the APIs section of the Auth0 Dashboard.

  • Click on the Create API button and fill out the "New API" form with the following values:

    • Name: Hello World Server

    • Identifier: https://hello-world.example.com

  • Click on the Create button.

Visit the "Register APIs" document for more details.

When setting up APIs, we also refer to the API identifier as the Audience value. Store that value in the following field to set up your API server in the next section:

Set Up and Run the Laravel Project

Start by cloning the Laravel project and checking out the basic-authorization branch:

git clone https://github.com/auth0-developer-hub/api_laravel_php_hello-world.git --branch basic-authorization

Make the project directory your current working directory:

cd api_laravel_php_hello-world

If you are running your project with Sail, generate a Laravel app key value using the following command:

./vendor/bin/sail artisan key:generate

If you are running your project with Composer, you can use the following command instead:

php artisan key:generate

Store the output from any of the commands above in the following field:

Now, create a .env file under the project directory and populate it as follows:

.env
APP_KEY=KEY-VALUE
SERVER_PORT=6060
CLIENT_ORIGIN_URL=http://localhost:4040
AUTH0_AUDIENCE=AUTH0-AUDIENCE
AUTH0_DOMAIN=AUTH0-DOMAIN

With your .env in place, install the Laravel project dependencies:

composer install

Execute the following command to run the Laravel API server using Sail:

./vendor/bin/sail up

Otherwise, use the following command to run the API server using Composer:

php artisan serve

Set Up and Run the Vue 3 Project

Start by cloning the project into your local machine:

git clone https://github.com/auth0-developer-hub/spa_vue-3_typescript_hello-world.git --branch basic-authentication

You are checking out the basic-authentication branch, which holds all the Vue 3 code related to implementing user login with Auth0.

Make the project directory your current working directory:

cd spa_vue-3_typescript_hello-world

Next, install the Vue 3 project dependencies:

npm install

Create a .env file under the project directory and populate it as follows:

.env
VUE_APP_AUTH0_DOMAIN=AUTH0-DOMAIN
VUE_APP_AUTH0_CLIENT_ID=AUTH0-CLIENT-ID
VUE_APP_AUTH0_AUDIENCE=https://hello-world.example.com
VUE_APP_AUTH0_CALLBACK_URL=http://localhost:4040/callback
VUE_APP_API_SERVER_URL=http://localhost:6060

Run the Vue 3 application by issuing the following command:

npm run serve

You can now visit http://localhost:4040/ to access the application.

There's something missing in this Vue 3 code sample. There's no login or sign-up forms!

When you click on the "Log In" button, Vue 3 takes you to the Auth0 Universal Login page. Your users can log in to your application through a page hosted by Auth0, which provides them with a secure, standards-based login experience that you can customize with your own branding and various authentication methods, such as logging in with username and password or with a social provider like Facebook or Google.

Once you log in, visit the protected "Profile" page, http://localhost:4040/profile, to see all the user profile information that Auth0 securely shares with your application using ID tokens.

Then, visit the "External API" page, http://localhost:4040/external-api, to practice requesting protected resources from an external API server using access tokens. Before you can do that, you need to set up and configure an API with Auth0. It'll be quick!