Skip to content

Commit d0fbd1c

Browse files
committed
fix(web): check for logged in user by USER_SERVICE, not token
Update project reviews and rules pages to check for logged-in user by USER_SERVICE and redirect home if not logged in, rather than by auth token. Also add refreshUser method to userService.ts to refresh user state and handle errors.
1 parent 11b3f66 commit d0fbd1c

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

apps/web/src/lib/user/userService.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ export class UserService {
7373
return user;
7474
}
7575

76+
async refreshUser() {
77+
try {
78+
const user = await this.fetchUser();
79+
this.user.set(user);
80+
this.error.set(undefined);
81+
} catch (error) {
82+
this.error.set(error);
83+
}
84+
}
85+
7686
clearUser() {
7787
this.user.set(undefined);
7888
}

apps/web/src/routes/(app)/[ownerSlug]/[projectSlug]/reviews/+page.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<script lang="ts">
22
import { goto } from '$app/navigation';
3-
import { AUTH_SERVICE } from '$lib/auth/authService.svelte';
43
import BranchIndexCard from '$lib/components/branches/BranchIndexCard.svelte';
54
import DashboardLayout from '$lib/components/dashboard/DashboardLayout.svelte';
65
import Table from '$lib/components/table/Table.svelte';
6+
import { USER_SERVICE } from '$lib/user/userService';
77
import { getBranchReviewsForRepository } from '@gitbutler/shared/branches/branchesPreview.svelte';
88
import { BranchStatus } from '@gitbutler/shared/branches/types';
99
import { inject } from '@gitbutler/shared/context';
@@ -14,12 +14,13 @@
1414
import { Button, Select, SelectItem } from '@gitbutler/ui';
1515
1616
// Get authentication service and check if user is logged in
17-
const authService = inject(AUTH_SERVICE);
1817
const routes = inject(WEB_ROUTES_SERVICE);
18+
const userService = inject(USER_SERVICE);
19+
const user = userService.user;
1920
20-
// If there is no token (user not logged in), redirect to home
21+
// If there is no user (user not logged in), redirect to home
2122
$effect(() => {
22-
if (!authService.token.current) {
23+
if ($user === undefined) {
2324
goto(routes.homePath());
2425
}
2526
});

apps/web/src/routes/(app)/[ownerSlug]/rules/+page.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { goto } from '$app/navigation';
3-
import { AUTH_SERVICE } from '$lib/auth/authService.svelte';
3+
import { USER_SERVICE } from '$lib/user/userService';
44
import { eventTimeStamp } from '@gitbutler/shared/branches/utils';
55
import { inject } from '@gitbutler/shared/context';
66
import Loading from '@gitbutler/shared/network/Loading.svelte';
@@ -11,12 +11,13 @@
1111
import type { Rule } from '@gitbutler/shared/rules/types';
1212
1313
// Get authentication service and check if user is logged in
14-
const authService = inject(AUTH_SERVICE);
1514
const routes = inject(WEB_ROUTES_SERVICE);
15+
const userService = inject(USER_SERVICE);
16+
const user = userService.user;
1617
17-
// If there is no token (user not logged in), redirect to home
18+
// If there is no user (user not logged in), redirect to home
1819
$effect(() => {
19-
if (!authService.token.current) {
20+
if ($user === undefined) {
2021
goto(routes.homePath());
2122
}
2223
});

0 commit comments

Comments
 (0)