-
-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OnNav runs before being able to access localstorage states in v10? #992
Comments
Can i see your code? |
What's up with this, local storage access is independent from component lifecycle. There should not be an issue with OnNav. |
Sorry for a long delay, been busy with other stuff // Every time the user navigates to a new page an authcheck is sent to confirm
// that the user session is valid (cookie) and that the user has permission
// to view the requested page. If the check fails the user is logged out.
func (ui *userInfo) OnNav(ctx app.Context) {
// test
var backend_addr string
ctx.GetState("backend_addr", &backend_addr)
log.Println("OnNav addr:", backend_addr)
//app.Window().Call("alert", xxx)
authCheck(ctx.Page().URL().Path, ctx)
}
// Check if a user-session is valid by sending the session cookie to the server
// and checking if a user is allowed to visit the requested page.
func authCheck(authPage string, ctx app.Context) bool {
fmt.Println("authCheck ", authPage)
authPage = strings.Trim(authPage, "/") // trims leading slash
data := &DataStruct{
Requested_page: authPage,
}
var backend_addr string
var client http.Client
ctx.GetState("backend_addr", &backend_addr)
ctx.GetState("cookiejar-client", &client)
fmt.Println("authCheck backend_addr:", backend_addr)
// send request...
resp, err := httpRequestNew(http.MethodPost, backend_addr+"/v1/authcheck", &client, data)
// handle response and logic... until I reload the page: 2024/10/29 10:14:02 OnNav addr:
authCheck /overview
authCheck backend_addr:
httpRequestNew: POST /v1/authcheck
Error occured. Error is: Post "/v1/authcheck": unsupported protocol scheme ""
panic: runtime error: index out of range [1] with length 1
goroutine 48 [running]:
main.authCheck.logRequest.func1()
/home/vs/git/repo/src/login.go:300 +0x409
github.com/maxence-charriere/go-app/v10/pkg/app.(*engineX).async.func1()
/home/vs/git/repo/vendor/github.com/maxence-charriere/go-app/v10/pkg/app/engine.go:359 +0x23
created by github.com/maxence-charriere/go-app/v10/pkg/app.(*engineX).async in goroutine 13
/home/vs/git/repo/vendor/github.com/maxence-charriere/go-app/v10/pkg/app/engine.go:358 +0x7c backend_addr is not loaded since its trying to send a request to just |
It seems like the state is set elsewhere. |
ctx.SetState("backend_addr", fmt.Sprintf("https://%s:%s", backend_addr, backend_port)).PersistWithEncryption() Im running this in |
Just some comments:
|
Could you put a log before the setstate to see where it is called? |
@oderwat I will try that tomorrow. You have a ctx pointer in your struct that you assign in OnMount? Encryption works fine. My webapp works until I refresh it. @maxence-charriere Can you explain this? Put a log in a function to see where it is called? Its called from my login-page's login function? |
We often copy the Using
Sure, but it uses |
I guess this should mean "when" it gets called. I would add logs before the set/get lines to see in what order they are used. |
Might be more optimal than our authcheck that runs all the time. Wanted to be 100% secure noone accesses something they should not be able to.
Do it max 😆
On my login page.
ctx.LocalStorage().Set("backend_addr", full_backend_addr) # trying this way, same problem
fmt.Println("setting local storage oderwat style >:)")
ctx.LocalStorage().Get("backend_addr", &backend_addr)
ctx.GetState("cookiejar-client", &client)
fmt.Println("httpReq localstorage:", backend_addr) Which works fine, I get the address printed.
2024/10/30 16:20:04 Log: Logged in as user: max
authCheck /overview
httpRequest: POST /v1/authcheck
httpReq localstorage: <----------------- EMPTY??? I just dont understand how this happens since it worked in v9. Is the state management too slow? Also a bonus question; are there no docs from changes other than the release notes? Thanks for replies |
Well, this leaves open what the full logging output for the initial and the reload case looks like. Also: Is the data still inside the local storage at that point using dev tools? I wonder why you store the server location at all in local storage? If you hand it over from backend to frontend using We make extensively use of local storage and index db without having problems on reloads. So I guess there is something just not working as you expect it. More debug output may help. We actually have every single call with debug output and implemented a way to download pprof traces. Just because stuff is not always working as we "think" it does. |
Managed to solve it but I dont even understand HOW this solution makes it work. I tried adding sleeps since I thought my backend authcheck (which got empty states) loaded before the states somehow, didnt work. func (lm *leftMenu) OnNav(ctx app.Context) {
endpoint := ctx.Page().URL().Path
// Dont remove this or the real authcheck towards the backend doesnt work
// Noone knows why
if !lm.authCheck(endpoint, ctx) {
app.Log("Autcheck denied:", endpoint)
ctx.Navigate("/login")
}
}
// return true if we are allowed to visit given page
func (lm *leftMenu) authCheck(endpoint string, ctx app.Context) bool {
var allowedPages []string
ctx.GetState("allowed-pages", &allowedPages)
endpoint = strings.Trim(endpoint, "/")
return Contains(allowedPages, endpoint)
} Weird :D |
PS The favicon is broken without any docs about what changed with it. A bug or something Im missing? http.Handle("/", &app.Handler{
Icon: app.Icon{Default: "/web/logo.png"},
...
} |
Hello,
Im migrating a project from v9 to v10 and ran into some problems regarding states.
My app is built with a menu sidebar and a "content" component that updates depending on what the user clicks in the menu.
Im using
OnNav
to run an authentication check towards my backend upon every "content" component change but now in v10 I cant access my states at this time?This same setup worked in v9. Did I miss some bigger architecture change?
The text was updated successfully, but these errors were encountered: