Skip to content

Commit a26bbfd

Browse files
committed
refactor(web): update root layout for auth/login routes
Update root layout logic: - Refactor authentication + token restore on navigation. - Use webRoutes for logic around the home page for login state check, not just token.
1 parent 09f22e7 commit a26bbfd

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

apps/web/src/routes/+layout.svelte

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import { provide } from '@gitbutler/shared/context';
1616
import { WebRoutesService, WEB_ROUTES_SERVICE } from '@gitbutler/shared/routing/webRoutes.svelte';
1717
import { type Snippet } from 'svelte';
18-
import { get } from 'svelte/store';
1918
import '$lib/styles/global.css';
2019
2120
interface Props {
@@ -30,17 +29,17 @@
3029
const authService = new AuthService();
3130
provide(AUTH_SERVICE, authService);
3231
33-
let token = $state<string | null>();
32+
const persistedToken = authService.token;
3433
3534
$effect(() => {
36-
token = get(authService.tokenReadable) || page.url.searchParams.get('gb_access_token');
37-
if (token) {
38-
authService.setToken(token);
39-
40-
if (page.url.searchParams.has('gb_access_token')) {
41-
page.url.searchParams.delete('gb_access_token');
42-
goto(`?${page.url.searchParams.toString()}`);
35+
if (page.url.searchParams.has('gb_access_token')) {
36+
const token = page.url.searchParams.get('gb_access_token');
37+
if (token && token !== persistedToken.current) {
38+
authService.setToken(token);
4339
}
40+
41+
page.url.searchParams.delete('gb_access_token');
42+
goto(`?${page.url.searchParams.toString()}`);
4443
}
4544
});
4645
@@ -49,7 +48,7 @@
4948
window.location.href = jsonLinks.legal.privacyPolicy.url;
5049
}
5150
52-
if (!token && page.route.id === '/(app)/home') {
51+
if (!persistedToken.current && page.route.id === '/(app)/home') {
5352
goto('/');
5453
}
5554
});
@@ -69,7 +68,7 @@
6968
{/if}
7069
</svelte:head>
7170

72-
{#if (page.route.id === '/(app)' && !token) || page.route.id === '/(app)/home'}
71+
{#if (page.route.id === '/(app)' && !persistedToken.current) || page.route.id === '/(app)/home'}
7372
<section class="marketing-page">
7473
<Header />
7574
<Hero />

0 commit comments

Comments
 (0)