How to add Auth0 Authentication to a Vue.js Application in 7 steps.
Storyblok is the first headless CMS that works for developers & marketers alike.
We've read multiple articles in the past on how to use Vue.js with Auth0, most of them were outdated or used JavaScript in a way we wouldn't. In this article, we will first guide you through the setup with a fresh initialized Vue.js application and how to setup your own Authentication plugin for Vue.
- Introduction
- Environment Setup
- Prepare Auth0
- Auth0 Callback Route
- Auth0 + Vue.js Authentication
- Adding a Navigation Guard
- Accessing User Information
Environment Setup
This tutorial will use a freshly created Vue.js project. The newest version of Vue.js by the time of writing this tutorial is 2.5.13 with the newest version of the @vue/cli 3.0.
We will use a basic setup including vue-router so you can use the CLI creation step with the setting manually select features
and select router
as shown in the image below.
The final step is to boot up your Vue.js application, this can be done by navigating into the project folder and executing the npm script serve
as shown we did below:
I'll not go deeper into how to use Vue.js and/or the CLI, since all you need to know is already covered in their documentation. After executing npm run serve
you should already see a basic setup opened in your default browser as we display in the following image.
Prepare Auth0
In order to get started with the setup of the Auth0 + Vue.js Authentication service, we will need our Auth0 account to be ready for us to use.
1. Create your new Account
The first is kinda clear. You can register in Auth0 for the free tier by simply filling their registration form and you're ready to go.
2. Create a new Client
You will find yourself on the dashboard of Auth0 which looks quite familiar for every developer because you already see a heat map as we know it from Github. On the right side, you will see the New Client
button which will guide you through the next setup.
3. Using the Quickstart to get your client settings
The Auth0 quickstart is nice to have and is used as a basis for this tutorial, sadly it was not a tutorial written especially for Vue.js, but we will still use it to receive our client settings without to look it up manually. At What technology are you using for your web app? simply press the Vue icon and scroll down to Create an Authentication Service and you should find your client settings. Simply copy that and save it for later, since we will use it during the Vue.js Plugin creation.
4. Preparing the Callback URL in the Client Settings
At the top of your current page, you should see the tab Settings right below the name of your newly created Client. In those settings, we will have to add http://localhost:8080/callback
as allowed Callback URL. You can add other Callback URLs with your own domain by comma-separating them (typically to handle different environments like QA or testing). Make sure the press Save Changes at the bottom of the screen, which can easily be overlooked.
Auth0 Callback Route
You may have noticed that the callback URL we've entered is without the #
in front of the route, to allow this we can simply add mode: 'history'
to our src/router.js
.
Since we won't have a /about
route we will simply rename it to /callback
as we did already above, in the same step we will also exchange the content of our now src/views/Callback.vue
to the following:
You will notice that we're using this.$auth
which currently does not exist - to allow us to access that functionally and finally create the bridge between Auth0 and Vue.js we will now add our own Vue.js plugin to achieve that.
Setup Auth0 + Vue.js Auth Plugin
We will use Auth0 and their Hosted Login Page in combination with their npm module auth0-js
, therefore we will start by installing that module.
Update (24.04.2018): Make sure you've auth0-js greater than 9.3.0 since known vulnerability are fixed in that version.
Next, we will create a new file src/auth.js
with the following content:
The code above introduces multiple new functionality we can use in our application. One of them is handleAuthentication
which is already used in the src/views/Callback.vue
View. It will make use of the parseHash
function of the auth0-js
client to allow the user authentication. You may also have noticed that we use computed properties with getter
and setter
. Vue.js computed properties are by default getter-only, but we can also provide a setter when we need it. Now when we run vm.token = authResult.idToken
, the setter will be invoked and vm.token
and will be updated accordingly in the localStorage
.
To use our new Vue + Auth0 plugin simply import it to src/main.js
as we do below:
User Information: Scope
The scope
parameter allows our application to express the desired scope of the access request. In turn, the scope
parameter can be used by the authorization server in the response to indicate which scopes were actually granted (if they are different than the ones requested). The scope we will use besides the openid
is called profile
which allows us to access their user information after they accept, make sure to only ask the user about information you really need - since we're going for profile
in this demo to speed up the process I recommend to specify only fields you will use to benefit your users. Make sure to update the copied configuration by adding profile
to the scope. You can always read More about scopes.
Adding a Navigation Guard
As the name suggests, the navigation guards provided by vue-router are primarily used to guard navigations either by redirecting it or canceling it. We will use a Global Guard and to do so we will update our src/router.js
accordingly.
Visit http://localhost:8080
You should now be already redirected to your Auth0 Hosted Login Page. After a quick sign-up with your Google Account or Email + Password (both default and can be changed in Auth0) you should be redirected back to the /callback
route of your application – and since that authentication should also already working – back to /
. In the next step we will adopt src/views/Home.vue
to using some user information.
Using User Information and News Entries
During handleAuthentication
in the src/auth.js
you can see that there is the idTokenPayload
which contains the user information we received from Auth0. To do so we've prepared a simple Bootstrap 4 Navbar including the received User Image and a logout button and we also display a short newsfeed from Storyblok in this now restricted area of your app.
Let's start with exchanging the content of src/App.vue
with the following:
To easily load data from an API we will add axios to our setup:
Accessing User Information
And to finally access the user information we will replace the content of src/views/Home.vue
with the code below:
Well done!
The final result should display a bootstrap jumbotron with your name, some cards, and a navbar. The cards will link to other blog posts of Storyblok as seen above. I hope you found this tutorial interesting and maybe it will help you to get started with your idea a little bit faster. I think that the combination of Auth0 with Vue.js is one of the most efficient ways to get your idea to an initial useable prototype and beyond. You won't have to deal with repetitive tasks for user authentication ever again (no login forms - yes! Of course you can customize those Hosted Login Pages) I'm looking forward to hear your feedback, thoughts and ideas to this tutorial or your project ideas!
Resource | Link |
---|---|
Github Repo of this Tutorial | https://github.com/DominikAngerer/auth0-vue |
Vue.js | https://www.vuejs.org |
Vue-CLI 3.0 | https://github.com/vuejs/vue-cli |
Auth0 | https://auth0.com |
Auth0-js on Npm | https://www.npmjs.com/package/auth0-js |
Want to get your hands on the newest technologies to revolutionize your projects?
Check out Storyblok!