|
| 1 | +# Device Activation |
| 2 | + |
| 3 | +With `WDOActivationService` you can onboard PowerAuth with just a piece of user information like his email, phone number, or login name. |
| 4 | + |
| 5 | +PowerAuth enrolled in such a way will need [further user verification](Verifying-User.md) until fully operational (able to sign operations). |
| 6 | + |
| 7 | +## Example app flow |
| 8 | + |
| 9 | +<p align="center"><img src="images/activation-mockup.png" alt="Example application flow for the activation" width="100%" /></p> |
| 10 | + |
| 11 | +## Creating an instance |
| 12 | + |
| 13 | +To create an instance you will need a `PowerAuth` instance that is __ready to be activated__. |
| 14 | + |
| 15 | +<!-- begin box info --> |
| 16 | +[Documentation for `PowerAuth Mobile JS SDK`](https://developers.wultra.com/components/react-native-powerauth-mobile-sdk/). |
| 17 | +<!-- end --> |
| 18 | + |
| 19 | + |
| 20 | +Example with `PowerAuth` instance: |
| 21 | + |
| 22 | +```typescript |
| 23 | +const powerAuth = new PowerAuth("my-pa-instance") |
| 24 | +powerAuth.configure({ |
| 25 | + configuration: "ARCB+...jg==", // base64 PowerAuth configuration |
| 26 | + baseEndpointUrl: "https://my-server-deployment.com/enrollment-server/" |
| 27 | +}) |
| 28 | +const activationService = new WDOActivationService( |
| 29 | + powerAuth, |
| 30 | + "https://my-server-deployment.com/enrollment-server-onboarding/" |
| 31 | +) |
| 32 | +``` |
| 33 | + |
| 34 | +## Retrieving the status |
| 35 | + |
| 36 | +To figure out if the activation process was already started and what is the status, you can use `hasActiveProcess`. |
| 37 | + |
| 38 | +```typescript |
| 39 | +/** |
| 40 | + * If the activation process is in progress. |
| 41 | + * |
| 42 | + * Note that even if this property is `true` it can be already discontinued on the server. |
| 43 | + * Calling `status()` for example after the app is launched in this case is recommended. |
| 44 | + */ |
| 45 | +get hasActiveProcess(): boolean; |
| 46 | +``` |
| 47 | + |
| 48 | +If the process was started, you can verify its status by calling the `status` function. You can show an appropriate UI to the user based on this status. |
| 49 | + |
| 50 | +```typescript |
| 51 | +/** |
| 52 | + * Retrieves status of the onboarding activation. |
| 53 | + * |
| 54 | + * @return Promise resolved with onboarding status. |
| 55 | + */ |
| 56 | +status(): Promise<WDOOnboardingStatus>; |
| 57 | +``` |
| 58 | + |
| 59 | +`WDOOnboardingStatus` possible values. |
| 60 | + |
| 61 | +```typescript |
| 62 | +declare enum WDOOnboardingStatus { |
| 63 | + /** Activation part of the process is in progress */ |
| 64 | + activationInProgress = "ACTIVATION_IN_PROGRESS", |
| 65 | + /** Verification part of the process is in progress */ |
| 66 | + verificationInProgress = "VERIFICATION_IN_PROGRESS", |
| 67 | + /** Onboarding process has failed */ |
| 68 | + failed = "FAILED", |
| 69 | + /** Onboarding process is completed */ |
| 70 | + finished = "FINISHED" |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Starting the process |
| 75 | + |
| 76 | +To start the activation process, you can use any credentials that are sufficient to you that can identify the user. |
| 77 | + |
| 78 | +Often, such data are user email, phone number, or userID. The definition of such data is up to your server implementation and requirements. |
| 79 | + |
| 80 | +To start the activation, use the `start` function. |
| 81 | + |
| 82 | +```typescript |
| 83 | +/** |
| 84 | + * Start onboarding activation with user credentials. |
| 85 | + * |
| 86 | + * For example, when you require email, your object would look like this: |
| 87 | + * ``` |
| 88 | + * { |
| 89 | + * email: "<user_email>" |
| 90 | + * } |
| 91 | + * ``` |
| 92 | + * @param credentials Object with credentials. Which credentials are needed should be provided by a system/backend provider. |
| 93 | + * @param processType The process type identification. If not specified, the default process type will be used. |
| 94 | + */ |
| 95 | + start(credentials: any, processType?: string): Promise<void>; |
| 96 | +``` |
| 97 | + |
| 98 | +### Example |
| 99 | + |
| 100 | +```typescript |
| 101 | + |
| 102 | +class MyUserService { |
| 103 | + // prepared service |
| 104 | + private activationService: WDOActivationService |
| 105 | + |
| 106 | + async startActivation(id: string) { |
| 107 | + const data = { userId: id} |
| 108 | + try { |
| 109 | + await this.activationService.start(data) |
| 110 | + // success, continue with `activate()` |
| 111 | + // at this moment, the `hasActiveProcess` starts return true |
| 112 | + } catch (error) { |
| 113 | + // show error to the user |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Creating the activation |
| 120 | + |
| 121 | +To activate the user (activating the `PowerAuth` instance), data retrieved from the process start can be used with optional `OTP`. The OTP is usually sent via SMS, email, or other channel. To decide if the OTP is needed, you can use the [Configuration API](Process-Configuration.md) or have it hardcoded. |
| 122 | + |
| 123 | +Use the `activate` function to create the activation. |
| 124 | + |
| 125 | +```typescript |
| 126 | +/** |
| 127 | + * Activate the PowerAuth instance that was passed in the initializer. |
| 128 | + * |
| 129 | + * @param activationName Name of the activation. Device name by default (usually something like John's iPhone or similar). |
| 130 | + * @param otp OTP code received by the user (via SMS or email). Optional when not required. |
| 131 | + * @return Promise resolved with activation result. |
| 132 | + */ |
| 133 | +activate(activationName: string, otp?: string): Promise<WDOPowerAuthActivationResult>; |
| 134 | +``` |
| 135 | + |
| 136 | +Example implementation: |
| 137 | + |
| 138 | +```typescript |
| 139 | +class MyUserService { |
| 140 | + // prepared service |
| 141 | + private activationService: WDOActivationService |
| 142 | + |
| 143 | + async activate(smsOTP?: string) { |
| 144 | + try { |
| 145 | + const activationResult = await activationService.activate("my-test-activation", smsOTP) |
| 146 | + // PowerAuthSDK instance was activated. |
| 147 | + // At this moment, navigate the user to |
| 148 | + // the PIN keyboard to finish the PowerAuthSDK initialization. |
| 149 | + // For more information, follow the PowerAuthSDK documentation. |
| 150 | + } catch (error) { |
| 151 | + if (allowOnboardingOtpRetry(error)) { |
| 152 | + // User entered the wrong OTP, prompt for a new one. |
| 153 | + // Remaining OTP attempts count: onboardingOtpRemainingAttempts(error) |
| 154 | + } else { |
| 155 | + // show error UI |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Canceling the process |
| 163 | + |
| 164 | +To cancel the process, just call the `cancel` function. |
| 165 | + |
| 166 | +```typescript |
| 167 | +/** |
| 168 | + * Cancel the activation process (issues a cancel request to the backend and clears the local process ID). |
| 169 | + * |
| 170 | + * @param forceCancel When true, the process will be canceled in the SDK even when fails on backend. `true` by default. |
| 171 | + */ |
| 172 | +cancel(forceCancel?: boolean): Promise<void>; |
| 173 | +``` |
| 174 | + |
| 175 | +## OTP resend |
| 176 | + |
| 177 | +In some cases, you need to resent the OTP: |
| 178 | + - OTP was not received by the user (for example when the email ends in the spam folder). |
| 179 | + - OTP expired. |
| 180 | + |
| 181 | + For such cases, use `resendOTP` function. |
| 182 | + |
| 183 | + ```typescript |
| 184 | +/** |
| 185 | +* OTP resend request. |
| 186 | +* |
| 187 | +* This is intended to be displayed for the user to use in case of the OTP is not received. |
| 188 | +* For example, when the user does not receive SMS after some time, there should be a button to "send again". |
| 189 | +*/ |
| 190 | +resendOTP(): Promise<void>; |
| 191 | + ``` |
| 192 | + |
| 193 | +## Read next |
| 194 | +- [Verifying User With Document Scan And Genuine Presence Check](Verifying-User.md) |
0 commit comments