public inbox for [email protected]
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v1 2/2] chore(profile): refetch user data on profile
Date: Tue, 11 Mar 2025 21:11:29 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

This commit will refetch user data to get the latest user data on
`/settings/profile` page. This method is to prevent users manipulating
any credentials on the localStorage, with refetching new data, it will
be replaced every time users navigating or reloading the
/settings/profile page.

Signed-off-by: Muhammad Rizki <[email protected]>
---
 .../(protected)/settings/profile/+page.ts     | 34 ++++++++++++++-----
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/routes/(protected)/settings/profile/+page.ts b/src/routes/(protected)/settings/profile/+page.ts
index c3f9bd3..65afa6f 100644
--- a/src/routes/(protected)/settings/profile/+page.ts
+++ b/src/routes/(protected)/settings/profile/+page.ts
@@ -3,19 +3,37 @@ import { zod } from "sveltekit-superforms/adapters";
 import { superValidate } from "sveltekit-superforms";
 import { profileSchema } from "$lib/schemas/profile-schema";
 import { useAuth } from "$lib/hooks/auth.svelte";
+import * as typing from "$typings"
+import http from "$lib/hooks/http.svelte";
+import { redirect } from "@sveltejs/kit";
 
 export const load: PageLoad = async () => {
   const auth = useAuth();
-  auth.refresh();
 
-  const data = {
-    username: auth.user?.username,
-    full_name: auth.user?.full_name,
-    ext_email: auth.user?.ext_email,
-    gender: auth.user?.gender,
-    socials: auth.user?.socials
+  const { status, data } = await http<{ user_info: typing.User }>({
+    params: { action: "get_user_info" }
+  });
+
+  if (status !== 200) {
+    localStorage.setItem("gwm_invalid_creds", String(1));
+    auth.clear();
+    return redirect(307, "/");
+  }
+
+  auth.save({
+    token: data.res?.renew_token?.token,
+    token_exp_at: data.res?.renew_token?.token_exp_at,
+    user_info: data.res?.user_info
+  });
+
+  const initialData = {
+    username: data.res?.user_info?.username,
+    full_name: data.res?.user_info?.full_name,
+    ext_email: data.res?.user_info?.ext_email,
+    gender: data.res?.user_info?.gender,
+    socials: data.res?.user_info?.socials
   };
 
-  const form = await superValidate(data, zod(profileSchema));
+  const form = await superValidate(initialData, zod(profileSchema));
   return { form };
 };
-- 
Muhammad Rizki


      parent reply	other threads:[~2025-03-11 14:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-11 14:11 [PATCH v1 0/2] Fix HTTP Muhammad Rizki
2025-03-11 14:11 ` [PATCH v1 1/2] fix(http): only logout on 401 `Unauthorized` response Muhammad Rizki
2025-03-11 14:11 ` Muhammad Rizki [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox