add create-account call for user sign up

This commit is contained in:
Satindar Dhillon 2022-09-08 21:56:53 -07:00
parent 20f618a1e6
commit 1e93e462b9
3 changed files with 55 additions and 2 deletions

View file

@ -33,6 +33,16 @@ data class EmailLoginCredentials(
val password: String
)
data class CreateAccountParams(
val pendingUserToken: String,
val userProfile: UserProfile
)
data class UserProfile(
val username: String,
val name: String
)
interface EmailLoginSubmit {
@Headers("Content-Type: application/json")
@POST("/api/mobile-auth/email-sign-in")
@ -51,6 +61,12 @@ interface PendingUserSubmit {
suspend fun submitPendingUser(@Body params: SignInParams): Response<PendingUserAuthPayload>
}
interface CreateAccountSubmit {
@Headers("Content-Type: application/json")
@POST("/api/mobile-auth/create-account")
suspend fun submitCreateAccount(@Body params: CreateAccountParams): Response<AuthPayload>
}
object RetrofitHelper {
fun getInstance(): Retrofit {
return Retrofit.Builder().baseUrl(Constants.apiURL)

View file

@ -51,7 +51,7 @@ fun CreateUserProfileView(viewModel: LoginViewModel) {
username = it
viewModel.validateUsername(it)
},
onSubmit = { } // TODO: add viewModel function
onSubmit = { viewModel.submitProfile(username = username, name = name) }
)
// TODO: add a activity indicator (maybe after a delay?)

View file

@ -49,7 +49,7 @@ class LoginViewModel @Inject constructor(
.distinctUntilChanged()
.asLiveData()
val registrationStateLiveData = MutableLiveData(RegistrationState.PendingUser) // TODO: switch back to Social
val registrationStateLiveData = MutableLiveData(RegistrationState.SocialLogin)
fun getAuthCookieString(): String? = runBlocking {
datastoreRepo.getString(DatastoreKeys.omnivoreAuthCookieString)
@ -141,6 +141,43 @@ class LoginViewModel @Inject constructor(
}
}
private fun getPendingAuthToken(): String? = runBlocking {
datastoreRepo.getString(DatastoreKeys.omnivorePendingUserToken)
}
fun submitProfile(username: String, name: String) {
viewModelScope.launch {
val request = RetrofitHelper.getInstance().create(CreateAccountSubmit::class.java)
isLoading = true
errorMessage = null
val pendingUserToken = getPendingAuthToken() ?: ""
val userProfile = UserProfile(name = name, username = username)
val params = CreateAccountParams(
pendingUserToken = pendingUserToken,
userProfile = userProfile
)
val result = request.submitCreateAccount(params)
isLoading = false
if (result.body()?.authToken != null) {
datastoreRepo.putString(DatastoreKeys.omnivoreAuthToken, result.body()?.authToken!!)
} else {
errorMessage = "Something went wrong. Please check your email/password and try again"
}
if (result.body()?.authCookieString != null) {
datastoreRepo.putString(
DatastoreKeys.omnivoreAuthCookieString, result.body()?.authCookieString!!
)
}
}
}
fun handleAppleToken(authToken: String) {
submitAuthProviderPayload(
params = SignInParams(token = authToken, provider = "APPLE")