Skip to content

Commit

Permalink
Authenticate and redirect to new page
Browse files Browse the repository at this point in the history
  • Loading branch information
nijuyonkadesu committed Jul 9, 2023
1 parent 0fd9f4f commit 1840707
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".DeskApplication"
android:allowBackup="true"
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/one/njk/celestidesk/data/auth/AuthApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import retrofit2.http.Body
import retrofit2.http.POST


const val BASE_URL = "https://celestidesk.onrender.com/api/employee"
interface AuthApi {

@POST("signup")
suspend fun signUp(
@Body request: AuthSignUpRequest
): TokenResponse

@POST("signin")
suspend fun signIn(
@POST("login")
suspend fun logIn(
@Body request: AuthLoginRequest
): TokenResponse

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import one.njk.celestidesk.data.auth.model.AuthSignUpRequest

interface AuthRepository {
suspend fun signUp(user: AuthSignUpRequest): AuthResult<Unit>
suspend fun signIn(user: AuthLoginRequest): AuthResult<Unit>
suspend fun logIn(user: AuthLoginRequest): AuthResult<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ class AuthRepositoryImpl(
}
}

override suspend fun signIn(user: AuthLoginRequest): AuthResult<Unit> {
override suspend fun logIn(user: AuthLoginRequest): AuthResult<Unit> {
return try {
val respone = api.signIn(user)
pref.setToken(respone)
val response = api.logIn(user)
pref.setToken(response)
Log.d("network", response.message)
AuthResult.Authorized()
} catch (e: HttpException) {
Log.d("network", e.message.toString())

if(e.code() == 401) AuthResult.UnAuthorized()
else AuthResult.UnknownError()
} catch (e: Exception) {
Log.d("network", e.message.toString())
AuthResult.UnknownError()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ data class AuthSignUpRequest(
val username: String,
val orgHandle: String,
val type: Role,
val passsword: String,
val password: String,
)
15 changes: 12 additions & 3 deletions app/src/main/java/one/njk/celestidesk/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
package one.njk.celestidesk.di

import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import one.njk.celestidesk.data.RolesDataStore
import one.njk.celestidesk.data.auth.AuthApi
import one.njk.celestidesk.data.auth.AuthRepository
import one.njk.celestidesk.data.auth.AuthRepositoryImpl
import one.njk.celestidesk.data.auth.BASE_URL
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.create
import javax.inject.Singleton

const val BASE_URL = "https://celestidesk.onrender.com/api/employee/"

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

@Provides
@Singleton
fun provideAuthApi(): AuthApi {
val moshi = Moshi
.Builder()
.add(KotlinJsonAdapterFactory())
.build()

return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(MoshiConverterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
.create()
}

@Provides
@Singleton
fun provideAuthRepository(api: AuthApi, pref: RolesDataStore): AuthRepositoryImpl{
fun provideAuthRepository(api: AuthApi, pref: RolesDataStore): AuthRepository{
return AuthRepositoryImpl(api, pref)
}

Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/one/njk/celestidesk/ui/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import kotlinx.coroutines.delay
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import one.njk.celestidesk.data.auth.model.AuthResult
import one.njk.celestidesk.databinding.FragmentLoginBinding
import one.njk.celestidesk.viewmodels.AuthViewModel

@AndroidEntryPoint
class LoginFragment: Fragment() {
lateinit var binding: FragmentLoginBinding
val viewModel: AuthViewModel by viewModels()
Expand All @@ -30,12 +33,26 @@ class LoginFragment: Fragment() {
binding.apply {
submit.setOnClickListener {
lifecycleScope.launch {
val inputUsername = username.editText?.text.toString()
val inputPassword = password.editText?.text.toString()
if(inputPassword.isNotEmpty() && inputUsername.isNotEmpty()){
viewModel.logIn(inputUsername, inputPassword)
}
}
}
viewModel.state.observe(viewLifecycleOwner){
if(it.isLoading) {
loading.visibility = View.VISIBLE
submit.visibility = View.GONE
delay(2000)
} else {
loading.visibility = View.INVISIBLE
submit.visibility = View.VISIBLE
findNavController().navigate(LoginFragmentDirections.actionLoginFragmentToRequestFragment())
}
if(it.authResult is AuthResult.Authorized) {
findNavController().navigate(
LoginFragmentDirections.actionLoginFragmentToRequestFragment())
} else {
Toast.makeText(context, "verify your username / password", Toast.LENGTH_SHORT).show()
}
}
submit.setOnEditorActionListener { view, event, _ ->
Expand Down
44 changes: 42 additions & 2 deletions app/src/main/java/one/njk/celestidesk/viewmodels/AuthViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
package one.njk.celestidesk.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.update
import one.njk.celestidesk.data.auth.AuthRepository
import one.njk.celestidesk.data.auth.model.AuthLoginRequest
import one.njk.celestidesk.data.auth.model.AuthResult
import javax.inject.Inject

class AuthViewModel: ViewModel() {
@HiltViewModel
class AuthViewModel @Inject constructor(
private val api: AuthRepository
) : ViewModel() {

}
private val uiState = MutableStateFlow(UiState())

@OptIn(ExperimentalCoroutinesApi::class)
val state: LiveData<UiState> = uiState.flatMapLatest { uiState ->
flowOf(uiState).flowOn(Dispatchers.Default)
}.asLiveData()

suspend fun logIn(username: String, password: String){
uiState.update {
it.copy(isLoading = true)
}
val result = api.logIn(
AuthLoginRequest(username, password)
)
uiState.update {
it.copy(isLoading = false, authResult = result)
}
}
}

data class UiState(
val isLoading: Boolean = false,
val authResult: AuthResult<Unit> = AuthResult.UnknownError(),
)
2 changes: 1 addition & 1 deletion app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<fragment
android:id="@+id/loginFragment"
android:name="one.njk.celestidesk.ui.LoginFragment"
android:label="fragment_login"
android:label="@string/app_name"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_loginFragment_to_requestFragment"
Expand Down

0 comments on commit 1840707

Please sign in to comment.