code: Сделал BasicAuthInterceptor авторизацию (наверное)
This commit is contained in:
parent
1d46891456
commit
d27973528d
@ -0,0 +1,16 @@
|
||||
package ru.myitschool.work.api
|
||||
|
||||
import okhttp3.Credentials
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
|
||||
class BasicAuthInterceptor(private val username: String, private val password: String) :
|
||||
Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val credential = Credentials.basic(username, password)
|
||||
val request = chain.request().newBuilder()
|
||||
.header("Authorization", credential)
|
||||
.build()
|
||||
return chain.proceed(request)
|
||||
}
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
package ru.myitschool.work.api
|
||||
|
||||
import android.content.Context
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import ru.myitschool.work.api.login.ApiServiceLogin
|
||||
import ru.myitschool.work.api.main.ApiServiceMain
|
||||
import ru.myitschool.work.api.scan.ApiServiceScan
|
||||
@ -15,8 +17,8 @@ object NetworkModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRetrofitClient(): RetrofitClient {
|
||||
return RetrofitClient()
|
||||
fun provideRetrofitClient(@ApplicationContext context: Context): RetrofitClient {
|
||||
return RetrofitClient(context)
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
@ -1,7 +1,7 @@
|
||||
package ru.myitschool.work.api
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import android.content.Context
|
||||
import androidx.core.content.ContentProviderCompat.requireContext
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
@ -9,15 +9,21 @@ import ru.myitschool.work.api.login.ApiServiceLogin
|
||||
import ru.myitschool.work.api.main.ApiServiceMain
|
||||
import ru.myitschool.work.api.scan.ApiServiceScan
|
||||
import ru.myitschool.work.core.Constants.SERVER_ADDRESS
|
||||
import ru.myitschool.work.utils.AuthPreferences
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
class RetrofitClient {
|
||||
class RetrofitClient(private val context: Context) {
|
||||
|
||||
private val serverAddress = SERVER_ADDRESS
|
||||
private var authPreferences: AuthPreferences = AuthPreferences(context)
|
||||
|
||||
private val httpClient = OkHttpClient.Builder()
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.addInterceptor(BasicAuthInterceptor(
|
||||
authPreferences.getLogin() ?: "",
|
||||
authPreferences.getPassword() ?: ""
|
||||
))
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
package ru.myitschool.work.core
|
||||
|
||||
object Constants {
|
||||
const val SERVER_ADDRESS = "http://localhost:8090"
|
||||
const val SERVER_ADDRESS = "http://192.168.1.113:8080"
|
||||
}
|
@ -51,6 +51,10 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
|
||||
addTextChangedListener(object : TextWatcher {
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
val login = s.toString()
|
||||
|
||||
authPreferences.saveLogin(binding.username.text.toString())
|
||||
authPreferences.savePassword(binding.password.text.toString())
|
||||
|
||||
binding.login.isEnabled = validateLogin(login)
|
||||
}
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
@ -79,8 +83,7 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
|
||||
binding.error.visibleOrGone(false)
|
||||
|
||||
authPreferences.saveLoginState(true)
|
||||
authPreferences.saveLogin(binding.username.text.toString()) // Сохраняем логин
|
||||
Toast.makeText(context, "Авторизация прошла успешно", Toast.LENGTH_SHORT).show()
|
||||
|
||||
navigateToMainScreen()
|
||||
}
|
||||
is LoginViewModel.LoginState.InvalidCredentials -> {
|
||||
@ -96,6 +99,9 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
|
||||
visibleOrGone(true)
|
||||
text = "Ошибка авторизации"
|
||||
}
|
||||
|
||||
authPreferences.clearLoginState()
|
||||
|
||||
Log.d("Authentication", "Ошибка авторизации")
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,6 @@ class MainViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
// Sealed класс для состояний
|
||||
sealed class MainState {
|
||||
data object Initial : MainState()
|
||||
data object Loading : MainState()
|
||||
|
@ -16,6 +16,7 @@ class AuthPreferences(context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun isLoggedIn(): Boolean {
|
||||
return sharedPreferences.getBoolean(KEY_IS_LOGGED_IN, false)
|
||||
}
|
||||
@ -38,6 +39,17 @@ class AuthPreferences(context: Context) {
|
||||
return sharedPreferences.getString("user_login", null)
|
||||
}
|
||||
|
||||
fun savePassword(login: String) {
|
||||
sharedPreferences.edit().apply {
|
||||
putString("user_password", login)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
fun getPassword(): String? {
|
||||
return sharedPreferences.getString("user_password", null)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PREFS_NAME = "AuthPreferences"
|
||||
private const val KEY_IS_LOGGED_IN = "is_logged_in"
|
||||
|
Loading…
x
Reference in New Issue
Block a user