Vue.js 2 Code Sample: Basic Authentication
This Vue.js 2 code sample powers up the "Vue.js 2 By Example: Authentication Essentials" guide, where you can learn how to implement authentication in Vue.js 2 apps using Auth0 by Okta.
Vue.js Code Sample Specs
This code sample uses the following tools:
- Vue
v2.6.14
- Vue Router
v3.5.3
- Auth0 SPA SDK
v2.0.1
- Vite
v2.8.4
The Vue.js project dependency installations were tested with npm v8.5.5
. Running the Vue.js application was tested using Node.js v16.15.0
.
Quick Auth0 Set Up
First and foremost, if you haven't already, sign up for an Auth0 account to connect your application with the Auth0 Identity Platform.
Next, you'll connect your Single-Page Application (SPA) with Auth0. You'll need to create an application registration in the Auth0 Dashboard and get two configuration values: the Auth0 Domain and the Auth0 Client ID. You'll also need to define an Auth0 Audience value within your project to practice making secure calls to an external API.
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:
Hello World Client
- Click on the Create button.
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:
http://localhost:4040/callback
http://localhost:4040
http://localhost:4040
Scroll down and click the "Save Changes" button.
Next, locate the "Basic Information" section.
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.
As such, enter the "Domain" and "Client ID" values in the following fields to set up your single-page application in the next section:
Set Up the Vue.js 2 Code Sample
Start by cloning the project into your local machine:
git clone https://github.com/auth0-developer-hub/spa_vue-v2_javascript_hello-world.git
Make the project directory your current working directory:
cd spa_vue-v2_javascript_hello-world
Then, check out the basic-authentication
branch, which holds all the Vue.js code related to implementing basic user authentication with Auth0:
git checkout basic-authentication
Next, install the Vue.js project dependencies:
npm install
Create a .env
file under the root project directory:
touch .env
Populate it with the following environment variables:
VITE_AUTH0_DOMAIN=AUTH0-DOMAINVITE_AUTH0_CLIENT_ID=AUTH0-CLIENT-IDVITE_AUTH0_CALLBACK_URL=http://localhost:4040/callbackVITE_API_SERVER_URL=http://localhost:6060
This Vue.js 2 code sample is compatible with any "Auth0 Hello World" API code samples, which run on http://localhost:6060
by default. However, the basic-authentication
branch mocks the external API server using json-server
.
Execute the following command to run the JSON server API:
npm run api
Finally, open another terminal tab and execute this command to run your Vue.js application:
npm run dev
Use the Vue.js 2 Sample Application
You can now visit http://localhost:4040/
to access the application.
If you want to learn how to implement user authentication in Vue.js 2 step by step, check out the "Vue.js 2 By Example: Authentication Essentials".
When you click on the "Log In" button, Vue.js 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 a username and password or with a social provider like Facebook or Google.
Once you log in, visit the protected "Profile" page to see all the user profile information that Auth0 securely shares with your application using ID tokens:
You can test that the protected Vue.js routes require users to log in before accessing them. Click on the "Log Out" button and try to access the Profile page, Protected page, or the Admin page:
If everything is working as expected, Vue.js redirects you to log in with Auth0.
Connect the Vue.js 2 Code Sample with an API Server
Before you can practice requesting protected resources from an external API server using access tokens, you need to set up and configure an API with Auth0. You can pair this Vue.js 2 code sample with any of our "Hello World" API server code samples.
Set up a Hello World API server
Pick an API code sample in your preferred backend framework and language from the list below and follow the instructions on the code sample page to set it up. Once you complete the sample API server set up, please return to this Vue.js code sample page to learn how to integrate that API server with your Vue.js application.
While setting up the API server code sample, you created an Auth0 Audience value. Store that value in the following field so that you can use it throughout the instructions present in this section easily:
Set up the Vue.js client application
If you haven't cloned the repository already, start by cloning the Vue.js project into your local machine:
git clone https://github.com/auth0-developer-hub/spa_vue-v2_javascript_hello-world.git
Make the project directory your current working directory:
cd spa_vue-v2_javascript_hello-world
Then, check out the basic-authentication-with-api-integration
branch:
git checkout basic-authentication-with-api-integration
Next, install the Vue.js project dependencies:
npm install
Now, either create or update the .env
file under the Vue.js project directory with the following:
VITE_AUTH0_DOMAIN=AUTH0-DOMAINVITE_AUTH0_CLIENT_ID=AUTH0-CLIENT-IDVITE_AUTH0_CALLBACK_URL=http://localhost:4040/callbackVITE_API_SERVER_URL=http://localhost:6060VITE_AUTH0_AUDIENCE=AUTH0-AUDIENCE
This time around, you include a VITE_AUTH0_AUDIENCE
value, which is an identifier that represents the compatible Hello World API you just registered in your Auth0 tenant. This identifier is also known as the Auth0 audience. Your Vue.js application must provide this value to the Auth0 authorization server in order to get a valid access token to make authenticated requests to the compatible external API.
If you haven't started the Vue.js application yet, execute this command to run it:
npm run dev
Now your Vue.js is all set up to request protected data from the "Hello World" API server of your choice. Ensure that your API server is running and visit the Protected page or the Admin page of your Vue.js application:
Verify that these pages are displaying the relevant messages from the API. Each API response includes a text
property with the corresponding message data for the page.
This basic-authentication-with-api-integration
branch of the code sample repository demonstrates how to request protected data from an API using access tokens in Vue Options API.
Handle Vue.js Complex Use Cases
These Vue.js code samples covered the most common authentication use case for a Vue.js application:
- Allow users to log in, sign up, and log out.
- Display user profile information.
- Require authentication to access Vue.js routes.
- Make authenticated calls to an API from Vue.js.
However, Auth0 is an extensible and flexible identity platform that can help you achieve even more. If you have a more complex use case, check out the Auth0 Architecture Scenarios to learn more about diverse architecture scenarios we have identified when working with customers on implementing Auth0.