-
Notifications
You must be signed in to change notification settings - Fork 0
Feature auth refactor #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c125a47
feat(auth_registration): Implement user registration screen and logic
22dd8c0
feat(auth): Implement user registration and add tests
a00eac3
feat(auth): Implement user registration and add tests
e3ab8c9
feat(auth): Implement user registration and add tests
1402971
Merge remote-tracking branch 'origin/featureAuth-Register' into featu…
1233ecc
feat(auth): Implement password visibility toggle
1f72627
refactor(auth): Standardize error handling with a generic Result class
32c0b77
refactor(auth): Clean up RegisterScreen layout
f3b09fa
refactor(auth): Standardize error messages using `UiText` and string …
DemisChan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
core/domain/util/src/main/java/com/dmd/tasky/core/domain/util/UiText.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.dmd.tasky.core.domain.util | ||
|
|
||
| import android.content.Context | ||
| import androidx.annotation.StringRes | ||
|
|
||
| sealed class UiText { | ||
| data class DynamicString(val value: String) : UiText() | ||
| class StringResource( | ||
| @StringRes val resId: Int, | ||
| vararg val args: Any | ||
| ) : UiText() | ||
|
|
||
| fun asString(context: Context): String { | ||
| return when (this) { | ||
| is DynamicString -> value | ||
| is StringResource -> context.getString(resId, *args) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
features/auth/src/main/java/com/dmd/tasky/feature/auth/data/remote/AuthApi.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| package com.dmd.tasky.feature.auth.data.remote | ||
|
|
||
| import com.dmd.tasky.feature.auth.data.remote.dto.LoginRequest | ||
| import com.dmd.tasky.feature.auth.data.remote.dto.RegisterRequest | ||
| import retrofit2.Response | ||
| import retrofit2.http.Body | ||
| import retrofit2.http.POST | ||
|
|
||
| interface AuthApi { | ||
| @POST("auth/login") | ||
| suspend fun login(@Body request: LoginRequest): AuthResponse | ||
| } | ||
|
|
||
| @POST("auth/register") | ||
| suspend fun register(@Body request: RegisterRequest): Response<Unit> | ||
| } |
3 changes: 1 addition & 2 deletions
3
.../feature/auth/data/remote/LoginRequest.kt → ...ture/auth/data/remote/dto/LoginRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
features/auth/src/main/java/com/dmd/tasky/feature/auth/data/remote/dto/RegisterRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.dmd.tasky.feature.auth.data.remote.dto | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class RegisterRequest( | ||
| val fullName: String, | ||
| val email: String, | ||
| val password: String | ||
| ) |
73 changes: 68 additions & 5 deletions
73
...es/auth/src/main/java/com/dmd/tasky/feature/auth/data/repository/DefaultAuthRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,82 @@ | ||
| package com.dmd.tasky.feature.auth.data.repository | ||
|
|
||
| import com.dmd.tasky.core.domain.util.Result | ||
| import com.dmd.tasky.feature.auth.data.remote.AuthApi | ||
| import com.dmd.tasky.feature.auth.data.remote.LoginRequest | ||
| import com.dmd.tasky.feature.auth.data.remote.dto.LoginRequest | ||
| import com.dmd.tasky.feature.auth.data.remote.dto.RegisterRequest | ||
| import com.dmd.tasky.feature.auth.domain.AuthRepository | ||
| import com.dmd.tasky.feature.auth.domain.model.AuthError | ||
| import com.dmd.tasky.feature.auth.domain.model.LoginResult | ||
| import com.dmd.tasky.feature.auth.domain.model.RegisterResult | ||
| import kotlinx.coroutines.CancellationException | ||
| import retrofit2.HttpException | ||
| import timber.log.Timber | ||
| import java.io.IOException | ||
| import java.net.SocketTimeoutException | ||
|
|
||
| class DefaultAuthRepository(private val api: AuthApi) : AuthRepository { | ||
| override suspend fun login(email: String, password: String): LoginResult { | ||
| return try { | ||
| val response = api.login(LoginRequest(email, password)) | ||
| LoginResult.Success(response.accessToken) | ||
| Result.Success(response.accessToken) | ||
| } catch (e: HttpException) { | ||
| val code = e.code() | ||
| val errorBody = e.response()?.errorBody()?.string() | ||
| Timber.e("HTTP Error: Code=$code, Body=$errorBody") | ||
| when (code) { | ||
| in 500..599 -> Result.Error(AuthError.Network.SERVER_ERROR) | ||
| 401 -> Result.Error(AuthError.Auth.INVALID_CREDENTIALS) | ||
| else -> Result.Error(AuthError.Network.UNKNOWN) | ||
| } | ||
| } catch (e: SocketTimeoutException) { | ||
| Timber.e("Timeout error: ${e.message}") | ||
| Result.Error(AuthError.Network.TIMEOUT) | ||
| } catch (e: IOException) { | ||
| Timber.e("Network error: ${e.message}") | ||
| Result.Error(AuthError.Network.NO_INTERNET) | ||
| } catch (e: Exception) { | ||
| if(e is CancellationException) throw e | ||
| LoginResult.Error(e.message ?: "Unknown error") | ||
| Timber.e("Exception during login: ${e.message}") | ||
| if (e is CancellationException) throw e | ||
| Result.Error(AuthError.Network.UNKNOWN) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun register( | ||
| fullName: String, | ||
| email: String, | ||
| password: String | ||
| ): RegisterResult { | ||
| return try { | ||
| Timber.d("Attempting register with: name='$fullName', email='$email'") | ||
| val response = api.register( | ||
| RegisterRequest( | ||
| fullName = fullName, | ||
| email = email, | ||
| password = password | ||
| ) | ||
| ) | ||
| Timber.d("Register successful! Status code: ${response.code()}") | ||
| Result.Success(Unit) | ||
| } catch (e: HttpException) { | ||
| val code = e.code() | ||
| val errorBody = e.response()?.errorBody()?.string() | ||
| Timber.e("HTTP Error: Code=$code, Body=$errorBody") | ||
| when (code) { | ||
| 409 -> Result.Error(AuthError.Auth.USER_ALREADY_EXISTS) | ||
| 400 -> Result.Error(AuthError.Auth.VALIDATION_FAILED) | ||
| in 500..599 -> Result.Error(AuthError.Network.SERVER_ERROR) | ||
| else -> Result.Error(AuthError.Network.UNKNOWN) | ||
| } | ||
| } catch (e: SocketTimeoutException) { | ||
| Timber.e("Timeout error: ${e.message}") | ||
| Result.Error(AuthError.Network.TIMEOUT) | ||
| } catch (e: IOException) { | ||
| Timber.e("Network error: ${e.message}") | ||
| Result.Error(AuthError.Network.NO_INTERNET) | ||
| } catch (e: Exception) { | ||
| Timber.e("Exception during register: ${e.message}") | ||
| if (e is CancellationException) throw e | ||
| Result.Error(AuthError.Network.UNKNOWN) | ||
| } | ||
| } | ||
| } | ||
4 changes: 3 additions & 1 deletion
4
features/auth/src/main/java/com/dmd/tasky/feature/auth/domain/AuthRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package com.dmd.tasky.feature.auth.domain | ||
|
|
||
| import com.dmd.tasky.feature.auth.domain.model.LoginResult | ||
| import com.dmd.tasky.feature.auth.domain.model.RegisterResult | ||
|
|
||
| interface AuthRepository { | ||
| suspend fun login(email: String, password: String): LoginResult | ||
| } | ||
| suspend fun register(fullName: String, email: String, password: String): RegisterResult | ||
| } |
27 changes: 27 additions & 0 deletions
27
features/auth/src/main/java/com/dmd/tasky/feature/auth/domain/model/AuthError.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.dmd.tasky.feature.auth.domain.model | ||
|
|
||
| import com.dmd.tasky.core.domain.util.EmptyResult | ||
| import com.dmd.tasky.core.domain.util.Error | ||
| import com.dmd.tasky.core.domain.util.Result | ||
|
|
||
| sealed interface AuthError : Error { | ||
| enum class Network : AuthError { | ||
| NO_INTERNET, // IOException | ||
| TIMEOUT, // SocketTimeoutException | ||
| SERVER_ERROR, // HTTP 500/502/503 | ||
| UNKNOWN // Unexpected exceptions | ||
| } | ||
|
|
||
| enum class Auth : AuthError { | ||
| INVALID_CREDENTIALS, // 401 for login | ||
| USER_ALREADY_EXISTS, // 409 for register | ||
| VALIDATION_FAILED // 400 for register | ||
| } | ||
| } | ||
|
|
||
|
|
||
| typealias LoginResult = Result<String, AuthError> | ||
|
|
||
| typealias RegisterResult = EmptyResult<AuthError> | ||
|
|
||
| typealias LogoutResult = EmptyResult<AuthError> |
7 changes: 0 additions & 7 deletions
7
features/auth/src/main/java/com/dmd/tasky/feature/auth/domain/model/LoginResult.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we maybe look for a way to avoid duplicating this try/catch logic in every API call? 😊
We could extract it into a
safeCallutility that wraps the network request and handles all the exceptions and response mapping in one place. Something like this:That way, the repository methods become much cleaner - something like:
In the other hand, I’d stick with the approach Philipp video and you referenced in the slack - it’s much simpler since the backend already assumes a consistent response structure across features. That way, you can handle everything in the presentation layer instead of creating separate classes or enums for each feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so if I stick to the approach on the video ...this is what i have done right?
if i understood correctly?