Skip to content

Commit

Permalink
feat:getMe/getDecryptedToken func
Browse files Browse the repository at this point in the history
  • Loading branch information
Chulinuwu committed Dec 22, 2024
1 parent a7d0704 commit c578dee
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
15 changes: 15 additions & 0 deletions src/lib/api/getToken.ts
Original file line number Diff line number Diff line change
@@ -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;
};
23 changes: 23 additions & 0 deletions src/lib/api/getme.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions src/routes/api/getme/+server.ts
Original file line number Diff line number Diff line change
@@ -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' }
});
};
12 changes: 0 additions & 12 deletions src/routes/auth/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
</script>

<div class="min-h-screen flex items-center justify-center bg-sucu-pink-04">
Expand Down

0 comments on commit c578dee

Please sign in to comment.