From c578dee315c6f6675c1c347284069fdd289bc01e Mon Sep 17 00:00:00 2001 From: Chulinuwu Date: Sun, 22 Dec 2024 19:31:29 +0700 Subject: [PATCH] feat:getMe/getDecryptedToken func --- src/lib/api/getToken.ts | 15 +++++++++++++++ src/lib/api/getme.ts | 23 +++++++++++++++++++++++ src/routes/api/getme/+server.ts | 24 ++++++++++++++++++++++++ src/routes/auth/login/+page.svelte | 12 ------------ 4 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 src/lib/api/getToken.ts create mode 100644 src/lib/api/getme.ts create mode 100644 src/routes/api/getme/+server.ts diff --git a/src/lib/api/getToken.ts b/src/lib/api/getToken.ts new file mode 100644 index 0000000..175d966 --- /dev/null +++ b/src/lib/api/getToken.ts @@ -0,0 +1,15 @@ +import CryptoJS from 'crypto-js'; + +const secretKey = import.meta.env.VITE_SECRET_KEY; + +export const getDecryptedToken = () => { + const encryptedToken = sessionStorage.getItem('encryptedToken'); + if (encryptedToken) { + const bytes = CryptoJS.AES.decrypt(encryptedToken, secretKey); + const decryptedToken = bytes.toString(CryptoJS.enc.Utf8); + console.log('Decrypted Token:', decryptedToken); + return decryptedToken; + } + console.log('No encrypted token found in sessionStorage.'); + return null; +}; \ No newline at end of file diff --git a/src/lib/api/getme.ts b/src/lib/api/getme.ts new file mode 100644 index 0000000..c6b8738 --- /dev/null +++ b/src/lib/api/getme.ts @@ -0,0 +1,23 @@ +const API_URL = '/api/getme'; + +export async function getMe(token: string) { + try { + const response = await fetch(API_URL, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + } + }); + + if (!response.ok) { + throw new Error('Failed to fetch user data'); + } + + const data = await response.json(); + return data; + } catch (error) { + console.error(error); + throw error; + } +} \ No newline at end of file diff --git a/src/routes/api/getme/+server.ts b/src/routes/api/getme/+server.ts new file mode 100644 index 0000000..e7fb12a --- /dev/null +++ b/src/routes/api/getme/+server.ts @@ -0,0 +1,24 @@ +import type { RequestHandler } from '@sveltejs/kit'; + +export const GET: RequestHandler = async ({ request }) => { + const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/api/v1/auth/me`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': request.headers.get('Authorization') || '' + } + }); + + if (!response.ok) { + return new Response(JSON.stringify({ error: 'Failed to fetch user data' }), { + status: response.status, + headers: { 'Content-Type': 'application/json' } + }); + } + + const data = await response.json(); + return new Response(JSON.stringify(data), { + status: 200, + headers: { 'Content-Type': 'application/json' } + }); +}; \ No newline at end of file diff --git a/src/routes/auth/login/+page.svelte b/src/routes/auth/login/+page.svelte index cd84082..ad67ac2 100644 --- a/src/routes/auth/login/+page.svelte +++ b/src/routes/auth/login/+page.svelte @@ -20,20 +20,8 @@ } catch (error) { errorMessage = error instanceof Error ? error.message : 'An unknown error occurred'; } - getDecryptedToken(); }; - const getDecryptedToken = () => { - const encryptedToken = sessionStorage.getItem('encryptedToken'); - if (encryptedToken) { - const bytes = CryptoJS.AES.decrypt(encryptedToken, secretKey); - const decryptedToken = bytes.toString(CryptoJS.enc.Utf8); - console.log('Decrypted Token:', decryptedToken); - return decryptedToken; - } - console.log('No encrypted token found in sessionStorage.'); - return null; - };