Skip to content

Commit c349529

Browse files
committed
fix #4 and formatting
1 parent 34cd8e5 commit c349529

File tree

17 files changed

+355
-265
lines changed

17 files changed

+355
-265
lines changed

examples/ioredis/src/app.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ declare global {
77
interface Locals {
88
isUserLoggedIn: boolean;
99
user: {
10-
email: string,
11-
name: string
12-
} | null;
10+
email: string;
11+
name: string;
12+
} | null;
1313
}
1414
// interface PageData {}
1515
// interface Platform {}

examples/ioredis/src/hooks.server.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import { sessionManager } from '$lib/session';
33
import { redirect } from '@sveltejs/kit';
44

55
export const handle: Handle = async ({ event, resolve }) => {
6-
const userSession = await sessionManager.getSession( await event.cookies );
6+
const userSession = await sessionManager.getSession(await event.cookies);
77

88
event.locals = {
99
isUserLoggedIn: false,
1010
user: null
1111
};
1212
if (userSession.error) {
13-
await sessionManager.deleteCookie( await event.cookies );
14-
return resolve( event );
13+
await sessionManager.deleteCookie(await event.cookies);
14+
return resolve(event);
1515
}
1616
if (userSession && userSession.data) {
1717
event.locals = {
1818
isUserLoggedIn: true,
19-
user: userSession?.data
19+
user: userSession?.data
2020
};
2121
}
22-
return resolve( event );
22+
return resolve(event);
2323
};

examples/ioredis/src/lib/session.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { IoRedisSessionStore, type ioRedisSessionOptions } from "@ethercorps/sveltekit-redis-session";
1+
import {
2+
IoRedisSessionStore,
3+
type ioRedisSessionOptions
4+
} from '@ethercorps/sveltekit-redis-session';
25
import { SECRET, REDIS_URL } from '$env/static/private';
3-
import Redis from "ioredis";
6+
import Redis from 'ioredis';
47

58
const sessionOptions: ioRedisSessionOptions = {
69
redisClient: new Redis(REDIS_URL),

examples/ioredis/src/routes/+page.server.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ export const actions: Actions = {
2424
});
2525
}
2626

27-
const {data, error, message } = await sessionManager.createSession( cookies, { email: formData.get( "email" ) }, '1' );
27+
const { data, error, message } = await sessionManager.createSession(
28+
cookies,
29+
{ email: formData.get('email') },
30+
'1'
31+
);
2832
if (error) {
2933
console.log(message);
3034
return fail(400, {
3135
data: Object.fromEntries(formData),
3236
message
3337
});
3438
}
35-
console.log(data)
39+
console.log(data);
3640
return { success: true, message };
3741
}
3842
};

examples/ioredis/src/routes/+page.svelte

+93-62
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import type { ActionData, PageServerData } from './$types';
44
import toast from 'svelte-french-toast';
55
import { onMount } from 'svelte';
6-
import {page} from "$app/stores";
7-
import {invalidateAll} from "$app/navigation";
6+
import { page } from '$app/stores';
7+
import { invalidateAll } from '$app/navigation';
88
let expireTime;
99
1010
export let form: ActionData;
@@ -18,76 +18,107 @@
1818
$: show = data.isAuthenticated;
1919
2020
const logoutUser = async () => {
21-
const response = await fetch( "/api/logout", {
22-
method: "POST",
23-
body: JSON.stringify( { email: data.user.email } ),
21+
const response = await fetch('/api/logout', {
22+
method: 'POST',
23+
body: JSON.stringify({ email: data.user.email }),
2424
headers: {
25-
"content-type": "application/json"
25+
'content-type': 'application/json'
2626
}
27-
} );
27+
});
2828
const responseData = await response.json();
2929
show = responseData.loggedIn;
3030
};
3131
3232
const updateCookieData = async () => {
33-
const response = await fetch( "/api/updateCookieData", {
34-
method: "POST",
35-
body: JSON.stringify( { name: 'The Ether' } ),
33+
const response = await fetch('/api/updateCookieData', {
34+
method: 'POST',
35+
body: JSON.stringify({ name: 'The Ether' }),
3636
headers: {
37-
"content-type": "application/json"
37+
'content-type': 'application/json'
3838
}
39-
} );
39+
});
4040
const responseData = await response.json();
4141
console.log(responseData.sessionData);
4242
if (responseData.success) {
43-
toast.success(`Cookie Data is successfully updated and new session data in redis is ${responseData.sessionData}`)
43+
toast.success(
44+
`Cookie Data is successfully updated and new session data in redis is ${responseData.sessionData}`
45+
);
4446
} else {
45-
toast.error(responseData.message)
47+
toast.error(responseData.message);
4648
}
47-
await invalidateAll()
49+
await invalidateAll();
4850
};
4951
</script>
5052

