Search…
Vue
Integrate Passage into your Vue application
To see a full example of using Passage in a Vue + Express.js application, please visit our sample app on GitHub.

Importing and Using the Passage-Auth Custom Element

The easiest way to add authentication to a web frontend is with a Passage Auth custom element. First you'll need to install the passage-elements package from npm:
1
npm i --save @passageidentity/passage-elements
Copied!
Then import the package in the module where you intend to use the custom element
1
import '@passageidentity/passage-elements/passage-auth'
Copied!
Importing this script will register the Passage custom element for use in your Vue components. For more information about custom elements refer to the online documentation.
It's then just a matter of embedding the passage-auth element into your component that will handle login. This is done in this example in frontend/src/views/Home.vue:
1
<div class="authContainer">
2
<passage-auth :app-id="appId"></passage-auth>
3
</div>
Copied!

Configuring Vue to Recognize Custom Elements

Vue works with custom elements out of the box but by default it will log a warning to the console that it could not resolve the component for the custom element. To configure Vue with information that the <passage-auth> tag is a custom element and suppress this warning you can add this configuration to vue.config.js:
1
compilerOptions: {
2
// treat any tag that starts with passage- as custom elements
3
isCustomElement: (tag) => tag.startsWith('passage-'),
4
},
Copied!

Getting Authentication Status and User Information

After the user has logged in with Passage, you can check if a user's auth token is valid and retrieve basic user information from Passage using the PassageUser class exported from @passageidentity/passage-auth/passage-user. This example wraps this functionality into a reusable Vue composable in useAuthStatus:
1
import { PassageUser } from '@passageidentity/passage-elements/passage-user'
2
​
3
export function useAuthStatus(){
4
...
5
new PassageUser().userInfo().then(userInfo => {
6
...
Copied!
The PassageUser().authGuard() can be used as a route guard in your application, but it should NOT be make authorization decisions when fetching data from an API server. Route guards provide a better user experience, but less security than using one of Passage's backend libraries. For applications using an API server, you must use one of the Passage backend SDKs to safely verify user authentication tokens.