Web JS with Vue
Prerequisites
Required client settings
As this library is designed for SPAs, make sure you have selected
Application Type in your client settings as Single Page Application.
For a fully functional integration ensure that Callback redirect URIs and Signout redirect URIs are configured.
Install web-js package
Vue client configuration
Grab the client key. Now, in your Vue main.js file create web-js Auth object which looks like below.
Next add a .env (.env.local for local or .env.production for production) file in your Vue project root, and populate following values,
Here is an example version of .env.local
Using web-js functions
In your *.vue files access various web-js JavaScript functions via this. For instance,
- To login with redirect,
- For logout with redirect,
- To check if user is authenticated,
In your *.js files you can access various web-js JavaScript functions via Vue.prototype. For instance,
Add view for redirect URI
- Create
Callback.vuein your view, add authorization response processing logic, and register as route/callback.
- Register redirect URI in your client's settings and also add in
.env. URI should behttps://{host}/callbackorhttp://{host}/callbackwhere host can be localhost i.e.localhost:8080or domain hosting your Vue app i.e.app.my-domain.com.
Add view to initiate login
- Create
Login.vuein your view, add logic to initiate login, and register as route/login.
- Your can use login endpoint
https://{host}/loginorhttp://{host}/loginas post logout uri in your client's settings and also add in.envwhere where host can be localhost i.e.localhost:8080or domain hosting your Vue app i.e.app.my-domain.com.
Add view for passwordless login
Alternatively, you can add passwordless login using magic link which will be sent by email. You must enable passwordless options in tenant's settings i.e. Passwordless Enabled and Using Email Link must be enabled for tenant.
- Make sure
openidandemailare included inscope(i.e.VUE_APP_Scopefor environment variable). This is required for session validation. - Create
Passwordless.vuein your view, and register as route/passwordless. Add a form on the view which captures email address of the user. - In
scriptsection of the view add form submit handleronSubmitfunction which basically calls$auth.login_with_email_linkwithemailas parameter.
warning
Passwordless login via email link is still in beta. Please use it with caution as APIs and SDKs will change in coming days.
Add view to initiate logout
- Create
Logout.vuein your view, add logic to initiate logout, and register as route/logout.
Add Vue router guard
Using web-js, you can guard your Vue router for un-authenticated users and redirect them to login. See below snippet which uses $auth.session.is_authenticated() and expects a meta.requiresAuth to be true.
Add Vue routes
Vue Sample
To learn more download our Vue sample from our Github repository. In less than 10 minutes you can deploy Vue sample to Netlify.