Skip to content

Commit 6e9649a

Browse files
rushkiiammarfaizi2
authored andcommitted
fix(auth): fix auth guard when credentials is invalid
This commit fixes auth guard to prevent invalid credentials to call API, it will redirect user to the login page if credentials is invalid. Signed-off-by: Muhammad Rizki <[email protected]> Link: https://lore.gnuweeb.org/gwml/[email protected] Signed-off-by: Ammar Faizi <[email protected]>
1 parent b21826c commit 6e9649a

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

src/lib/hooks/auth.svelte.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ let data = $state<LoginResponse>({
55
token_exp_at: 0
66
});
77

8+
const getUserFromLocalStorage = () => {
9+
const user = localStorage.getItem("gwm_uinfo");
10+
if (!user) return undefined;
11+
12+
try {
13+
return JSON.parse(user) as User;
14+
} catch {
15+
return undefined;
16+
}
17+
};
18+
819
export function useAuth() {
920
return {
1021
get token() {
@@ -24,8 +35,12 @@ export function useAuth() {
2435
},
2536

2637
refresh() {
27-
const user = localStorage.getItem("gwm_uinfo");
28-
data.user_info = JSON.parse(user!) as User;
38+
const token = localStorage.getItem("gwm_token");
39+
const token_exp_at = Number(localStorage.getItem("gwm_token_exp_at"));
40+
41+
data.user_info = getUserFromLocalStorage();
42+
data.token = token!;
43+
data.token_exp_at = token_exp_at;
2944
},
3045

3146
save({ user_info, token, token_exp_at }: LoginResponse) {
@@ -42,15 +57,16 @@ export function useAuth() {
4257
},
4358

4459
isValid() {
60+
const user = getUserFromLocalStorage();
4561
const token = localStorage.getItem("gwm_token");
46-
const user = localStorage.getItem("gwm_uinfo");
62+
const expLs = localStorage.getItem("gwm_token_exp_at");
4763

48-
if (!token || !user) {
64+
if (!token || !user || !expLs) {
4965
this.clear();
5066
return false;
5167
}
5268

53-
const exp = Number(localStorage.getItem("gwm_token_exp_at"));
69+
const exp = Number(expLs);
5470
const unix = Math.round(new Date().getTime() / 1000);
5571

5672
if (unix >= exp) {

src/lib/hooks/http.svelte.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { goto } from "$app/navigation";
12
import { PUBLIC_BASE_URL } from "$env/static/public";
23
import * as typing from "$typings";
34
import axios from "axios";
@@ -53,10 +54,11 @@ client.interceptors.response.use(
5354
const response = err.response as AxiosResponse<typing.ResponseAPI<typing.RenewTokenResponse>>;
5455
const status = response ? response.status : null;
5556

56-
if (status === 403 && response?.data) {
57+
if (status !== 200) {
5758
localStorage.removeItem("gwm_token");
5859
localStorage.removeItem("gwm_token_exp_at");
5960
localStorage.removeItem("gwm_uinfo");
61+
goto("/");
6062
}
6163

6264
return response;

src/routes/(protected)/+layout.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
import AppSidebar from "$components/customs/app-sidebar.svelte";
44
import Header from "$components/customs/header.svelte";
55
import Separator from "$components/ui/separator/separator.svelte";
6+
import { useAuth } from "$lib/hooks/auth.svelte";
7+
import { goto, onNavigate } from "$app/navigation";
68
79
let { children } = $props();
10+
11+
const auth = useAuth();
12+
13+
onNavigate(() => {
14+
if (auth.isValid()) return;
15+
goto("/");
16+
});
817
</script>
918

1019
<Sidebar.Provider class="light">

src/routes/(protected)/+layout.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ export const load: LayoutLoad = async () => {
99

1010
if (!auth.isValid()) {
1111
localStorage.setItem("gwm_invalid_creds", String(1));
12+
auth.clear();
1213
return redirect(307, "/");
1314
}
1415

15-
const { data } = await http<{ user_info: typing.User }>({
16+
const { status, data } = await http<{ user_info: typing.User }>({
1617
params: { action: "get_user_info" }
1718
});
1819

20+
if (status !== 200) {
21+
localStorage.setItem("gwm_invalid_creds", String(1));
22+
auth.clear();
23+
return redirect(307, "/");
24+
}
25+
1926
auth.save({
2027
token: data.res?.renew_token?.token,
2128
token_exp_at: data.res?.renew_token?.token_exp_at,

src/routes/+page.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const load: PageLoad = async () => {
1111

1212
if (auth.isValid()) return redirect(307, "/home");
1313

14+
auth.refresh();
15+
1416
const form = await superValidate(zod(loginSchema));
1517
return { form, isInvalidCreds };
1618
};

0 commit comments

Comments
 (0)