ItalyPaleAle / svelte-spa-router

Router for SPAs using Svelte 3
MIT License
1.53k stars 105 forks source link

Is this not possible in routeLoaded? #299

Closed Neptunium1129 closed 1 year ago

Neptunium1129 commented 1 year ago

image

If there is no way, I want to find an alternative.

ItalyPaleAle commented 1 year ago

It's very hard for me to help without seeing more context.

What I'd normally recommend is to use route pre-conditions to redirect users to your #login endpoint if needed: https://github.com/ItalyPaleAle/svelte-spa-router/blob/master/Advanced%20Usage.md#route-pre-conditions

Neptunium1129 commented 1 year ago

app.svelte is branched for three conditions: customer, developer, and external developer. However, I thought that it is not suitable for preconditions because it only applies to true or false.

Neptunium1129 commented 1 year ago

image

'/': wrap({
        asyncComponent: () => import("./components/work/main.svelte"),
        userData: {
            hello: 'world',
            foo: 'bar',
            myFunc: () => {
                console.log('login do something!');
            }
        },
        conditions: [
            // First pre-condition function
            async (detail) => {
                let result = false;
                console.log("Pre-condition 1 executed : ", detail);
                //session check
                debugger;
                await api.post("login/loginCheck", "", null).then((res)=>{
                    console.log(res);
                    if(res.data.responseMessage=="FAIL"){
                        result = false;
                    }else{
                        result = true;
                        userSession.set(res.data.data);
                    }
                })
                // Pre-condition succeeds only 50% of times
                return result;
            },
        ]
    }),
ItalyPaleAle commented 1 year ago

You can return false from the condition function, then listen to the “conditionFailed” event to redirect the user (use the replace() method from this router)

Neptunium1129 commented 1 year ago

Thansk!