Skip to content
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

Merged
merged 25 commits into from
Oct 17, 2024

Conversation

coaguila
Copy link
Contributor

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 the viewModelScope. It closes issue #11.

Changes

AuthModelSupabase function isLoggedIn refactored for readibility. MainCoroutineRule.kt created to test functions that have viewModelScope.

Files

Added

  • AuthViewModel.kt in app.src.main,java,com.android.periodpals.model.auth
  • AuthViewModelTest.kt in app.src.test,java,com.android.periodpals.model.auth
  • MainCoroutineRule.kt in app.src.test,java,com.android.periodpals
  • SupabaseClient.kt in app.src.test,java,com.android.periodpals.model
  • SharedPreferenceHelper.kt in app/src/main/java/com/android/periodpals/utils
  • User.kt in app/src/main/java/com/android/periodpals/model/user
  • UserAuthState.kt in app/src/main/java/com/android/periodpals/model/user

Modified

  • app/build.gradle.kts
  • AuthModelSupabase.kt in app.src.main,java,com.android.periodpals.model.auth
  • AuthModelSupabaseTest.kt in app.src.test,java,com.android.periodpals.model.auth

Dependencies Added

  • org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1 for MainCoroutineRule.kt

Testing

  • Added testing for AuthViewModel.kt. It tests wether it correctly handled successful and erroneous calls to the authentication model.
  • Modified testing for AuthModel.kt to adapt correctly to the changes to the class.

charliemangano and others added 21 commits October 8, 2024 23:48
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
@coaguila coaguila requested a review from Harrish92 October 17, 2024 19:36
@coaguila coaguila linked an issue Oct 17, 2024 that may be closed by this pull request
@coaguila coaguila requested a review from francelu October 17, 2024 19:41
@coaguila coaguila assigned charliemangano and coaguila and unassigned coaguila Oct 17, 2024
Copy link
Contributor

@francelu francelu left a 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.

…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
Copy link
Contributor

@Harrish92 Harrish92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@francelu francelu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks !

@coaguila coaguila merged commit 732bf97 into main Oct 17, 2024
2 checks passed
@charliemangano charliemangano deleted the feat/authentication/view-model branch October 27, 2024 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Define ViewModel for Supabase authentication
4 participants