Skip to content

ionic-team/getting-started-ac-vue

Repository files navigation

Getting Started with Auth Connect in @ionic/vue

In this tutorial we will walk through the basic setup and use of Ionic's Auth Connect in an @ionic/vue application.

In this tutorial, you will learn how to:

  • Install and configure Auth Connect
  • Perform Login and Logout operations
  • Check if the user is authenticated
  • Obtain the tokens from Auth Connect
  • Integrate Identity Vault with Auth Connect

:::note The source code for the Ionic application created in this tutorial can be found here :::

Generate the Application

The first step to take is to generate the application:

ionic start getting-started-ac-vue tabs --type=vue

Now that the application has been generated, let's also add the iOS and Android platforms.

Open the capacitor.config.ts file and change the appId to something unique like io.ionic.gettingstartedacvue:

import { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  appId: 'io.ionic.gettingstartedacvue',
  appName: 'getting-started-ac-vue',
  webDir: 'dist',
  server: {
    androidScheme: 'https',
  },
};

export default config;

Next, build the application, then install and create the platforms:

npm run build
ionic cap add android
ionic cap add ios

Finally, in order to ensure that a cap sync is run with each build, add it to the build script in the package.json file as such:

  "scripts": {
    "build": "vue-tsc && vite build && cap sync",
    ...
  },

Install Auth Connect

In order to install Auth Connect, you will need to use ionic enterprise register to register your product key. This will create a .npmrc file containing the product key.

If you have already performed that step for your production application, you can just copy the .npmrc file from your production project. Since this application is for learning purposes only, you don't need to obtain another key.

You can now install Auth Connect and sync the platforms:

npm install @ionic-enterprise/auth

Configure Auth Connect

Our next step is to configure Auth Connect. Create a file named src/composables/auth.ts and fill it with the following boilerplate content:

import { ProviderOptions } from '@ionic-enterprise/auth';
import { isPlatform } from '@ionic/vue';

const isNative = isPlatform('hybrid');

const options: ProviderOptions = {
  clientId: '',
  discoveryUrl: '',
  scope: 'openid offline_access',
  audience: '',
  redirectUri: isNative ? '' : '',
  logoutUrl: isNative ? '' : '',
};

export const useAuth = () => ({});

Auth Connect Options

The options object is passed to the login() function when we establish the authentication session. As you can see, there are several items that we need to fill in. Specifically: audience, clientId, scope, discoveryUrl, redirectUri, and logoutUrl.

Obtaining this information likely takes a little coordination with whoever administers our backend services. In our case, we have a team that administers our Auth0 services and they have given us the following information:

  • Application ID: yLasZNUGkZ19DGEjTmAITBfGXzqbvd00
  • Audience: https://io.ionic.demo.ac
  • Metadata Document URL: https://dev-2uspt-sz.us.auth0.com/.well-known/openid-configuration
  • Web Redirect (for development): http://localhost:8100/login
  • Native Redirect (for development): msauth://login
  • Additional Scopes: email picture profile

Translating that into our configuration object, we now have this:

const options: ProviderOptions = {
  audience: 'https://io.ionic.demo.ac',
  clientId: 'yLasZNUGkZ19DGEjTmAITBfGXzqbvd00',
  discoveryUrl: 'https://dev-2uspt-sz.us.auth0.com/.well-known/openid-configuration',
  logoutUrl: isNative ? 'msauth://login' : 'http://localhost:8100/login',
  redirectUri: isNative ? 'msauth://login' : 'http://localhost:8100/login',
  scope: 'openid offline_access email picture profile',
};

The web redirect for development is on port 8100. Vue uses port 5173 by default, so we will need to make a minor change to our package.json file as well:

  "scripts": {
    "build": "vue-tsc && vite build && cap sync",
    "dev": "vite --port=8100",
    "lint": "eslint",
    "prepare": "husky install",
    "preview": "vite preview",
    "test:e2e": "cypress run",
    "test:unit": "vitest"
  },

Note: you can use your own configuration for this tutorial as well. However, we suggest that you start with our configuration, get the application working, and then try your own configuration after that.

Initialization

Before we can use any AuthConnect functions we need to make sure we have performed the initialization. Add the code to do this after the setting of the options value in src/composables/auth.ts.

import { AuthConnect, ProviderOptions } from '@ionic-enterprise/auth';
import { isPlatform } from '@ionic/vue';

const isNative = isPlatform('hybrid');

const options: ProviderOptions = {
  // see the options setting above
};

