-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feat/authentication/view model : View Model for Supabase Authentication #47
Conversation
Added class `com.android.periodpals.model.AuthViewModel`. It takes an AuthModel (to be implemented) and has a single function for now: `signUpWithEmail`. The View can call this function to register a new user using an email and password.
Added a data class `com.android.periodpals.model.user.User`. For now, this class has attributes `uid`, `userName` and `displayName` but can be augmented later.
Code was used to test the connection to the Supabase database by having a screen display a `countries` list present on Supabase. Deleted the countries list in Supabase as well.
This `SupabaseClient` object can be called from anywhere to access the Supabase client configured with the correct url and anon key. For now, it install only the `Auth` plugin.
…d's shared preferences The file used by `SharedPreferenceHelper` to store and fetch the preferences is called `period_pals_prefs`. It saves preferences in a (key, value) structure.
Added a `UserState` class that defines a user state: either `Loading`, `Success` or `Error`. Added an observable user state `userState` to the class. Improved `signUpWithEmail` function to take a context as argument (used to save the access token in Android's shared preferences). Added `onSuccess` and `onFailure` in the call to the Model's `login` method.
Added public functions to manage the authentication in the View Model: + `logInWithEmail` to log a user that already has a registered + `logOut` to log a user out + `isUserLoggedIn` to check whether or not the user is logged in
Renamed class `UserState` to `UserAuthState`, as well as `userState` variables to `userAuthState` for consistency.
…hViewModel` Set the `_userAuthState` to `Loading` at the beginning of the `signUpWithEmail`, `logInWithEmail` and `logOut` functions in `AuthViewModel` so that the state behaves as expected.
Created a `clearPreferences` function in `SharedPreferencesHelper` and called it in the `onSuccess` lambda of the `logOut` function of the `AuthViewModel`. The signature of the `logOut` function changed and now takes a `Context` as parameter.
…hentication/view-model # Conflicts: # app/build.gradle.kts # app/src/main/AndroidManifest.xml # app/src/main/java/com/android/periodpals/MainActivity.kt # gradle/libs.versions.toml # gradle/wrapper/gradle-wrapper.properties
…hentication/view-model # Conflicts: # app/src/main/java/com/android/periodpals/MainActivity.kt # gradle/libs.versions.toml
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.
LGTM.
Requires minor changes for PR approval, c.f. code / file comments.
app/src/main/java/com/android/periodpals/model/auth/AuthViewModel.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/android/periodpals/utils/SharedPreferenceHelper.kt
Outdated
Show resolved
Hide resolved
…omments to MainCoroutineRule.kt and remove commented out code from AuthViewModel.kt
…iew-model # Conflicts: # app/build.gradle.kts # app/src/main/java/com/android/periodpals/MainActivity.kt
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.
LGTM.
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.
LGTM. Thanks !
View Model for Supabase Authentication
Description
This PR introduces the View Model for the Supabase Authentication. It adds multiple new utilities classes. For now it only accepts user account creation through email. Add Also a rule for the
MainCoroutine
to be able to run test on functions that have theviewModelScope
. It closes issue #11.Changes
AuthModelSupabase
functionisLoggedIn
refactored for readibility.MainCoroutineRule.kt
created to test functions that haveviewModelScope
.Files
Added
AuthViewModel.kt
inapp.src.main,java,com.android.periodpals.model.auth
AuthViewModelTest.kt
inapp.src.test,java,com.android.periodpals.model.auth
MainCoroutineRule.kt
inapp.src.test,java,com.android.periodpals
SupabaseClient.kt
inapp.src.test,java,com.android.periodpals.model
SharedPreferenceHelper.kt
inapp/src/main/java/com/android/periodpals/utils
User.kt
inapp/src/main/java/com/android/periodpals/model/user
UserAuthState.kt
inapp/src/main/java/com/android/periodpals/model/user
Modified
app/build.gradle.kts
AuthModelSupabase.kt
inapp.src.main,java,com.android.periodpals.model.auth
AuthModelSupabaseTest.kt
inapp.src.test,java,com.android.periodpals.model.auth
Dependencies Added
org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1
forMainCoroutineRule.kt
Testing
AuthViewModel.kt
. It tests wether it correctly handled successful and erroneous calls to the authentication model.AuthModel.kt
to adapt correctly to the changes to the class.