# AHA auth0 action helper

## Create custom actions in auth0

1. login auth0 account
2. create a action: e.g. `actions` &rarr; `Flows` &rarr; `Login` &rarr; `Add Action` &rarr; `Build Custom`
3. initialize the action:
    ```
    name = Post Login
    Trigger = Login / Post Login
    Runtime = Node 16
    ```
4. click `Create`

## Add dependencies

1. click the cube icon in the left side of codes
2. click `Add Dependency`
    ```
    name = @earnaha/auth0-action-helper
    version = 1.0.21 or latest
    ```
3. click `Create`

## Setup event secrets

1. click the key icon in the left side of codes
2. click `Add Secret`, input keys and values like below, and then click `Create` for each one.
    - ENV = local, dev, test, stage, alpha, beta, gamma, prod, ...etc.
    - SERVICE = any string to distinguish your sites or services.
    - DOMAIN = https://subdomain.yourdomain.com (backend url, https required)
    - ACCESS_KEY = the secret key to access your backend api, encode string more than 25 letters
    - ACCESS_SALT = the secret salt to access your backend api, a number more than 10 digits
    - AUTH0_DOMAIN = `Applications` &rarr; `Applications` &rarr; `Machine to Machine App` &rarr; `Settings` &rarr; `Basic Information` &rarr; `Domain`
    - AUTH0_CLIENT_ID = `Applications` &rarr; `Applications` &rarr; `Machine to Machine App` &rarr; `Settings` &rarr; `Basic Information` &rarr; `Client ID`
    - AUTH0_CLIENT_SECRET = `Applications` &rarr; `Applications` &rarr; `Machine to Machine App` &rarr; `Settings` &rarr; `Basic Information` &rarr; `Client Secret`
    - SENTRY_DSN = the sentry dns for logging, a sentry account is required. (https://sentry.io/)
    - SENTRY_TRACES_SAMPLE_RATE = the sentry trace setting. e.g. 0.1, 1.0, ...etc.
    - SENTRY_LOGGER_LEVEL = the sentry logger level setting. e,g, debug, info, ...etc.
    - LINK_ACCOUNT_TIME = YYYY-MM-DD, account created after this date, merge accounts if having the same email.

## Code in actions

### Post login

make an API in your backend side with the route _/auth/v3/login_ to handle the signed in/up account data.

```
const AhaHelper = require('@earnaha/auth0-action-helper');
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
	try {
		console.log('onExecutePostLogin event=',JSON.stringify(event));
		console.log('onExecutePostLogin api=',JSON.stringify(api));
		const helper = new AhaHelper.PostLoginHelper(event.secrets);
		const res = await helper.exec(event, api);
		console.log('onExecutePostLogin res=',res);
	} catch (e) {
		const errMsg = e?.response?.data || e?.message || `${e}`;
		console.error('onExecutePostLogin error=', errMsg);
		throw e;
	}
};


/**
* Handler that will be invoked when this action is resuming after an external redirect. If your
* onExecutePostLogin function does not perform a redirect, this function can be safely ignored.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
// exports.onContinuePostLogin = async (event, api) => {
// };

```

### Post change password

make an API in your backend side with the route _/auth/v3/refer/invitation/accept_ to handle the invitation acceptance things after the invited account signs in/up.

```
const AhaHelper = require('@earnaha/auth0-action-helper');
/**
* Handler that will be called during the execution of a PostChangePassword flow.
*
* @param {Event} event - Details about the user and the context in which the change password is happening.
* @param {PostChangePasswordAPI} api - Methods and utilities to help change the behavior after a user changes their password.
*/
exports.onExecutePostChangePassword = async (event, api) => {
	try {
		console.log('onExecutePostChangePassword event=',JSON.stringify(event));
		console.log('onExecutePostChangePassword api=',JSON.stringify(api));
		const helper = new AhaHelper.PostChangePasswordHelper(event.secrets);
		const res = await helper.exec(event, api);
		console.log('onExecutePostChangePassword res=',JSON.stringify(res));
	} catch (e) {
		const errMsg = e?.response?.data || e?.message || `${e}`;
		console.error('onExecutePostChangePassword error=', errMsg);
		throw e;
	}
};
```

### Post user registration

currently no further handling in the exec function.

```
const AhaHelper = require('@earnaha/auth0-action-helper');
/**
* Handler that will be called during the execution of a PostUserRegistration flow.
*
* @param {Event} event - Details about the context and user that has registered.
* @param {PostUserRegistrationAPI} api - Methods and utilities to help change the behavior after a signup.
*/
exports.onExecutePostUserRegistration = async (event, api) => {
  try {
		console.log('onExecutePostUserRegistration event=',JSON.stringify(event));
		console.log('onExecutePostUserRegistration api=',JSON.stringify(api));
		const helper = new AhaHelper.PostUserRegistrationHelper(event.secrets);
		const res = await helper.exec(event, api);
		console.log('onExecutePostUserRegistration res=',JSON.stringify(res));
	} catch (e) {
		const errMsg = e?.response?.data || e?.message || `${e}`;
		console.error('onExecutePostUserRegistration error=', errMsg);
		throw e;
	}
};
```

### Pre user registration

currently no further handling in the exec function.

```
const AhaHelper = require('@earnaha/auth0-action-helper');
/**
* Handler that will be called during the execution of a PreUserRegistration flow.
*
* @param {Event} event - Details about the context and user that is attempting to register.
* @param {PreUserRegistrationAPI} api - Interface whose methods can be used to change the behavior of the signup.
*/
exports.onExecutePreUserRegistration = async (event, api) => {
  try {
		console.log('onExecutePreUserRegistration event=',JSON.stringify(event));
		console.log('onExecutePreUserRegistration api=',JSON.stringify(api));
		const helper = new AhaHelper.PreUserRegistrationHelper(event.secrets);
		const res = await helper.exec(event, api);
		console.log('onExecutePreUserRegistration res=',res);
	} catch (e) {
		const errMsg = e?.response?.data || e?.message || `${e}`;
		console.error('onExecutePreUserRegistration error=', errMsg);
		throw e;
	}
};
```

## Deploy and test