const performInit = async (): Promise<void> => {
  await AuthConnect.setup({
    platform: isNative ? 'capacitor' : 'web',
    logLevel: 'DEBUG',
    ios: {
      webView: 'private',
    },
    web: {
      uiMode: 'popup',
      authFlow: 'implicit',
    },
  });
};

let initializing: Promise<void>;
const initialize = async (): Promise<void> => {
  if (!initializing) {
    initializing = new Promise((resolve) => {
      performInit().then(() => resolve());
    });
  }
  return initializing;
};

initialize();

export const useAuth = () => ({});

This will get Auth Connect ready to use within our application. Notice that this is also where we supply any platform specific Auth Connect options. Right now, the logLevel is set to DEBUG since this is a demo application. In a production environment, we probably would set it to DEBUG in development and ERROR in production.

The initialize() function will be called from several locations to ensure the setup is complete before making any further AuthConnect calls.

The Provider

Auth Connect requires a provider object that specifies details pertaining to communicating with the OIDC service. Auth Connect offers several common providers out of the box: Auth0Provider, AzureProvider, CognitoProvider, OktaProvider, and OneLoginProvider. You can also create your own provider, though doing so is beyond the scope of this tutorial.

Since we are using Auth0, we will create an Auth0Provider:

import { Auth0Provider, ProviderOptions } from '@ionic-enterprise/auth';
import { isPlatform } from '@ionic/vue';
...
const provider = new Auth0Provider();
...
export const useAuth = () => ({});

Login and Logout

Login and logout are the two most fundamental operations in the authentication flow.

For the login(), we need to pass both the provider and the options we established above. The login() call resolves an AuthResult if the operation succeeds. The AuthResult contains the auth tokens as well as some other information. This object needs to be passed to almost all other Auth Connect functions. As such, it needs to be saved.

The login() call rejects with an error if the user cancels the login or if something else prevents the login to complete.

Add the following code to src/composables/auth.ts:

import { Auth0Provider, AuthConnect, AuthResult, ProviderOptions } from '@ionic-enterprise/auth';
import { isPlatform } from '@ionic/vue';
...
let authResult: AuthResult | null = null;
...
const login = async (): Promise<void> => {
  await initialize();
  authResult = await AuthConnect.login(provider, options);
}
...
export const auth = () => ({
  login
});

For the logout operation, we pass the provider and the authResult that was returned by the login() call.

const logout = async (): Promise<void> => {
  await initialize();
  if (authResult) {
    await AuthConnect.logout(provider, authResult);
    authResult = null;
  }
};
...
export const useAuth = () => ({
  login,
  logout,
});

To test these new function, replace the ExploreContainer with "Login" and "Logout" buttons in the src/views/Tab1Page.vue file:

<ion-button @click="logout">Logout</ion-button> <ion-button @click="login">Login</ion-button>

Within the script setup area, import useAuth and expose the login and logout functions:

<script setup lang="ts">
import { IonButton, IonPage, IonHeader, IonToolbar, IonTitle, IonContent } from '@ionic/vue';
import { useAuth } from '@/composables/auth';

const { login, logout } = useAuth();
</script>

If you are using our Auth0 provider, you can use the following credentials for the test:

  • Email Address: test@ionic.io
  • Password: Ion54321

You should be able to login and and logout successfully.

Configure the Native Projects

Build the application for a native device and try the login there as well. You should notice that this does not work on your device.

The problem is that we need to let the native device know which application(s) are allowed to handle navigation to the msauth:// scheme. To do this, we need to modify our android/app/build.gradle and ios/App/App/Info.plist files as noted here. In the Info.plist file, use msauth in place of $AUTH_URL_SCHEME.

Determine Current Auth Status

Right now, the user is shown both the login and logout buttons, and you don't really know if the user is logged in or not. Let's change that.

A simple strategy to use is if we have an AuthResult then we are logged in, otherwise we are not. Add code to do that in src/composables/auth.ts. Ignore the extra complexity with the getAuthResult() function. We will expand on that as we go.

const getAuthResult = async (): Promise<AuthResult | null> => {
  return authResult;
}

const isAuthenticated = async (): Promise<boolean> => {
  await initialize();
  return !!(await getAuthResult());
}
...
export const auth = () => ({
  isAuthenticated,
  login,
  logout,
});

Use this in the Tab1Page to display only the Login or the Logout button, depending on the current login status. First, update the bindings on the buttons:

<ion-button v-if="authenticated" @click="logoutClicked">Logout</ion-button>
<ion-button v-else @click="loginClicked">Login</ion-button>

