Medium article released.

Final changes.
Code cleaning.
This commit is contained in:
Burak Fidan
2022-12-28 17:43:15 +03:00
parent bb69ad7cd2
commit 9712a106e9
13 changed files with 18 additions and 26 deletions

View File

@@ -1 +1,3 @@
# Token-Authentication
https://burakdev.medium.com/jwt-authentication-and-refresh-token-in-android-with-retrofit-interceptor-authenticator-da021f7f7534

View File

@@ -12,8 +12,5 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
}
}

View File

@@ -1,6 +1,6 @@
package com.mrntlu.tokenauthentication.models
data class Data(
data class UserInfo(
val _id: String,
val email_address: String
)

View File

@@ -1,6 +1,9 @@
package com.mrntlu.tokenauthentication.models
import com.google.gson.annotations.SerializedName
data class UserInfoResponse(
val `data`: Data,
@SerializedName("data")
val userInfo: UserInfo,
val message: String
)

View File

@@ -10,12 +10,6 @@ import retrofit2.http.Header
import retrofit2.http.POST
interface AuthApiService {
@POST("auth/register")
suspend fun register(
@Body auth: Auth,
)
@POST("auth/login")
suspend fun login(
@Body auth: Auth,

View File

@@ -5,7 +5,6 @@ import retrofit2.Response
import retrofit2.http.GET
interface MainApiService {
@GET("user/info")
suspend fun getUserInfo(): Response<UserInfoResponse>
}

View File

@@ -21,7 +21,6 @@ import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class LoginFragment : Fragment() {
private val viewModel: AuthViewModel by viewModels()
private val tokenViewModel: TokenViewModel by activityViewModels()
@@ -37,13 +36,13 @@ class LoginFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
val loginTV = view.findViewById<TextView>(R.id.loginTV)
tokenViewModel.token.observe(viewLifecycleOwner) { token ->
if (token != null)
navController.navigate(R.id.action_loginFragment_to_main_nav_graph)
}
val loginTV = view.findViewById<TextView>(R.id.loginTV)
viewModel.loginResponse.observe(viewLifecycleOwner) {
when(it) {
is ApiResponse.Failure -> loginTV.text = it.errorMessage

View File

@@ -8,16 +8,10 @@ import android.view.ViewGroup
import com.mrntlu.tokenauthentication.R
class RegisterFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false)
}
}

View File

@@ -20,7 +20,6 @@ import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class MainFragment : Fragment() {
private val viewModel: MainViewModel by viewModels()
private val tokenViewModel: TokenViewModel by activityViewModels()
@@ -48,7 +47,7 @@ class MainFragment : Fragment() {
mainTV.text = when(it) {
is ApiResponse.Failure -> "Code: ${it.code}, ${it.errorMessage}"
ApiResponse.Loading -> "Loading"
is ApiResponse.Success -> "ID: ${it.data.data._id}\nMail: ${it.data.data.email_address}\n\nToken: $token"
is ApiResponse.Success -> "ID: ${it.data.userInfo._id}\nMail: ${it.data.userInfo.email_address}\n\nToken: $token"
}
}

View File

@@ -1,6 +1,5 @@
package com.mrntlu.tokenauthentication.viewmodels
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
@@ -13,12 +12,11 @@ open class BaseViewModel : ViewModel() {
protected fun <T> baseRequest(liveData: MutableLiveData<T>, errorHandler: CoroutinesErrorHandler, request: () -> Flow<T>) {
mJob = viewModelScope.launch(Dispatchers.IO + CoroutineExceptionHandler { _, error ->
Log.d("BaseTest", "baseRequest: $error")
viewModelScope.launch(Dispatchers.Main) {
errorHandler.onError(error.localizedMessage ?: "Error occured! Please try again.")
}
}){
request().cancellable().collect {
request().collect {
withContext(Dispatchers.Main) {
liveData.value = it
}

View File

@@ -12,6 +12,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Fragment"
android:textSize="20sp"
android:textColor="@color/black"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/loginButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

View File

@@ -19,6 +19,10 @@
<TextView
android:id="@+id/infoTV"
android:text="Welcome!"
android:textSize="20sp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?><!--
Sample data extraction rules file; uncomment and customize as necessary.
Sample userInfo extraction rules file; uncomment and customize as necessary.
See https://developer.android.com/about/versions/12/backup-restore#xml-changes
for details.
-->