From 4b2fb40d6f37c7b000dd084ba8a4f8aedcaf7b9d Mon Sep 17 00:00:00 2001 From: qowevisa Date: Wed, 6 Nov 2024 19:42:42 +0200 Subject: [PATCH] Everything I have --- package-lock.json | 49 +++++++++++++++++++++++++--- package.json | 2 +- src/hooks.server.ts | 24 ++++++++++++++ src/lib/api.ts | 4 ++- src/routes/+layout.svelte | 17 ++++++++++ src/routes/+page.svelte | 7 ++-- src/routes/api/auth/login/+server.js | 38 +++++++++++++++++++++ src/routes/login/+page.svelte | 15 ++++++--- src/routes/test/+page.svelte | 47 ++++++++++++++++++++++++++ src/routes/test2/+page.server.js | 9 +++++ src/routes/test2/+page.svelte | 46 ++++++++++++++++++++++++++ vite.config.ts | 2 +- 12 files changed, 246 insertions(+), 14 deletions(-) create mode 100644 src/hooks.server.ts create mode 100644 src/routes/api/auth/login/+server.js create mode 100644 src/routes/test/+page.svelte create mode 100644 src/routes/test2/+page.server.js create mode 100644 src/routes/test2/+page.svelte diff --git a/package-lock.json b/package-lock.json index ff034c0..5c3f277 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1017,6 +1017,19 @@ "node": ">= 8" } }, + "node_modules/anymatch/node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/arg": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.2.tgz", @@ -1817,6 +1830,19 @@ "node": ">=8.6" } }, + "node_modules/micromatch/node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/mini-svg-data-uri": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/mini-svg-data-uri/-/mini-svg-data-uri-1.4.4.tgz", @@ -2007,13 +2033,15 @@ "license": "ISC" }, "node_modules/picomatch": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", - "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.2.tgz", + "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", "dev": true, "license": "MIT", + "optional": true, + "peer": true, "engines": { - "node": ">=8.6" + "node": ">=12" }, "funding": { "url": "https://github.com/sponsors/jonschlinkert" @@ -2698,6 +2726,19 @@ "node": ">= 6" } }, + "node_modules/tailwindcss/node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/tailwindcss/node_modules/postcss-selector-parser": { "version": "6.1.2", "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-6.1.2.tgz", diff --git a/package.json b/package.json index 58feaeb..5376a82 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "test", + "name": "fin-check-front", "version": "0.0.1", "type": "module", "scripts": { diff --git a/src/hooks.server.ts b/src/hooks.server.ts new file mode 100644 index 0000000..58e0e52 --- /dev/null +++ b/src/hooks.server.ts @@ -0,0 +1,24 @@ +// import type { Handle, HandleFetch } from "@sveltejs/kit"; +import type { HandleFetch } from "@sveltejs/kit"; + +// export const handle: Handle = async ({ event, resolve }) => { +// console.log("handle Function") +// return await resolve(event) +// } + +console.log("in hooks file: 1") +export const handleFetch: HandleFetch = async ({ request, fetch }) => { + console.log(request) + console.log("handleFetch triggered for URL:", request.url); + console.log("in handleFetch: 2") + if (request.url.startsWith("https://api.fin.qowevisa.click/api")) { + request = new Request( + request.url.replace("https://api.fin.qowevisa.click/api", "http://localhost:3000/api"), + request + ) + }; + console.log(request) + console.log("in handleFetch: 3") + + return fetch(request); +}; diff --git a/src/lib/api.ts b/src/lib/api.ts index 6910161..5a8a4c3 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,7 +1,7 @@ // src/lib/api.ts // Define the base URL of your API -const BASE_API_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3111/api'; +const BASE_API_URL = import.meta.env.VITE_API_BASE_URL_SERVER || 'http://localhost:3111/api'; // Interface for the login response interface LoginResponse { @@ -20,6 +20,7 @@ interface UserData { // Generic function for making API requests export async function apiFetch(endpoint: string, options: RequestInit = {}): Promise { const url = `${BASE_API_URL}${endpoint}`; + console.log("Fetching ", url) const defaultHeaders = { 'Content-Type': 'application/json', @@ -57,6 +58,7 @@ export async function login(username: string, password: string): Promise { return apiFetch('/user/me', { + credentials: 'include', headers: { Authorization: `Bearer ${token}` }, }); } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 9b776b7..415e6f4 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,6 +1,23 @@ +{#if $page.url.pathname != "/login"} + +{/if} + {@render children()} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c4ed993..857c29c 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,5 +1,6 @@ -