Notice the newly added v-if and v-else conditions. Also notice the changes to the @click event bindings. The reason for this is that our click logic is going to do a little more work than before.

What we want to do in the script setup node of the Tab1Page is:

  • upon creating the page, check the current auth status
  • after performing a login or logout operation, refresh the auth status

Here is one way to code all of that. Integrate this into the existing Tab1Page code.

const authenticated = ref<boolean>();
const { login, logout, isAuthenticated } = useAuth();

const checkAuth = async () => {
  authenticated.value = await isAuthenticated();
};

const loginClicked = async () => {
  try {
    await login();
    checkAuth();
  } catch (err) {
    console.log('Error logging in:', err);
  }
};

const logoutClicked = async () => {
  await logout();
  checkAuth();
};

checkAuth();

Notice the try ... catch in loginClicked(). The login() will throw an error if the user fails to log in. Production applications should have some kind of handling here, but our sample can get away with simply logging the fact.

At this point, you should see the Login button if you are not logged in and the Logout button if you are.

Get the Tokens

We can now log in and out, but what about getting at the tokens that our OIDC provider gave us? This information is stored as part of the AuthResult. Auth Connect also includes some methods that allow us to easily look at the contents of the tokens. For example:

const getAccessToken = async (): Promise<string | undefined> => {
  await initialize();
  const res = await getAuthResult();
  return res?.accessToken;
};

const getUserName = async (): Promise<string | undefined> => {
  await initialize();
  const res = await getAuthResult();
  if (res) {
    const data = (await AuthConnect.decodeToken(TokenType.id, res)) as { name: string };
    return data?.name;
  }
};

Note: the format and data stored in the ID token may changed based on your provider and configuration. Check the documentation and configuration of your own provider for details.

Add these to src/composables/auth.ts and export them at the end of the file like we did the other functions.

You can use these wherever you need to supply a specific token. For example, if you are accessing a backend API that requires you to include a bearer token (and you probably are if you are using Auth Connect), then you can use the getAccessToken() method and create in interceptor that adds the token.

We don't need an interceptor for this app, but as a challenge to you, update the Tab1Page to show the current user's name when they are logged in. You could also display the access token if you want (though you would never do that in a real app).

Refreshing the Authentication

In a typical OIDC implementation, access tokens are very short lived. In such a case, it is common to use a longer lived refresh token to obtain a new AuthResult.

Let's add a function to src/composables/auth.ts that does the refresh, and then modify getAuthResult() to call it when needed.

const refreshAuth = async (authResult: AuthResult): Promise<AuthResult | null> => {
  let newAuthResult: AuthResult | null = null;

  if (await AuthConnect.isRefreshTokenAvailable(authResult)) {
    try {
      newAuthResult = await AuthConnect.refreshSession(provider, authResult);
    } catch (err) {
      null;
    }
  }

  return newAuthResult;
};

const getAuthResult = async (): Promise<AuthResult | null> => {
  if (authResult && (await AuthConnect.isAccessTokenExpired(authResult))) {
    authResult = await refreshAuth(authResult);
  }
  return authResult;
};

Now anything using getAuthResult() to get the current auth result will automatically handle a refresh if needed.

Store the Auth Result

Up until this point, we have been storing our AuthResult in a local state variable in src/composables/auth.ts. This has a couple of disadvantages:

  • Our tokens could show up in a stack trace.t
  • Our tokens do not survive a browser refresh or application restart.

There are several options we could use to store the AuthResult, but one that handles persistence as well as storing the data in a secure location on native devices is Identity Vault.

For our application we will install identity vault and use it in "secure storage" mode to store the tokens. The first step is to install the product.

npm i @ionic-enterprise/identity-vault

Next we will create a factory that builds either the actual vault if we are on a device or a browser based "vault" that is suitable for development if we are in the browser. The following code should go in src/composables/vault-factory.ts.

import { isPlatform } from '@ionic/vue';
import { BrowserVault, IdentityVaultConfig, Vault } from '@ionic-enterprise/identity-vault';

export const useVaultFactory = () => {
  const createVault = (config: IdentityVaultConfig): Vault | BrowserVault =>
    isPlatform('hybrid') ? new Vault(config) : new BrowserVault(config);

  return { createVault };
};

This provides us with a secure vault on our devices, or a fallback vault that allows us to keep using our browser-based development flow.

Now that we have a factory in place to build our vaults, let's create some functions that allow us to manage our authentication result.

Create a file called src/composables/session-vault.ts with the following contents:

import { AuthResult } from '@ionic-enterprise/auth';
import { DeviceSecurityType, VaultType } from '@ionic-enterprise/identity-vault';
import { useVaultFactory } from './vault-factory';

const key = 'auth-result';

const { createVault } = useVaultFactory();
const vault = createVault({
  key: 'io.ionic.gettingstartedacvue',
  type: VaultType.SecureStorage,
  deviceSecurityType: DeviceSecurityType.None,
  lockAfterBackgrounded: 5000,
  shouldClearVaultAfterTooManyFailedAttempts: true,
  customPasscodeInvalidUnlockAttempts: 2,
  unlockVaultOnLoad: false,
});

const clearSession = (): Promise<void> => {
  return vault.clear();
};

const getSession = (): Promise<AuthResult | null> => {
  return vault.getValue<AuthResult>(key);
};

const setSession = (value: AuthResult): Promise<void> => {
  return vault.setValue(key, value);
};

export const useSessionVault = () => ({
  clearSession,
  getSession,
  setSession,
});

Then modify src/composables/auth.ts to use the sessionVault functions. The goal is to no longer store the auth result in a session variable. Instead, we will use the session vault to store the result and retrieve it from the vault as needed.

Remove the let authResult: AuthResult | null; line and replace it with the following:

import { useSessionVault } from './session-vault';

const { clearSession, getSession, setSession } = useSessionVault();

Create a new local function called saveAuthResult():

const saveAuthResult = async (authResult: AuthResult | null): Promise<void> => {
  if (authResult) {
    await setSession(authResult);
  } else {
    await clearSession();
  }
};

Modify refreshAuth to save the results of an attempted refresh:

const refreshAuth = async (authResult: AuthResult): Promise<AuthResult | null> => {
  let newAuthResult: AuthResult | null;

  if (await AuthConnect.isRefreshTokenAvailable(authResult)) {
    try {
      newAuthResult = await AuthConnect.refreshSession(provider, authResult);
    } catch (err) {
      // You could also log this, or otherwise mark the failure.
      // This app just makes the user redo their login since that is about
      // the only action a user could take.
      null;
    }
    saveAuthResult(newAuthResult);
  }

  return newAuthResult;
};

Modify getAuthResult() to obtain the auth result from the vault:

const getAuthResult = async (): Promise<AuthResult | null> => {
  let authResult = await getSession();
  if (authResult && (await AuthConnect.isAccessTokenExpired(authResult))) {
    authResult = await refreshAuth(authResult);
  }
  return authResult;
};

Finally, modify login() and logout() to both save the results of the operation accordingly:

const login = async (): Promise<void> => {
  await initialize();
  const authResult = await AuthConnect.login(provider, options);
  await saveAuthResult(authResult);
};

const logout = async (): Promise<void> => {
  await initialize();
  const authResult = await getAuthResult();
  if (authResult) {
    await AuthConnect.logout(provider, authResult);
    await saveAuthResult(null);
  }
};

You should now be able to refresh the app and have a persistent session.

Guard the Routes

Let's pretend that Tab2Page and Tab3Page had super secret information that only logged in users could see (they don't, of course, but we can pretend). We would not want users getting there if they were not currently authenticated.

We can use our isAuthenticated() function to build a guard for those routes.

Open src/router/index.ts. At the top of the file, import useAuth.

import { useAuth } from '@/composables/auth';

const { isAuthenticated } = useAuth();

Then add some metadata to the tab2 and tab3 routes to indicate that they require authentication:

      {
        path: 'tab2',
        component: () => import('@/views/Tab2.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: 'tab3',
        component: () => import('@/views/Tab3.vue'),
        meta: { requiresAuth: true },
      },

Create a guard function (you will need to add more import statements):

const checkAuthStatus = async (
  to: RouteLocationNormalized,
  from: RouteLocationNormalized,
  next: NavigationGuardNext
) => {
  if (to.matched.some((r) => r.meta.requiresAuth)) {
    if (!(await isAuthenticated())) {
      return next('/');
    }
  }
  next();
};

Finally, after the router is created, but before it is exported, add the guard:

router.beforeEach(checkAuthStatus);

Now if you are not logged in and try to click on tabs 2 or 3, the application will not navigate and you will stay on tab 1. Furthermore, if you try to manually load http://localhost:8100/tabs/tab2 (or tab3), you will be redirected to tab1.

Conclusion

At this point, you should have a good idea of how Auth Connect and Identity Vault work together to provide a complete and secure authentication solution. There is still more functionality that can be implemented. Be sure to check out our other documentation and demos to see how to expand on this to offer expanded functionality such as Biometric based authentication.

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published