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

Create 13 #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions 13_auth/13
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
1


class LoginViewModel : ViewModel() {
private val repository = UserRepository()

fun authenticateUser(login: String, password: String) {
repository.authenticateUser(login, password)
}
}


class UserRepository {
private val retrofit = Retrofit.Builder()
.baseUrl("http://localhost:9999/")
.addConverterFactory(GsonConverterFactory.create())
.build()

private val service = retrofit.create(UserService::class.java)

fun authenticateUser(login: String, password: String) {
val call = service.authenticateUser(login, password)
call.enqueue(object : Callback<User> {
override fun onResponse(call: Call<User>, response: Response<User>) {
if (response.isSuccessful) {
val user = response.body()
AppAuth.saveUser(user)
}
}

override fun onFailure(call: Call<User>, t: Throwable) {
// Обработка ошибки
}
})
}
}


interface UserService {
@FormUrlEncoded
@POST("api/users/authentication")
fun authenticateUser(
@Field("login") login: String,
@Field("pass") password: String
): Call<User>
}


data class User(
val id: Int,
val token: String
)


object AppAuth {
private var user: User? = null

fun saveUser(user: User?) {
this.user = user
}

fun getUser(): User? {
return user
}
}