5153
<div class="h-screen font-sans login bg-cover">
5254
<div class="container mx-auto h-full flex flex-1 justify-center items-center">
5355
<div class="w-full max-w-lg">
5456
{#if show}
55-
5657
<div class="relative mx-auto max-w-md overflow-hidden rounded-lg bg-white shadow">
5758
<div>
58-
<img src="https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80" class="w-full object-cover" alt="" />
59+
<img
60+
src="https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80"
61+
class="w-full object-cover"
62+
alt=""
63+
/>
5964
</div>
60-
<div class="absolute inset-0 z-10 bg-gradient-to-t from-black"></div>
65+
<div class="absolute inset-0 z-10 bg-gradient-to-t from-black" />
6166
<div class="absolute inset-x-0 bottom-0 z-20 p-4">
62-
<p class="mb-1 text-sm text-white text-opacity-80">{#if $page.data.user?.name} {$page.data.user.name} • {/if} <time>Session is valid for only 10 Mins</time></p>
63-
<h3 class="text-xl font-medium text-white">{#if $page.data.user?.email} {$page.data.user.email} {/if}</h3>
64-
<p class="mt-1 text-white text-opacity-80">You are testing for sveltekit redis session manager by ethercorps.</p>
67+
<p class="mb-1 text-sm text-white text-opacity-80">
68+
{#if $page.data.user?.name} {$page.data.user.name} • {/if}
69+
<time>Session is valid for only 10 Mins</time>
70+
</p>
71+
<h3 class="text-xl font-medium text-white">
72+
{#if $page.data.user?.email} {$page.data.user.email} {/if}
73+
</h3>
74+
<p class="mt-1 text-white text-opacity-80">
75+
You are testing for sveltekit redis session manager by ethercorps.
76+
</p>
6577
</div>
6678
</div>
6779
<div class="flex items-center justify-center mt-2">
68-
<div class="inline-flex -space-x-0 divide-x divide-gray-900 overflow-hidden rounded-lg border border-gray-900 shadow-sm">
69-
<button on:click={() => updateCookieData()}
70-
type="button" class="inline-flex items-center bg-amber-500 px-4 py-2.5 text-center text-sm font-medium text-amber-100 shadow-sm hover:bg-amber-100 hover:text-amber-900 transition easy-in-out duration-500">
71-
<svg xmlns="http://www.w3.org/2000/svg" class="mr-2 h-4 w-4" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
72-
<path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" />
80+
<div
81+
class="inline-flex -space-x-0 divide-x divide-gray-900 overflow-hidden rounded-lg border border-gray-900 shadow-sm"
82+
>
83+
<button
84+
on:click={() => updateCookieData()}
85+
type="button"
86+
class="inline-flex items-center bg-amber-500 px-4 py-2.5 text-center text-sm font-medium text-amber-100 shadow-sm hover:bg-amber-100 hover:text-amber-900 transition easy-in-out duration-500"
87+
>
88+
<svg
89+
xmlns="http://www.w3.org/2000/svg"
90+
class="mr-2 h-4 w-4"
91+
fill="none"
92+
viewBox="0 0 24 24"
93+
stroke-width="1.5"
94+
stroke="currentColor"
95+
>
96+
<path
97+
stroke-linecap="round"
98+
stroke-linejoin="round"
99+
d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99"
100+
/>
73101
</svg>
74102

75103
Update Cookie Data
76104
</button>
77-
<button on:click={() => logoutUser()}
78-
type="button" class="inline-flex items-center bg-red-500 px-4 py-2.5 text-center text-sm font-medium text-red-100 shadow-sm hover:bg-red-100 hover:text-red-900 transition easy-in-out duration-500">
105+
<button
106+
on:click={() => logoutUser()}
107+
type="button"
108+
class="inline-flex items-center bg-red-500 px-4 py-2.5 text-center text-sm font-medium text-red-100 shadow-sm hover:bg-red-100 hover:text-red-900 transition easy-in-out duration-500"
109+
>
79110
<svg
80-
xmlns="http://www.w3.org/2000/svg"
81-
fill="none"
82-
viewBox="0 0 24 24"
83-
stroke-width="1.5"
84-
stroke="currentColor"
85-
class="w-6 h-6"
111+
xmlns="http://www.w3.org/2000/svg"
112+
fill="none"
113+
viewBox="0 0 24 24"
114+
stroke-width="1.5"
115+
stroke="currentColor"
116+
class="w-6 h-6"
86117
>
87118
<path
88-
stroke-linecap="round"
89-
stroke-linejoin="round"
90-
d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75"
119+
stroke-linecap="round"
120+
stroke-linejoin="round"
121+
d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75"
91122
/>
92123
</svg>
93124
Logout
@@ -97,12 +128,12 @@
97128
{:else}
98129
<div class="leading-loose">
99130
<form
100-
class="max-w-sm m-4 p-10 bg-white bg-opacity-25 rounded shadow-xl"
101-
method="post"
102-
use:enhance={() => {
131+
class="max-w-sm m-4 p-10 bg-white bg-opacity-25 rounded shadow-xl"
132+
method="post"
133+
use:enhance={() => {
103134
return async ({ result, update }) => {
104-
await invalidateAll()
105-
};
135+
await invalidateAll();
136+
};
106137
}}
107138
>
108139
<p class="text-white font-medium text-center text-lg font-bold">
@@ -111,34 +142,34 @@
111142
<div class="">
112143
<label class="block text-sm text-white" for="email">E-mail</label>
113144
<input
114-
class="w-full px-5 py-1 text-gray-700 bg-gray-300 rounded focus:outline-none focus:bg-white"
115-
type="email"
116-
name="email"
117-
id="email"
118-
119-
placeholder="[email protected]"
120-
aria-label="email"
121-
required
145+
class="w-full px-5 py-1 text-gray-700 bg-gray-300 rounded focus:outline-none focus:bg-white"
146+
type="email"
147+
name="email"
148+
id="email"
149+
150+
placeholder="[email protected]"
151+
aria-label="email"
152+
required
122153
/>
123154
</div>
124155
<div class="mt-2">
125-
<label class="block text-sm text-white">Password</label>
156+
<label class="block text-sm text-white">Password</label>
126157
<input
127-
class="w-full px-5 py-1 text-gray-700 bg-gray-300 rounded focus:outline-none focus:bg-white"
128-
value="Shivam@Meena"
129-
type="password"
130-
name="password"
131-
id="password"
132-
placeholder="Shivam@Meena"
133-
arial-label="password"
134-
required
158+
class="w-full px-5 py-1 text-gray-700 bg-gray-300 rounded focus:outline-none focus:bg-white"
159+
value="Shivam@Meena"
160+
type="password"
161+
name="password"
162+
id="password"
163+
placeholder="Shivam@Meena"
164+
arial-label="password"
165+
required
135166
/>
136167
</div>
137168

138169
<div class="mt-4 items-center flex justify-center">
139170
<button
140-
class="px-4 py-1 text-white font-light tracking-wider bg-gray-900 hover:bg-gray-800 rounded"
141-
type="submit">Login</button
171+
class="px-4 py-1 text-white font-light tracking-wider bg-gray-900 hover:bg-gray-800 rounded"
172+
type="submit">Login</button
142173
>
143174
</div>
144175
</form>
@@ -151,7 +182,7 @@
151182
<style>
152183
.login {
153184
/*background: url(https://tailwindadmin.netlify.app/dist/images/login-new.jpeg);*/
154-
background: url("http://bit.ly/2gPLxZ4");
185+
background: url('http://bit.ly/2gPLxZ4');
155186
background-repeat: no-repeat;
156187
background-size: cover;
157188
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import {fail, json, redirect, type RequestHandler} from "@sveltejs/kit";
2-
import {sessionManager} from "$lib/session";
1+
import { fail, json, redirect, type RequestHandler } from '@sveltejs/kit';
2+
import { sessionManager } from '$lib/session';
33

44
export const POST: RequestHandler = async ({ request, locals, cookies }) => {
5-
if (locals && locals.isUserLoggedIn) {
6-
const { email, password } = await request.json();
7-
if (
8-
email !== "[email protected]" ||
9-
password !== "Shivam@Meena"
10-
) {
11-
return fail( 400, {
12-
email, password ,
13-
message: "Invalid credentials"
14-
} );
15-
}
16-
const { error, message } = await sessionManager.createSession( cookies, {
17-
email
18-
}, '2' );
19-
if (error) {
20-
return fail( 400, {
21-
email, password,
22-
message
23-
} );
24-
}
25-
return json( { success: true, message } );
26-
}
27-
throw redirect(302, '/');
5+
if (locals && locals.isUserLoggedIn) {
6+
const { email, password } = await request.json();
7+
if (email !== '[email protected]' || password !== 'Shivam@Meena') {
8+
return fail(400, {
9+
email,
10+
password,
11+
message: 'Invalid credentials'
12+
});
13+
}
14+
const { error, message } = await sessionManager.createSession(
15+
cookies,
16+
{
17+
email
18+
},
19+
'2'
20+
);
21+
if (error) {
22+
return fail(400, {
23+
email,
24+
password,
25+
message
26+
});
27+
}
28+
return json({ success: true, message });
29+
}
30+
throw redirect(302, '/');
2831
};
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import type {RequestHandler} from "@sveltejs/kit";
2-
import {fail, json, redirect} from "@sveltejs/kit";
3-
import {sessionManager} from "$lib/session";
1+
import type { RequestHandler } from '@sveltejs/kit';
2+
import { fail, json, redirect } from '@sveltejs/kit';
3+
import { sessionManager } from '$lib/session';
44

55
export const POST = (async ({ request, locals, cookies }) => {
6-
if (locals && locals.isUserLoggedIn) {
7-
const additionalData = await request.json();
8-
const newSessionData = { ...locals.user, ...additionalData };
9-
const { data, error, message } = await sessionManager.updateSession( cookies, newSessionData);
10-
if (!error) {
11-
locals.user = newSessionData
12-
}
13-
return json( { success: !error, message, sessionData: await sessionManager.getSession(cookies) } );
14-
}
15-
throw redirect(302, '/');
6+
if (locals && locals.isUserLoggedIn) {
7+
const additionalData = await request.json();
8+
const newSessionData = { ...locals.user, ...additionalData };
9+
const { data, error, message } = await sessionManager.updateSession(cookies, newSessionData);
10+
if (!error) {
11+
locals.user = newSessionData;
12+
}
13+
return json({
14+
success: !error,
15+
message,
16+
sessionData: await sessionManager.getSession(cookies)
17+
});
18+
}
19+
throw redirect(302, '/');
1620
}) satisfies RequestHandler;

0 commit comments

Comments
 (0)