Want to integrate auth0 in ionic using capacitor

Viewed 897

I want to integrate auth0 in my ionic4 application. I think I am supposed to have separate implementation of auth0 for web(pwa) and android/ios. I am using capacitor and I am not able to find an appropriate solution. Is there any way in which I can maintain a single code base for all platforms?

1 Answers

My solution is to use Auth0 API to handle appropriate actions: Sign Up, Sign In, Sign Out and Renew Token. You can find detailed description for lots of actions at Auth0 docs.

So, I managed to create utility functions like the following one:

import environment from 'environment';

import * as constants from './constants';

const signUp = async (email: string, password: string): Promise<any> => {
  const resonse = await fetch(constants.AUTH0_ENDPOINTS.SIGN_UP, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
    },
    body: JSON.stringify({
      client_id: environment.AUTH0_CLIENT_ID,
      connection: constants.AUTH0_DATABASE_NAME,
      email,
      password,
    }),
  });

  return resonse.json();
};

export default signUp;

Also I implemented Auth class that is responsible for session operating, refreshing token and silent auth.

Related