Skip to content

Commit b21dd00

Browse files
committed
Desktop issue fix: Attempting login second time was not working
1 parent 012d967 commit b21dd00

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

kmauth-google/kmauth_google.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = 'kmauth_google'
3-
spec.version = '0.0.1-alpha-test'
3+
spec.version = '0.0.3-alpha03'
44
spec.homepage = ''
55
spec.source = { :http=> ''}
66
spec.authors = ''

kmauth-google/src/jvmMain/kotlin/com/sunildhiman90/kmauth/google/GoogleAuthManagerJvm.kt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ import io.ktor.server.response.respondText
2525
import io.ktor.server.routing.get
2626
import io.ktor.server.routing.routing
2727
import kotlinx.coroutines.CoroutineScope
28+
import kotlinx.coroutines.DelicateCoroutinesApi
2829
import kotlinx.coroutines.Dispatchers
30+
import kotlinx.coroutines.GlobalScope
2931
import kotlinx.coroutines.cancel
32+
import kotlinx.coroutines.delay
3033
import kotlinx.coroutines.launch
3134
import kotlinx.coroutines.withContext
3235
import java.awt.Desktop
@@ -36,6 +39,7 @@ import java.net.http.HttpClient
3639
import java.net.http.HttpRequest
3740
import java.net.http.HttpResponse
3841
import java.util.UUID
42+
import kotlin.jvm.java
3943

4044

4145
internal class GoogleAuthManagerJvm : GoogleAuthManager {
@@ -48,7 +52,7 @@ internal class GoogleAuthManagerJvm : GoogleAuthManager {
4852
private val redirectUri = "http://localhost:8080/callback" // Ktor will listen on this URI
4953
private var uniqueUserId: String? = null
5054
private var onSignResult: ((KMAuthUser?, Throwable?) -> Unit)? = null
51-
private val scope = CoroutineScope(Dispatchers.IO)
55+
private var scope = CoroutineScope(Dispatchers.IO)
5256

5357
init {
5458

@@ -79,6 +83,7 @@ internal class GoogleAuthManagerJvm : GoogleAuthManager {
7983
}
8084

8185
// Start the Ktor HTTP server to handle the OAuth redirect response
86+
@OptIn(DelicateCoroutinesApi::class)
8287
private fun startHttpServer(flow: GoogleAuthorizationCodeFlow, port: Int = 8080): EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration> {
8388
Logger.d("Starting HTTP server on port $port")
8489
val server = embeddedServer(Netty, port = port) {
@@ -151,16 +156,30 @@ internal class GoogleAuthManagerJvm : GoogleAuthManager {
151156
)
152157
}
153158

154-
Logger.d("Shutting down server")
155-
server?.stop(1000, 1000)
156-
scope.cancel()
159+
// 🔁 Then shutdown outside of the Ktor pipeline
160+
performShutdownCleanup()
157161

158162
}
159163
}
160164
}.start(wait = false)
161165
return server
162166
}
163167

168+
private fun performShutdownCleanup() {
169+
scope.launch {
170+
try {
171+
delay(500) // wait for response to be sent
172+
Logger.d("Shutting down server")
173+
server?.stop(1000, 1000) // Stop the server gracefully
174+
175+
scope.cancel()
176+
} catch (e: Exception) {
177+
Logger.e("Error shutting down server: ${e.message}")
178+
scope.cancel() // Ensure scope is cancelled even if there's an error
179+
}
180+
}
181+
}
182+
164183
private fun launchGoogleSignIn() {
165184

166185
try {
@@ -182,16 +201,15 @@ internal class GoogleAuthManagerJvm : GoogleAuthManager {
182201
}
183202

184203
// Start the HTTP server in a separate thread, otherwise it will block the ui
204+
// We need to reinitialize the scope, otherwise it will throw exception second time becoz we are cancelling the scope after stopping the server and cancelled scope can not be used to launch coroutine again without recreating new scope
205+
scope = CoroutineScope(Dispatchers.IO)
185206
scope.launch {
186207
server = startHttpServer(flow)
187-
}.invokeOnCompletion {
188-
Logger.d("invokeOnCompletion called")
189-
scope.cancel()
190208
}
191209
} catch (e: Exception) {
192210
e.printStackTrace()
211+
Logger.e("Not able to start the server: ${e.message.toString()}")
193212
onSignResult?.invoke(null, e)
194-
Logger.e(e.message.toString())
195213
}
196214
}
197215

0 commit comments

Comments
 (0)