Skip to content

Commit

Permalink
fix auth feature
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Dec 17, 2023
1 parent a3b8a15 commit 775545c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<application
android:allowBackup="true"
android:allowClearUserData="true"
android:enableOnBackInvokedCallback="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
13 changes: 13 additions & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,18 @@ fun BuildConfigExtension.setLocalProperty(dir: Project) {
val key = gradleLocalProperties(dir.projectDir)["KINOPOISK_API_KEY"]
?.toString()
?: throw IllegalStateException("KINOPOISK_API_KEY should be initialised")
val serverHost = gradleLocalProperties(dir.projectDir)["SERVER_HOST"]
?.toString()
?: throw IllegalStateException("SERVER_HOST should be initialised")
val serverApiVersion = gradleLocalProperties(dir.projectDir)["SERVER_API_VERSION"]
?.toString()
?: throw IllegalStateException("SERVER_API_VERSION should be initialised")
val serverPort = gradleLocalProperties(dir.projectDir)["SERVER_PORT"]
?.toString()
?: throw IllegalStateException("SERVER_PORT should be initialised")

buildConfigField("String", "KINOPOISK_API_KEY", key)
buildConfigField("String", "SERVER_HOST", serverHost)
buildConfigField("String", "SERVER_API_VERSION", serverApiVersion)
buildConfigField("String", "SERVER_PORT", serverPort)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stslex.core.network.api.server

import Wizard.core.network.BuildConfig
import com.stslex.core.core.AppDispatcher
import com.stslex.core.network.api.base.NetworkClient
import com.stslex.core.network.api.base.NetworkClientBuilder.setupLogging
Expand All @@ -22,7 +23,6 @@ import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.URLProtocol
import io.ktor.http.contentType
import io.ktor.http.encodedPath
import kotlinx.coroutines.withContext

interface ServerApiClient : NetworkClient
Expand Down Expand Up @@ -52,14 +52,17 @@ class ServerApiClientImpl(
handleResponseExceptionWithRequest(errorParser)
}
defaultRequest {
url(API_HOST) {
host = API_HOST
encodedPath = "api/v1"
protocol = URLProtocol.HTTP
contentType(ContentType.Application.Json)
}
url(
scheme = URLProtocol.HTTP.name,
host = BuildConfig.SERVER_HOST,
port = BuildConfig.SERVER_PORT.toInt(),
path = BuildConfig.SERVER_API_VERSION,
block = {
contentType(ContentType.Application.Json)
}
)
headers {
append(API_KEY_NAME, "API_KEY") // TODO
append(API_KEY_NAME, "API_KEY") // TODO move to buildConfig
}
}
}
Expand Down Expand Up @@ -88,7 +91,6 @@ class ServerApiClientImpl(
}

companion object {
private const val API_KEY_NAME = "API_KEY"
private const val API_HOST = "api_host"
private const val API_KEY_NAME = "x-api-key"
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
package com.stslex.core.network.clients.auth.client

import com.stslex.core.network.api.server.ServerApiClient
import com.stslex.core.network.clients.auth.request.AuthRequest
import com.stslex.core.network.clients.auth.request.LoginRequest
import com.stslex.core.network.clients.auth.request.RegisterRequest
import com.stslex.core.network.clients.auth.response.LoginOkResponse
import com.stslex.core.network.clients.auth.response.RegisterRequest
import com.stslex.core.network.utils.token.AuthController
import com.stslex.core.network.utils.token.toModel
import io.ktor.client.call.body
import io.ktor.client.request.post
import io.ktor.util.InternalAPI
import io.ktor.client.request.setBody

class AuthClientImpl(
private val networkClient: ServerApiClient,
private val tokenController: AuthController
) : AuthClient {

@OptIn(InternalAPI::class)
override suspend fun authUser(
login: String,
password: String
): LoginOkResponse = networkClient.request {
post("$AUTH_URL/login") {
body = AuthRequest(
login = login,
password = password
setBody(
LoginRequest(
login = login,
password = password
)
)
}.body<LoginOkResponse>().also {
tokenController.update(it.toModel())
}
}

@OptIn(InternalAPI::class)
override suspend fun registerUser(
login: String,
username: String,
password: String
): LoginOkResponse = networkClient.request {
post("$AUTH_URL/register") {
body = RegisterRequest(
login = login,
password = password,
username = username
setBody(
RegisterRequest(
login = login,
password = password,
username = username
)
)
}.body<LoginOkResponse>().also {
tokenController.update(it.toModel())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class AuthRequest(
data class LoginRequest(
@SerialName("login")
val login: String,
@SerialName("password")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.stslex.core.network.clients.auth.response
package com.stslex.core.network.clients.auth.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class AuthStore(
authFieldsState = AuthFieldsState.AUTH
)
}
navigate(Navigation.HomeFeature)
// TODO sendEvent(Event.ShowSnackbar.SuccessRegister)
}) {
interactor
Expand All @@ -121,6 +122,7 @@ class AuthStore(
authFieldsState = AuthFieldsState.AUTH
)
}
navigate(Navigation.HomeFeature)
// TODO sendEvent(Event.ShowSnackbar.SuccessRegister)
}) {
interactor
Expand Down

0 comments on commit 775545c

Please sign in to comment.