Hey, we updated the wrappers from pre690 to pre754...
# javascript
h
Hey, we updated the wrappers from pre690 to pre754 and suddenly this loader doesn't do redirects anymore:
Copy code
val MainLoader = LoaderFunction<Any?> { args, handler ->
    val currentPath = window.location.pathname
    val kcRedirect = encodeURIComponent("${environment.FRONTEND_URL}/login/callback?redirect=$currentPath")
    if (currentPath != basePathWith("/login") && currentPath != basePathWith("/login/callback")) {
        val state = AuthStore.getAuthState()
        if (state == AuthState.NONE || state == AuthState.EXPIRED) {
            println("REDIRECT")
            PromiseResult(redirect("$kcLoginUrl$kcRedirect", args))
        }
    }
    PromiseResult(args)
}
The
AuthStore
is a singleton that only looks into the session storage for a token and returns if it's still valid. The console prints the
REDIRECT
but no redirect is happening. When I strip the function down to
PromiseResult(redirect("<https://google.de>", args))
it redirects to google.
Oh I think I found it. At first I tried this but it also didn't work:
Copy code
val MainLoader = LoaderFunction<Any?> { args, handler ->
    val currentPath = window.location.pathname
    val kcRedirect = encodeURIComponent("${environment.FRONTEND_URL}/login/callback?redirect=$currentPath")
    if (currentPath != basePathWith("/login") && currentPath != basePathWith("/login/callback")) {
        when (AuthStore.getAuthState()) {
            AuthState.NONE, AuthState.EXPIRED -> {
                PromiseResult(redirect("$kcLoginUrl$kcRedirect", args))
            }
            else ->
                PromiseResult(args)
        }
    } else
        PromiseResult(args)
}
Even though the code path leads to the
PromiseResult(redirect)
the other
PromisResult
intervened it seems. I only use one
PromiseResult
now and it works again:
Copy code
val MainLoader = LoaderFunction<Any?> { args, handler ->
    val currentPath = window.location.pathname
    val kcRedirect = encodeURIComponent("${environment.FRONTEND_URL}/login/callback?redirect=$currentPath")
    val redirect = when (AuthStore.getAuthState()) {
        AuthState.NONE, AuthState.EXPIRED -> {
            if (currentPath != basePathWith("/login") && currentPath != basePathWith("/login/callback")) {
                redirect("$kcLoginUrl$kcRedirect", args)
            } else null
        }
        else -> null
    }
    PromiseResult(redirect ?: args)
}
a
@turansky ^^
t
@Hildebrandt Tobias Problem solved?
h
Hey, yes. Only having one
PromiseResult
solved my Problem. Still weird behavior though.
t
It's bacause you missed
else
In fact initial function always return
PromiseResult(args)
(last expression) - it's standard Kotlin function behaviour
h
Oh, I see I must have taken the wrong version from history. It was definitely a version that returned the right Promise. Like in the second example.
Maybe I just was unlucky with hot replace though. I'm sometimes a little paranoid that the changes are actually done. Or maybe I was too hasty testing.
t
Copy code
val MainLoader = LoaderFunction<Any?> { args, _ ->
    MainLoaderImpl(args)
}

private fun MainLoaderImpl(args): PromiseResult<Any?> { 
    val currentPath = window.location.pathname
    val kcRedirect = encodeURIComponent("${environment.FRONTEND_URL}/login/callback?redirect=$currentPath")
    if (currentPath != basePathWith("/login") && currentPath != basePathWith("/login/callback")) {
        val state = AuthStore.getAuthState()
        if (state == AuthState.NONE || state == AuthState.EXPIRED) {
            println("REDIRECT")
            return PromiseResult(redirect("$kcLoginUrl$kcRedirect", args))
        }
    }
    
    return PromiseResult(args)
}
With explicit
return
it's much simpler
h
That's definitely true!
t
Is there location in router context?
h
I don't know what you mean exactly I am not yet deep enough into react router.
Oh you mean the kcRedirect?
No it's the external keycloak
t
I'm not sure, that
window.location.pathname
call is unsafe/valid
In
args
you have
request
and
context
- probably you can use it
h
Ah I wasn't aware. Thanks I'll rewrite it.