fix update token

This commit is contained in:
2026-09-15 12:34:07 +03:00
parent eeaca6c1bd
commit 218659fbd8
5 changed files with 47 additions and 24 deletions
@@ -16,7 +16,7 @@ class DbModule {
fun provideSqlDriver(envFetcher: EnvFetcher): SqlDriver { fun provideSqlDriver(envFetcher: EnvFetcher): SqlDriver {
return PGSimpleDataSource().apply { return PGSimpleDataSource().apply {
val db = envFetcher.get("POSTGRES_DB", "gigawear") val db = envFetcher.get("POSTGRES_DB", "gigawear")
val uri = envFetcher.get("VIDEOBOX_SERVER_URI", "localhost") val uri = envFetcher.get("DB_SERVER_URI", "localhost")
val login = envFetcher.get("POSTGRES_USER", "login") val login = envFetcher.get("POSTGRES_USER", "login")
val pass = envFetcher.get("POSTGRES_PASSWORD", "password") val pass = envFetcher.get("POSTGRES_PASSWORD", "password")
setUrl("jdbc:postgresql://$uri:5432/$db") setUrl("jdbc:postgresql://$uri:5432/$db")
@@ -36,9 +36,6 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
<data
android:scheme="gigawear"
android:host="chat" />
</intent-filter> </intent-filter>
</activity> </activity>
@@ -6,6 +6,7 @@ import io.ktor.client.request.post
import io.ktor.client.request.setBody import io.ktor.client.request.setBody
import org.koin.core.annotation.Factory import org.koin.core.annotation.Factory
import ru.shadowsparky.gigawear.common.AuthRepository import ru.shadowsparky.gigawear.common.AuthRepository
import ru.shadowsparky.gigawear.common.UpdateTokenRequest
import ru.shadowsparky.http.domain.RefreshTokenAction import ru.shadowsparky.http.domain.RefreshTokenAction
import ru.shadowsparky.http.domain.TokenInfo import ru.shadowsparky.http.domain.TokenInfo
@@ -13,7 +14,7 @@ import ru.shadowsparky.http.domain.TokenInfo
class RefreshTokenActionImpl : RefreshTokenAction { class RefreshTokenActionImpl : RefreshTokenAction {
override suspend fun refresh(client: HttpClient, refreshToken: String): TokenInfo { override suspend fun refresh(client: HttpClient, refreshToken: String): TokenInfo {
return client.post("$ENDPOINT/${AuthRepository.UPDATE_TOKEN_PATH}") { return client.post("$ENDPOINT/${AuthRepository.UPDATE_TOKEN_PATH}") {
setBody(refreshToken) setBody(UpdateTokenRequest(refreshToken))
}.body() }.body()
} }
} }
@@ -1,24 +1,59 @@
package ru.shadowsparky.gigawear.presentation package ru.shadowsparky.gigawear.presentation
import com.arkivanov.decompose.ComponentContext import com.arkivanov.decompose.ComponentContext
import com.arkivanov.essenty.lifecycle.coroutines.coroutineScope
import com.arkivanov.essenty.lifecycle.doOnStart
import kotlinx.coroutines.Job
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import org.koin.core.Koin import org.koin.core.Koin
import org.koin.core.annotation.Factory import org.koin.core.annotation.Factory
import ru.shadowsparky.gigawear.domain.HealthCheck
import ru.shadowsparky.gigawear.presentation.nav.Route import ru.shadowsparky.gigawear.presentation.nav.Route
import ru.shadowsparky.http.domain.TokenStorage
import ru.shadowsparky.ui.nav.RootComponent import ru.shadowsparky.ui.nav.RootComponent
class GigaWearRootComponent( class GigaWearRootComponent(
context: ComponentContext context: ComponentContext,
private val tokenStorage: TokenStorage,
private val healthCheck: HealthCheck
) : RootComponent<Route>(context) { ) : RootComponent<Route>(context) {
private val scope = coroutineScope()
override val initStack: List<Route> = listOf(Route.RoutingScreen()) override val initStack: List<Route> = listOf(Route.RoutingScreen())
override val serializer: KSerializer<Route> = Route.serializer() override val serializer: KSerializer<Route> = Route.serializer()
override val koin: Koin = ru.shadowsparky.koin override val koin: Koin = ru.shadowsparky.koin
init {
var job: Job? = null
lifecycle.doOnStart {
job?.cancel()
job = scope.launch {
if (tokenStorage.token.firstOrNull() != null) {
runCatching {
healthCheck.healthCheck()
}.onFailure {
ensureActive()
tokenStorage.clear()
navReplace(Route.AuthScreen())
}
}
}
}
}
} }
@Factory @Factory
class RootComponentFactory { class RootComponentFactory(
private val tokenStorage: TokenStorage,
private val healthCheck: HealthCheck
) {
fun create(context: ComponentContext): GigaWearRootComponent { fun create(context: ComponentContext): GigaWearRootComponent {
return GigaWearRootComponent(context) return GigaWearRootComponent(
context,
tokenStorage,
healthCheck
)
} }
} }
@@ -2,12 +2,10 @@ package ru.shadowsparky.gigawear.presentation.routing
import com.arkivanov.decompose.ComponentContext import com.arkivanov.decompose.ComponentContext
import com.arkivanov.essenty.lifecycle.coroutines.coroutineScope import com.arkivanov.essenty.lifecycle.coroutines.coroutineScope
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.koin.core.annotation.Factory import org.koin.core.annotation.Factory
import org.koin.core.annotation.Named import org.koin.core.annotation.Named
import ru.shadowsparky.gigawear.domain.HealthCheck
import ru.shadowsparky.gigawear.presentation.nav.Child import ru.shadowsparky.gigawear.presentation.nav.Child
import ru.shadowsparky.gigawear.presentation.nav.Route import ru.shadowsparky.gigawear.presentation.nav.Route
import ru.shadowsparky.http.domain.TokenStorage import ru.shadowsparky.http.domain.TokenStorage
@@ -17,25 +15,18 @@ import ru.shadowsparky.ui.nav.RouteNavigator
class RoutingComponent( class RoutingComponent(
private val context: ComponentContext, private val context: ComponentContext,
private val tokenStorage: TokenStorage, private val tokenStorage: TokenStorage,
private val healthCheck: HealthCheck,
private val navigateTo: (Route) -> Unit private val navigateTo: (Route) -> Unit
) : ComponentContext by context { ) : ComponentContext by context {
private val scope = coroutineScope() private val scope = coroutineScope()
init { init {
scope.launch { scope.launch {
if (tokenStorage.token.firstOrNull() != null) { val screen = if (tokenStorage.token.firstOrNull() != null) {
runCatching { Route.ChatScreen()
healthCheck.healthCheck()
navigateTo(Route.ChatScreen())
}.onFailure {
ensureActive()
tokenStorage.clear()
navigateTo(Route.AuthScreen())
}
} else { } else {
navigateTo(Route.AuthScreen()) Route.AuthScreen()
} }
navigateTo(screen)
} }
} }
} }
@@ -44,14 +35,13 @@ class RoutingComponent(
@Named(Route.ROUTING) @Named(Route.ROUTING)
class RoutingComponentFactory( class RoutingComponentFactory(
private val tokenStorage: TokenStorage, private val tokenStorage: TokenStorage,
private val healthCheck: HealthCheck
) : ComponentFactory<Child.RoutingChild, Route.RoutingScreen, Route> { ) : ComponentFactory<Child.RoutingChild, Route.RoutingScreen, Route> {
override fun create( override fun create(
route: Route.RoutingScreen, route: Route.RoutingScreen,
child: ComponentContext, child: ComponentContext,
root: RouteNavigator<Route> root: RouteNavigator<Route>
): Child.RoutingChild { ): Child.RoutingChild {
val comp = RoutingComponent(child, tokenStorage, healthCheck) { val comp = RoutingComponent(child, tokenStorage) {
root.navReplace(it) root.navReplace(it)
} }
return Child.RoutingChild(comp) return Child.RoutingChild(comp)