pevey / sveltekit-medusa-client Goto Github PK
View Code? Open in Web Editor NEWA client library for communicating with a Medusa ecommerce backend in SvelteKit
Home Page: https://pevey.com/sveltekit-medusa-client
License: MIT License
A client library for communicating with a Medusa ecommerce backend in SvelteKit
Home Page: https://pevey.com/sveltekit-medusa-client
License: MIT License
Hello,
I'm having an issue using the client's login function. Here's how I have everything set up:
/lib/server/medusa.ts
import { MEDUSA_BACKEND_URL } from '$env/static/private';
import { MedusaClient } from 'sveltekit-medusa-client';
export default new MedusaClient(MEDUSA_BACKEND_URL, {
persistentCart: true
});
app.d.ts
declare global {
namespace App {
interface Locals {
sid: string;
cartid: string;
user: any;
cart: any;
}
interface PageData {
flash?: { type: 'success' | 'error'; message: string };
}
}
}
export {};
hooks.server.ts
import medusa from '$lib/server/medusa';
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
event = await medusa.handleRequest(event);
return await resolve(event);
};
/accounts/signin/+page.server.ts
import type { PageServerLoad, Actions } from './$types';
import { fail } from '@sveltejs/kit';
import { z } from 'zod';
import { message, superValidate } from 'sveltekit-superforms/server';
import { redirect } from 'sveltekit-flash-message/server';
import medusa from '$lib/server/medusa';
const loginSchema = z.object({
email: z.string().email(),
password: z.string().min(6)
});
export const load: PageServerLoad = async () => {
const form = await superValidate(loginSchema);
return { form };
};
export const actions: Actions = {
default: async (event) => {
const form = await superValidate(event.request, loginSchema);
if (!form.valid) {
return fail(400, { form });
}
if (await medusa.login(event.locals, event.cookies, form.data.email, form.data.password)) {
const message = { type: 'success', message: 'Login successful!' } as const;
throw redirect(302, '/', message, event);
} else {
return message(form, 'Invalid email and/or password', { status: 401 });
}
}
};
/accounts/signin/+page.svelte
<script lang="ts">
import SuperDebug from 'sveltekit-superforms/client/SuperDebug.svelte';
import type { PageData } from './$types';
import { superForm } from 'sveltekit-superforms/client';
export let data: PageData;
const { form, enhance, errors, message } = superForm(data.form, {
onError({ result }) {
$message = result.error.message;
}
});
</script>
<main>
<section>
<h2>Login page</h2>
<SuperDebug data={$form} />
<br />
{#if $message}
<div>{$message}</div>
<br />
{/if}
<form method="post" use:enhance>
<label>
<span>E-Mail</span>
<input
type="email"
name="email"
autocomplete="email"
placeholder="E-Mail"
aria-invalid={$errors.email ? 'true' : undefined}
bind:value={$form.email} />
</label>
{#if $errors.email}<span>{$errors.email}</span>{/if}
<label>
<span>Password</span>
<input
type="password"
name="password"
autocomplete="current-password"
placeholder="Password"
aria-invalid={$errors.password ? 'true' : undefined}
bind:value={$form.password} />
</label>
{#if $errors.password}<span>{$errors.password}</span>{/if}
<button type="submit">Sign In</button>
</form>
<p>
<a href="#">Forgot your password?</a>
</p>
<p>
New customer? <a href="/accounts/register">Start here</a>
</p>
</section>
</main>
Upon submitting the form, the TypeError occurs. If this helps, a console log of locals on submit looks like this:
{ sid: '', cartid: '', cart: null }
Any assistance would be appreciated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.