Giter VIP home page Giter VIP logo

sveltekit-medusa-client's People

Contributors

danielcelliot avatar pevey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sveltekit-medusa-client's Issues

TypeError: Cannot convert undefined or null to object

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.