From your explanation and provided code, it seems you want some sort of authentication-aware routing in your Svelte application that displays either Home.svelte or HomeLogin.svelte depending on the user's authentication status. The code snippets you provided contain some logic towards this scenario, however, they do not include the switch between Home.svelte and HomeLogin.svelte explicitly.
Here's some guidance on how to achieve desired behavior:
-
Define HomeLogin component:
Ensure you have a HomeLogin.svelte component created to represent the homepage for logged-in users.
-
Import HomeLogin in _layout.svelte:
This component should be imported similarly to how you import Home.
import { Home, HomeLogin, ... } from '../components';
- Conditionally render the right component:
In your
<Router> within the _layout.svelte file, you may need some logic that switches the component based on whether someone is logged in or not. It seems that you are using a store called userlog to keep track of the login status.
<Router>
{#if $userlog}
<Route path="/" component={HomeLogin} default/>
{:else}
<Route path="/" component={Home} default/>
{/if}
<!-- Other routes -->
</Router>
- Update navigation logic:
You may no longer need the following lines in the
onMount function as setting a default route and using the proper condition should handle this appropriately:
navigate("/Home");
setTimeout(() => {
navigate("/");
}, 100);
-
Update the Nav and auth logic:
Inside nav.svelte and auth.svelte, you should ensure that the correct events (confirmLogin and confirmLogout) are wired correctly to update the userlog store or the respective user store that reflects the login state.
-
Handle user state updates reactively:
Eventually, you may want to react to changes in user state by redirecting to the new default page. You could achieve this by subscribing to the userlog store and using navigate when changes happen.
import { onMount, onDestroy } from 'svelte';
import { userlog } from './../auth/index.js';
import { navigate } from 'svelte-routing';
let unsubscribe;
onMount(() => {
unsubscribe = userlog.subscribe($userlog => {
// When the user logs in or out, navigate to the appropriate home page
navigate($userlog ? '/HomeLogin' : '/Home');
});
});
onDestroy(() => {
if (unsubscribe) {
unsubscribe();
}
});
- Optimize your application's behavior:
Make sure the rest of your application is aware of the user's login state and behaves as expected upon user login or logout.
Keep in mind that this guidance is based on the code you provided and assumptions on traditional Svelte usage. You may need to adjust the logic based on how your application and authentication flow are specifically set up.