Skip to content

Commit

Permalink
Merge pull request #27 from Achintha444/main
Browse files Browse the repository at this point in the history
docs: Update android docs to add missing details
  • Loading branch information
Achintha444 authored May 22, 2024
2 parents 1d3c808 + ac8a130 commit 8c7b02b
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions docs/website/android/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ val authenticationProvider: AuthenticationProvider = asgardeoAuth.getAuthenticat
- **AuthenticationState.Authenticated**: User is authenticated.
- **AuthenticationState.Error**: An error occurred during the authentication flow.

3. To start the authentication process, call `authenticationProvider.isLoggedInStateFlow`, this will check if there is an active session available, and if available, the authentication state will emit **AuthenticationState.Authenticated**, else will emit **AuthenticationState.Initial**.
3. To start the authentication process, call `authenticationProvider.getAuthenticationStateFlow`, this will act as the observer for the authentication state changes.

After that, you can call the `authenticationProvider.initializeAuthentication` to initialize the authentication process.
```kotlin
val state = authenticationProvider.getAuthenticationStateFlow()
```

After that, you can call `authenticationProvider.isLoggedInStateFlow`, this will check if there is an active session available, and if available, the authentication state will emit **AuthenticationState.Authenticated**, else will emit **AuthenticationState.Initial**. Then call the `authenticationProvider.initializeAuthentication` to initialize the authentication process when the state is **AuthenticationState.Initial**.

> [!IMPORTANT]
> All the suspend functions should be called inside a coroutine scope. Suspended functions in the SDK are designed to be optimezed for `Dispatchers.IO` context.
```kotlin
@Composable
Expand All @@ -60,38 +67,50 @@ internal fun LandingScreen() {
// Initiate a call to /authorize endpoint only if a valid AT is not available
authenticationProvider.isLoggedInStateFlow(context) // [!code highlight]
handleAuthenticationState(state)
isLoggedInStateFlow()
}

private fun isLoggedInStateFlow() {
GlobalScope.launch {
authenticationProvider.isLoggedInStateFlow(context) // [!code highlight]
_state.update { landingScreenState ->
landingScreenState.copy(isLoading = false)
}
}
}

private fun handleAuthenticationState(state: AuthenticationState) {
authStateJob = state.collect {
when (it) {
is AuthenticationState.Initial -> {
// pre /authorize
authenticationProvider.initializeAuthentication(context) // [!code highlight]
}
is AuthenticationState.Unauthenticated -> {
/**
* Gets called when /authorize and /authn responds with an “INCOMPLETE” state.
* This means authentication flow is still not completed and a particular step is getting
* challenged for authentication.
*/
LoginForm(it.authenticationFlow)
}
is AuthenticationState.Error -> {
/**
* Gets called when /authorize and /authn responds with an “FAILED_INCOMPLETE” state
* which responds at an error of a particular authentication step
*/
}
is AuthenticationState.Authenticated -> {
/**
* Gets called when /authn responds with an “SUCCESS” state. This means
* authentication flow is completed
*/
onSuccessfulLogin()
}
is AuthenticationState.Loading -> {
// Show loading
GlobalScope.launch {
state.collect {
when (it) {
is AuthenticationState.Initial -> {
// pre /authorize
authenticationProvider.initializeAuthentication(context) // [!code highlight]
}
is AuthenticationState.Unauthenticated -> {
/**
* Gets called when /authorize and /authn responds with an “INCOMPLETE” state.
* This means authentication flow is still not completed and a particular step is getting
* challenged for authentication.
*/
LoginForm(it.authenticationFlow)
}
is AuthenticationState.Error -> {
/**
* Gets called when /authorize and /authn responds with an “FAILED_INCOMPLETE” state
* which responds at an error of a particular authentication step
*/
}
is AuthenticationState.Authenticated -> {
/**
* Gets called when /authn responds with an “SUCCESS” state. This means
* authentication flow is completed
*/
onSuccessfulLogin()
}
is AuthenticationState.Loading -> {
// Show loading
}
}
}
}
Expand All @@ -104,7 +123,7 @@ Assuming that you have configured Username and Password as the first authenticat
* Assuming the authentication process is, basic as first factor and TOTP as second factor
*/
@Composable
internal fun LoginForm() {
internal fun LoginForm(
authenticationFlow: AuthenticationFlowNotSuccess,
onSuccessfulLogin: (User) -> Unit
) {
Expand Down

0 comments on commit 8c7b02b

Please sign in to comment.