Hildebrandt Tobias
11/29/2023, 11:22 PMval authContext = createContext<AuthJWT?>(null)
val authDispatch = createContext<Unit>()
val authReducer: Reducer<AuthJWT?, AuthJWT> = { state, action ->
action
}
object AuthStore {
fun setToken(jwtToken: AuthJWT) {
// ??
}
}
Hildebrandt Tobias
11/30/2023, 10:48 AMval authContext = createContext<AuthJWT?>(null)
val authDispatch = createContext<Dispatch<AuthJWT>>()
val authReducerTask: Reducer<AuthJWT?, AuthJWT> = { state, action ->
action
}
val authReducer = useReducer(authReducerTask, null)
val AuthenticationProvider = FC<Props> {
authReducer.let {(state, dispatch) ->
authContext.Provider(value = state) {
authDispatch.Provider(value = dispatch) {
// children() ?
}
}
}
}
Hildebrandt Tobias
11/30/2023, 12:21 PMval authContext = createContext<String?>(null)
val authDispatch = createContext<Dispatch<String>>()
val authReducerTask: Reducer<String?, String> = { state, action ->
println("Reducer called with Action $action")
action
}
The entrypoint App.kt is essentially just:
val App = FC<Props> {
val authReducer = useReducer(authReducerTask, null)
StrictMode {
authReducer.let {(state, dispatch) ->
authContext.Provider(value = state) {
authDispatch.Provider(value = dispatch) {
CssBaseline()
RouterProvider {
router = appRouter
}
}
}
}
}
}
The Main.kt
val Main = FC<Props> {
val token = useContext(authContext)
println("Main context: ${authContext.hashCode()}")
println("Main token: $token")
}
The Login.kt
val LoginView = FC<Props> {
val token = useContext(authContext)
println("LoginView context: ${authContext.hashCode()}")
println("LoginView Token: $token")
location.href = "${environment.SERVER_URL}/login/callback"
}
And the LoginCallback.kt
val LoginCallback = FC<Props> {
val token = useContext(authContext)
val dispatch = useContext(authDispatch)
println("LoginCallback context: ${authContext.hashCode()}")
println("LoginCallback Token: $token")
println("LoginCallback Dispatch: $dispatch")
dispatch?.let {
println("Dispatching")
it("NEW TOKEN")
}
location.href = environment.SERVER_URL // back to main.
}
So when I visit /login
he does a roundtrip to /login/callback
and then finally sits at /
.
But when I look at the Output then the String is not written and always null.
Console:
LoginView context: -208293842
LoginView Token: null
LoginView context: -208293842
LoginView Token: null
LoginCallback context: -1122324025
LoginCallback Token: null
LoginCallback Dispatch: function () { [native code] }
Dispatching
Reducer called with Action NEW TOKEN
LoginCallback context: -1122324025
LoginCallback Token: NEW TOKEN
LoginCallback Dispatch: function () { [native code] }
Dispatching
LoginCallback context: -1122324025
LoginCallback Token: NEW TOKEN
LoginCallback Dispatch: function () { [native code] }
Dispatching
Reducer called with Action NEW TOKEN
Main context: 1918903185
Main token: null
Main context: 1918903185
Main token: null
Hildebrandt Tobias
11/30/2023, 5:08 PMArtem Kobzar
12/01/2023, 9:18 AMHildebrandt Tobias
12/02/2023, 2:39 PMhref
because on a SO answer there was a snippet on why to always use href
.
Thanks!Hildebrandt Tobias
12/03/2023, 1:42 AMuseLocalStorage
to retrieve relevant data and rebuild the context again?