Skip to content

Commit

Permalink
fix:lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Chulinuwu committed Dec 22, 2024
1 parent d16fe1b commit e58b87d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 67 deletions.
18 changes: 9 additions & 9 deletions src/lib/api/getToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ 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);
return decryptedToken;
}
console.log('No encrypted token found in sessionStorage.');
return null;
};
const encryptedToken = sessionStorage.getItem('encryptedToken');
if (encryptedToken) {
const bytes = CryptoJS.AES.decrypt(encryptedToken, secretKey);
const decryptedToken = bytes.toString(CryptoJS.enc.Utf8);
return decryptedToken;
}
console.log('No encrypted token found in sessionStorage.');
return null;
};
36 changes: 18 additions & 18 deletions src/lib/api/getme.ts
Original file line number Diff line number Diff line change
@@ -1,23 +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}`
}
});
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');
}
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;
}
}
const data = await response.json();
return data;
} catch (error) {
console.error(error);
throw error;
}
}
2 changes: 1 addition & 1 deletion src/lib/components/Sidebar/Sidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</div>
<div class="body-small font-semibold">
<div class="text-xs">{user.name}</div>
<div class="text-xs">{user.lastname}</div>
<div class="text-xs">{user.lastname}</div>
</div>
<div class="text-xs font-semibold">
{user.role}
Expand Down
38 changes: 19 additions & 19 deletions src/routes/admin/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import Sidebar from '$lib/components/Sidebar/Sidebar.svelte';
import { getMe } from '$lib/api/getme';
import { getDecryptedToken } from '$lib/api/getToken';
import { getDecryptedToken } from '$lib/api/getToken';
import { onMount } from 'svelte';
const menuItems = [
Expand All @@ -15,27 +15,27 @@
name: 'Not found',
lastname: 'Not found',
role: 'Not found',
id: 'Not found',
id: 'Not found'
};
onMount(async () => {
const token = getDecryptedToken();
if (token) {
try {
const userData = await getMe(token);
user = {
name: userData.result.first_name,
lastname: userData.result.last_name,
role: userData.result.role,
id: userData.result.id,
};
} catch (error) {
console.error('Failed to fetch user data:', error);
}
} else {
console.log('No token found');
}
});
const token = getDecryptedToken();
if (token) {
try {
const userData = await getMe(token);
user = {
name: userData.result.first_name,
lastname: userData.result.last_name,
role: userData.result.role,
id: userData.result.id
};
} catch (error) {
console.error('Failed to fetch user data:', error);
}
} else {
console.log('No token found');
}
});
</script>

<div class="flex min-h-screen bg-white">
Expand Down
38 changes: 19 additions & 19 deletions src/routes/api/getme/+server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +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') || ''
}
});
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' }
});
}
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' }
});
};
const data = await response.json();
return new Response(JSON.stringify(data), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
};
1 change: 0 additions & 1 deletion src/routes/auth/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
errorMessage = error instanceof Error ? error.message : 'An unknown error occurred';
}
};
</script>

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

0 comments on commit e58b87d

Please sign in to comment.