|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amplifyframework.ui.authenticator |
| 17 | + |
| 18 | +import androidx.compose.runtime.Immutable |
| 19 | +import androidx.compose.runtime.Stable |
| 20 | +import com.amplifyframework.auth.AuthCodeDeliveryDetails |
| 21 | +import com.amplifyframework.auth.AuthException |
| 22 | +import com.amplifyframework.auth.AuthUser |
| 23 | +import com.amplifyframework.auth.AuthUserAttribute |
| 24 | +import com.amplifyframework.auth.result.AuthSignOutResult |
| 25 | +import com.amplifyframework.ui.authenticator.enums.AuthenticatorInitialStep |
| 26 | +import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep |
| 27 | +import com.amplifyframework.ui.authenticator.forms.MutableFormState |
| 28 | + |
| 29 | +/** |
| 30 | + * A Screen State is a state holder for the UI for a specific [AuthenticatorStep]. |
| 31 | + */ |
| 32 | +@Stable |
| 33 | +interface AuthenticatorScreenState { |
| 34 | + val step: AuthenticatorStep |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * The Authenticator is loading the current state of the user's Auth session. |
| 39 | + */ |
| 40 | +@Immutable |
| 41 | +object LoadingState : AuthenticatorScreenState { |
| 42 | + override val step = AuthenticatorStep.Loading |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * The Authenticator has encountered an unrecoverable error. |
| 47 | + * @param error The error that occurred. |
| 48 | + */ |
| 49 | +@Immutable |
| 50 | +data class ErrorState(val error: AuthException) : AuthenticatorScreenState { |
| 51 | + override val step = AuthenticatorStep.Error |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * The user has completed the sign in process. |
| 56 | + */ |
| 57 | +@Immutable |
| 58 | +interface SignedInState : AuthenticatorScreenState { |
| 59 | + /** |
| 60 | + * The [AuthUser] instance for the signed in user. |
| 61 | + */ |
| 62 | + val user: AuthUser |
| 63 | + |
| 64 | + /** |
| 65 | + * Sign out the current user. This does a local sign out and returns the [AuthSignOutResult] that |
| 66 | + * may be inspected to determine if any parts of the sign out were unsuccessful. |
| 67 | + */ |
| 68 | + suspend fun signOut(): AuthSignOutResult |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * An [AuthenticatorScreenState] for a part of UI that contains an input form. |
| 73 | + */ |
| 74 | +@Stable |
| 75 | +interface FormHolderState : AuthenticatorScreenState { |
| 76 | + val form: MutableFormState |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * The user is on the Sign In step. They can enter their Sign In information to authenticate with Amplify. |
| 81 | + */ |
| 82 | +@Stable |
| 83 | +interface SignInState : FormHolderState { |
| 84 | + fun moveTo(step: AuthenticatorInitialStep) |
| 85 | + suspend fun signIn() |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * The user has completed the initial Sign In step, and needs to enter the confirmation code from an MFA |
| 90 | + * message to complete the sign in process. |
| 91 | + */ |
| 92 | +@Stable |
| 93 | +interface SignInConfirmMfaState : FormHolderState { |
| 94 | + val deliveryDetails: AuthCodeDeliveryDetails? |
| 95 | + fun moveTo(step: AuthenticatorInitialStep) |
| 96 | + suspend fun confirmSignIn() |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * The user has completed the initial Sign In step, and needs to enter the confirmation code from a custom |
| 101 | + * challenge to complete the sign in process. |
| 102 | + */ |
| 103 | +@Stable |
| 104 | +interface SignInConfirmCustomState : FormHolderState { |
| 105 | + val additionalInfo: Map<String, String> |
| 106 | + val deliveryDetails: AuthCodeDeliveryDetails? |
| 107 | + fun moveTo(step: AuthenticatorInitialStep) |
| 108 | + suspend fun confirmSignIn() |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * The user has completed the initial Sign In step, and is required to change their password in order to complete |
| 113 | + * the sign in process. |
| 114 | + */ |
| 115 | +@Stable |
| 116 | +interface SignInConfirmNewPasswordState : FormHolderState { |
| 117 | + fun moveTo(step: AuthenticatorInitialStep) |
| 118 | + suspend fun confirmSignIn() |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * The user is on the Sign Up step, and can fill out the account creation form to Sign Up. |
| 123 | + */ |
| 124 | +@Stable |
| 125 | +interface SignUpState : FormHolderState { |
| 126 | + fun moveTo(step: AuthenticatorInitialStep) |
| 127 | + suspend fun signUp() |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * The user has signed up, but needs to enter a confirmation code sent to them. |
| 132 | + */ |
| 133 | +@Stable |
| 134 | +interface SignUpConfirmState : FormHolderState { |
| 135 | + val deliveryDetails: AuthCodeDeliveryDetails? |
| 136 | + fun moveTo(step: AuthenticatorInitialStep) |
| 137 | + suspend fun confirmSignUp() |
| 138 | + suspend fun resendCode() |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * The user is on the Password Reset step. They can enter their username to begin the password reset. |
| 143 | + */ |
| 144 | +@Stable |
| 145 | +interface PasswordResetState : FormHolderState { |
| 146 | + fun moveTo(step: AuthenticatorInitialStep) |
| 147 | + suspend fun submitPasswordReset() |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * The user has entered their username and been sent a confirmation code. The need to enter the code and their new |
| 152 | + * password to complete the password reset. |
| 153 | + */ |
| 154 | +@Stable |
| 155 | +interface PasswordResetConfirmState : FormHolderState { |
| 156 | + val deliveryDetails: AuthCodeDeliveryDetails? |
| 157 | + fun moveTo(step: AuthenticatorInitialStep) |
| 158 | + suspend fun submitPasswordResetConfirm() |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * The user has successfully signed in and their account is confirmed, however they do not have any means of account recovery (email, phone) that is confirmed. |
| 163 | + */ |
| 164 | +@Stable |
| 165 | +interface VerifyUserState : FormHolderState { |
| 166 | + /** |
| 167 | + * The list of unverified attributes. |
| 168 | + */ |
| 169 | + val attributes: List<AuthUserAttribute> |
| 170 | + |
| 171 | + /** |
| 172 | + * Submit the selected attribute to initiate the attribute verification. |
| 173 | + */ |
| 174 | + suspend fun verifyUser() |
| 175 | + |
| 176 | + /** |
| 177 | + * Skip verification and move to the Signed In state |
| 178 | + */ |
| 179 | + fun skip() |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * The user has initiated verification of an account recovery mechanism (email, phone) and needs to provide a confirmation code. |
| 184 | + */ |
| 185 | +@Stable |
| 186 | +interface VerifyUserConfirmState : FormHolderState { |
| 187 | + /** |
| 188 | + * The details of where the verification code was sent. |
| 189 | + */ |
| 190 | + val deliveryDetails: AuthCodeDeliveryDetails? |
| 191 | + |
| 192 | + /** |
| 193 | + * Submit the entered confirmation code to confirm the verification. |
| 194 | + */ |
| 195 | + suspend fun confirmVerifyUser() |
| 196 | + |
| 197 | + /** |
| 198 | + * Re-send the verification code. |
| 199 | + */ |
| 200 | + suspend fun resendCode() |
| 201 | + |
| 202 | + /** |
| 203 | + * Skip verification and move to the Signed In state |
| 204 | + */ |
| 205 | + fun skip() |
| 206 | +} |
0 commit comments