Welcome to SvelteKit1

- Visit svelte.dev/docs/kit to read the - documentation123213 + ping + test + test2 + login

diff --git a/src/routes/api/auth/login/+server.js b/src/routes/api/auth/login/+server.js new file mode 100644 index 0000000..f01bbdc --- /dev/null +++ b/src/routes/api/auth/login/+server.js @@ -0,0 +1,38 @@ +// src/routes/api/auth/login/+server.ts +import { json } from '@sveltejs/kit'; +import { login } from '$lib/api'; + +export async function POST({ request, cookies }) { + console.log("in POST1") + const authToken = request.headers.get('App-Token'); + console.log("authToken = ", authToken) + console.log("it should be = ", import.meta.env.VITE_AUTH_APP_TOKEN) + if (authToken !== import.meta.env.VITE_AUTH_APP_TOKEN) { + return json({ error: 'Forbidden' }, { status: 403 }); + } + console.log("in POST2") + const { username, password } = await request.json(); + + console.log("in POST3") + try { + console.log("in POST4") + const loginResponse = await login(username, password); // Call the backend login + console.log("loginResponse = ", loginResponse) + + const session = loginResponse.headers.get("Set-Cookie"); + + // Set cookie securely on the server + cookies.set('session', loginResponse.token, { + httpOnly: true, // Prevents JavaScript access + secure: process.env.NODE_ENV === 'production', // Only send over HTTPS in production + sameSite: 'Strict', // Ensures the cookie is only sent in a first-party context + maxAge: 3600 // Expiry time in seconds (e.g., 1 hour) + }); + + console.log("in POST5") + return json({ id: loginResponse.id, name: loginResponse.name }); // Return necessary data + } catch (error) { + return json({ error: error.message }, { status: 401 }); + } +} + diff --git a/src/routes/login/+page.svelte b/src/routes/login/+page.svelte index 7257162..a556799 100644 --- a/src/routes/login/+page.svelte +++ b/src/routes/login/+page.svelte @@ -1,5 +1,4 @@ diff --git a/src/routes/test/+page.svelte b/src/routes/test/+page.svelte new file mode 100644 index 0000000..68a94d3 --- /dev/null +++ b/src/routes/test/+page.svelte @@ -0,0 +1,47 @@ + + + +
+ + + +
diff --git a/src/routes/test2/+page.server.js b/src/routes/test2/+page.server.js new file mode 100644 index 0000000..f368e5d --- /dev/null +++ b/src/routes/test2/+page.server.js @@ -0,0 +1,9 @@ +export async function load({ fetch }) { + const response = await fetch("https://api.fin.qowevisa.click/api/authping", { + credentials: "include", + }); + + return { + message: await response.json() + }; +} diff --git a/src/routes/test2/+page.svelte b/src/routes/test2/+page.svelte new file mode 100644 index 0000000..a0aa04d --- /dev/null +++ b/src/routes/test2/+page.svelte @@ -0,0 +1,46 @@ + + + +

{data.message.message}

+
+ + + +
diff --git a/vite.config.ts b/vite.config.ts index bbf8c7d..2e920e4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,5 +2,5 @@ import { sveltekit } from '@sveltejs/kit/vite'; import { defineConfig } from 'vite'; export default defineConfig({ - plugins: [sveltekit()] + plugins: [sveltekit()], });