Skip to content

Commit c99ee17

Browse files
authored
Merge pull request #155 from android/check_server_session
Check session ID validity with server before auto sign-in checks. Also disable passkey prompt after splash screen
2 parents b7dcbfc + 2a9a162 commit c99ee17

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

Shrine/app/src/main/java/com/authentication/shrine/repository/AuthRepository.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,29 @@ class AuthRepository @Inject constructor(
434434
return false
435435
}
436436

437+
/**
438+
* Checks if session id is valid with server. Currently makes a getKeys() request
439+
*
440+
* @return True if the session id is valid, false otherwise.
441+
*/
442+
suspend fun isSessionIdValid(): Boolean {
443+
val sessionId = dataStore.read(SESSION_ID)
444+
if (!sessionId.isNullOrBlank()) {
445+
try {
446+
val apiResult = authApiService.getKeys(
447+
cookie = sessionId.createCookieHeader(),
448+
)
449+
if (apiResult.isSuccessful) {
450+
return true
451+
}
452+
} catch (e: ApiException) {
453+
Log.e(TAG, "Cannot call getKeys for isSessionIdValid")
454+
}
455+
}
456+
457+
return false
458+
}
459+
437460
/**
438461
* Sets the sign-in state.
439462
*

Shrine/app/src/main/java/com/authentication/shrine/ui/viewmodel/SplashViewModel.kt

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ class SplashViewModel @Inject constructor(
4242

4343
init {
4444
viewModelScope.launch {
45-
val startDestination = if (isSignedInThroughPassword()) {
46-
if (isSignedInThroughPasskeys()) {
45+
val startDestination =
46+
if (isSessionIdValid()) {
4747
ShrineAppDestinations.MainMenuRoute.name
4848
} else {
49-
ShrineAppDestinations.CreatePasskeyRoute.name
49+
signOut()
50+
ShrineAppDestinations.AuthRoute.name
5051
}
51-
} else {
52-
ShrineAppDestinations.AuthRoute.name
53-
}
5452

5553
_uiState.update {
5654
SplashScreenState(
@@ -62,27 +60,21 @@ class SplashViewModel @Inject constructor(
6260
}
6361

6462
/**
65-
* Checks if the user is signed in using a password.
66-
*
67-
* This method checks if the user is signed in by calling the `isSignedIn()` method of the
68-
* [AuthRepository].
63+
* Checks if the session ID is valid with the server.
6964
*
70-
* @return True if the user is signed in using a password, false otherwise.
65+
* @return True if the session ID is valid, false otherwise.
7166
*/
72-
private suspend fun isSignedInThroughPassword(): Boolean {
73-
return repository.isSignedInThroughPassword()
67+
private suspend fun isSessionIdValid(): Boolean {
68+
return repository.isSessionIdValid()
7469
}
7570

7671
/**
77-
* Checks if the user is signed in using passkeys.
78-
*
79-
* This method checks if the user is signed in through passkeys by calling the
80-
* `isSignedInThroughPasskeys()` method of the [AuthRepository].
81-
*
82-
* @return True if the user is signed in using passkeys, false otherwise.
72+
* Signs out the user.
8373
*/
84-
private suspend fun isSignedInThroughPasskeys(): Boolean {
85-
return repository.isSignedInThroughPasskeys()
74+
private fun signOut() {
75+
viewModelScope.launch {
76+
repository.signOut()
77+
}
8678
}
8779
}
8880

0 commit comments

Comments
 (0)