check cookie
This commit is contained in:
parent
03de896ccb
commit
63d8764d0d
|
@ -44,4 +44,54 @@ export async function login(email, password) {
|
||||||
//cookie.set("check", "check", { expires: 7 })
|
//cookie.set("check", "check", { expires: 7 })
|
||||||
console.log(data)
|
console.log(data)
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const logout = async (event) => {
|
||||||
|
//destroy cookie sessionID
|
||||||
|
const sessionID = event.cookies.get("sessionID");
|
||||||
|
console.log("SessionID: ", sessionID);
|
||||||
|
const apiUrl = import.meta.env.VITE_API_URL;
|
||||||
|
const res = await fetch(apiUrl + '/logout', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getFromAPI = async (event, endpoint, data) => {
|
||||||
|
const sessionID = event.cookies.get("sessionID");
|
||||||
|
const apiUrl = import.meta.env.VITE_API_URL;
|
||||||
|
const res = await fetch(apiUrl + endpoint, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer ' + sessionID,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const postToAPI = async (event, endpoint, data) => {
|
||||||
|
const sessionID = event.cookies.get("sessionID");
|
||||||
|
const apiUrl = import.meta.env.VITE_API_URL;
|
||||||
|
const res = await fetch(apiUrl + endpoint, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer ' + sessionID,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
// Create a writable store with initial value of null
|
||||||
|
export const user = writable(null);
|
||||||
|
|
||||||
|
// Function to set user data
|
||||||
|
export const setUserData = async (sessionID) => {
|
||||||
|
const userID = await getUserID(sessionID); // your function to fetch user ID based on session ID
|
||||||
|
user.set({ sessionID, userID });
|
||||||
|
};
|
||||||
|
|
||||||
|
getUserID = async (sessionID) => {
|
||||||
|
console.log("SessionID: ", sessionID);
|
||||||
|
return 1234;
|
||||||
|
};
|
|
@ -0,0 +1,21 @@
|
||||||
|
console.log("Hello from layout.server.js");
|
||||||
|
|
||||||
|
export const load = async (event) => {
|
||||||
|
const sessionID = event.cookies.get("sessionID");
|
||||||
|
console.log("SessionID: ", sessionID);
|
||||||
|
const apiUrl = import.meta.env.VITE_API_URL;
|
||||||
|
const res = await fetch(apiUrl + '/edible', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer ' + sessionID
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json();
|
||||||
|
const userID = data.userID;
|
||||||
|
console.log("UserID: ", userID); // Here's the userID
|
||||||
|
} else {
|
||||||
|
console.log("Invalid session or other error.");
|
||||||
|
}
|
||||||
|
};
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import Header from '$components/Header.svelte';
|
import Header from '$components/Header.svelte';
|
||||||
|
import Footer from '$components/Footer.svelte';
|
||||||
let text = '';
|
let text = '';
|
||||||
const apiUrl = import.meta.env.VITE_API_URL;
|
const apiUrl = import.meta.env.VITE_API_URL;
|
||||||
|
|
||||||
|
@ -67,6 +68,8 @@
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<Footer/>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
ul li {
|
ul li {
|
||||||
max-width: 300px;
|
max-width: 300px;
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
import { redirect } from "@sveltejs/kit";
|
import { redirect } from "@sveltejs/kit";
|
||||||
import { login } from '$lib/api';
|
|
||||||
|
|
||||||
export const load = async (event) => {
|
export const load = async (event) => {
|
||||||
console.log("hello from backend")
|
console.log("hello from backend")
|
||||||
const sessionID = event.cookies.get('sessionID');
|
//const sessionID = event.cookies.get('sessionID');
|
||||||
|
|
||||||
if (sessionID) {
|
//if (sessionID) {
|
||||||
throw redirect('/profile');
|
// throw redirect('/profile');
|
||||||
}
|
//}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
|
@ -16,7 +15,6 @@ export const actions = {
|
||||||
const email = formData.get('email');
|
const email = formData.get('email');
|
||||||
const password = formData.get('password');
|
const password = formData.get('password');
|
||||||
|
|
||||||
|
|
||||||
const apiUrl = import.meta.env.VITE_API_URL;
|
const apiUrl = import.meta.env.VITE_API_URL;
|
||||||
|
|
||||||
const res = await fetch(apiUrl + '/login', {
|
const res = await fetch(apiUrl + '/login', {
|
||||||
|
@ -32,16 +30,20 @@ export const actions = {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
console.log("ok")
|
const sessionID = res.headers.get('Authorization').substring(7);
|
||||||
return redirect('/profile');
|
event.cookies.set('sessionID', sessionID, {
|
||||||
} else {
|
httpOnly: true,
|
||||||
console.log("not ok")
|
maxAge: 60 * 60 * 24 * 7, // 1 week
|
||||||
const error = await res.json();
|
path: '/',
|
||||||
return {
|
});
|
||||||
status: 400,
|
|
||||||
redirect: '/login',
|
throw redirect(301,'/profile');
|
||||||
body: { error }
|
}
|
||||||
};
|
const error = await res.json();
|
||||||
|
|
||||||
|
if (error){
|
||||||
|
const encodedMessage = encodeURIComponent(error.error);
|
||||||
|
throw redirect(301,'/login?error='+ encodedMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,37 +3,30 @@
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { login } from '$lib/api';
|
|
||||||
import { isValidEmail } from '$lib/utils';
|
|
||||||
import Modal from '$components/Modal.svelte';
|
import Modal from '$components/Modal.svelte';
|
||||||
import Header from '$components/Header.svelte';
|
import Header from '$components/Header.svelte';
|
||||||
import Footer from '$components/Footer.svelte';
|
import Footer from '$components/Footer.svelte';
|
||||||
import { navigate } from 'svelte-navigator';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
|
let errorMessage = '';
|
||||||
let showModal = false;
|
let showModal = false;
|
||||||
let modalMessage = 'Test message';
|
let modalMessage = 'Test message';
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
|
if (urlParams.has('error')) {
|
||||||
|
errorMessage = decodeURIComponent(urlParams.get('error'));
|
||||||
|
showModal = true;
|
||||||
|
modalMessage = errorMessage;
|
||||||
|
} else {
|
||||||
|
showModal = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let email = '';
|
let email = '';
|
||||||
let password = '';
|
let password = '';
|
||||||
|
|
||||||
async function handleLogin() {
|
|
||||||
return;
|
|
||||||
if (!isValidEmail(email)){
|
|
||||||
modalMessage = "Please enter the email address you used to sign up.";
|
|
||||||
showModal = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const user = await login(email, password);
|
|
||||||
navigate('/profile');
|
|
||||||
//reload the page
|
|
||||||
location.reload();
|
|
||||||
} catch (err) {
|
|
||||||
modalMessage = err;
|
|
||||||
showModal = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function closeModal() {
|
function closeModal() {
|
||||||
showModal = false;
|
showModal = false;
|
||||||
}
|
}
|
||||||
|
@ -45,16 +38,9 @@
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
|
|
||||||
<form class="colonel" on:submit={handleLogin}>
|
|
||||||
<input type="email" placeholder="Email" bind:value={email} required>
|
|
||||||
<input type="password" placeholder="P@s5word" bind:value={password} required>
|
|
||||||
|
|
||||||
<button type="submit" class="btn-fly">Log in</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<form class="colonel" method="POST">
|
<form class="colonel" method="POST">
|
||||||
<input type="email" name="email" placeholder="Email" bind:value={email} required>
|
<input class="valid" type="email" name="email" placeholder="Email" bind:value={email} required>
|
||||||
<input type="password" name="password" placeholder="P@s5word" bind:value={password} required>
|
<input class="valid" type="password" name="password" placeholder="P@s5word" bind:value={password} required>
|
||||||
|
|
||||||
<button type="submit" class="btn-fly">Log in</button>
|
<button type="submit" class="btn-fly">Log in</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { redirect } from "@sveltejs/kit";
|
||||||
|
|
||||||
|
export const load = async (event) => {
|
||||||
|
const sessionID = event.cookies.get("sessionID");
|
||||||
|
console.log("SessionID: ", sessionID);
|
||||||
|
const apiUrl = import.meta.env.VITE_API_URL;
|
||||||
|
const res = await fetch(apiUrl + '/edible', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Authorization': 'Bearer ' + sessionID
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
const responseBody = await res.json();
|
||||||
|
console.log(responseBody); // log the entire response body
|
||||||
|
const { userID } = responseBody;
|
||||||
|
console.log(userID);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw redirect(301,'/login?error='+ encodeURIComponent("You must be logged in to view this page."));
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,15 +1,15 @@
|
||||||
<script>
|
<script>
|
||||||
import { onMount } from 'svelte';
|
import Header from '$components/Header.svelte';
|
||||||
//import { getUserData } from '$lib/api'; // This function should send a request to your server to get the user's data.
|
import Footer from '$components/Footer.svelte';
|
||||||
|
|
||||||
//let user;
|
|
||||||
|
|
||||||
//onMount(async () => {
|
|
||||||
// user = await getUserData();
|
|
||||||
//});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h2>Profile</h2>
|
<Header />
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<h2>Profile</h2>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<Footer />
|
||||||
<!-- Path: src/routes/profile/+page.svelte
|
<!-- Path: src/routes/profile/+page.svelte
|
||||||
{#if user}
|
{#if user}
|
||||||
<h1>Welcome, {user.name}!</h1>
|
<h1>Welcome, {user.name}!</h1>
|
||||||
|
|
Loading…
Reference in New Issue