Skip to content

Commit

Permalink
Login with Google POC
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Nov 26, 2024
1 parent 85422c9 commit 2e015f2
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 2 deletions.
13 changes: 13 additions & 0 deletions composeApp/src/commonMain/kotlin/ui/screen/intro/IntroViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package ui.screen.intro

import IvyConstants
import Platform
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import domain.GoogleAuthenticationUseCase
import ui.ComposeViewModel
import ui.navigation.Navigation
import ui.screen.home.HomeScreen

class IntroViewModel(
private val navigation: Navigation,
private val platform: Platform,
private val googleAuthenticationUseCase: GoogleAuthenticationUseCase,
) : ComposeViewModel<IntroViewState, IntroViewEvent> {
@Composable
override fun viewState(): IntroViewState {
LaunchedEffect(Unit) {
platform.getUrlParam(IvyConstants.SessionTokenParam)
?.let { sessionToken ->
navigation.navigate(HomeScreen())
}
}
return IntroViewState()
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/runServer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi
PORT=8081

# Use lsof to check if the port is in use
PID=$(sudo lsof -ti:$PORT)
PID=$(lsof -ti:$PORT)

# If a PID exists, kill the process
if [ ! -z "$PID" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class GoogleAuthenticationApi : Api {
// TODO: 1. Validate authorization code
// TODO: 2. Created session token
val sessionToken = UUID.randomUUID().toString()
call.respondRedirect("${IvyUrls.frontEnd}?sessionToken=$sessionToken")
val frontEndUrl = IvyUrls.debugFrontEnd
call.respondRedirect("${frontEndUrl}?${IvyConstants.SessionTokenParam}=$sessionToken")
}
}

Expand Down
1 change: 1 addition & 0 deletions shared/src/commonMain/kotlin/IvyConstants.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
object IvyConstants {
val GoogleClientId = "717609528266-tt1p4jhsg8glp4qlj9td5db1racvqlal.apps.googleusercontent.com"
val GoogleAuthCallbackEndpoint = "/auth/google/callback"
val SessionTokenParam = "sessionToken"
}
1 change: 1 addition & 0 deletions shared/src/commonMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface Platform {
fun log(level: LogLevel, msg: String)
fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient
fun playSound(soundUrl: String)
fun getUrlParam(key: String): String?
}

enum class LogLevel {
Expand Down
1 change: 1 addition & 0 deletions shared/src/commonMain/kotlin/ivy/IvyUrls.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ object IvyUrls {
const val tos = "https://github.com/Ivy-Apps/legal/blob/main/ivy-learn-tos.md"
const val privacy = "https://github.com/Ivy-Apps/legal/blob/main/ivy-learn-privacy.md"
const val frontEnd = "https://ivylearn.app"
const val debugFrontEnd = "http://localhost:8080/"
}
4 changes: 4 additions & 0 deletions shared/src/iosMain/kotlin/Platform.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class IOSPlatform : Platform {
override fun playSound(soundUrl: String) {
// TODO: Implement
}

override fun getUrlParam(key: String): String? {
return null
}
}


Expand Down
7 changes: 7 additions & 0 deletions shared/src/jsMain/kotlin/Platform.js.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import io.ktor.client.*
import io.ktor.client.engine.js.*
import kotlinx.browser.window
import org.w3c.dom.Audio
import org.w3c.dom.url.URLSearchParams

class JsPlatform : Platform {
override val name: String = "Web with Kotlin/JS"
Expand All @@ -19,6 +21,11 @@ class JsPlatform : Platform {
val audio = Audio(soundUrl)
audio.play()
}

override fun getUrlParam(key: String): String? {
val urlParams = URLSearchParams(window.location.search)
return urlParams.get(key)
}
}

actual fun platform(): Platform = JsPlatform()
4 changes: 4 additions & 0 deletions shared/src/jvmMain/kotlin/Platform.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class JVMPlatform: Platform {
override fun playSound(soundUrl: String) {
// TODO: Implement
}

override fun getUrlParam(key: String): String? {
return null
}
}

actual fun platform(): Platform = JVMPlatform()

0 comments on commit 2e015f2

Please sign in to comment.