svecure
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

🔐 Svecure - Secure your load functions

Secure your load functions with wrappers that will handle errorchecking for you.

Installation

npm install svecure

# or yarn
yarn add svecure

# or pnpm
pnpm add svecure

Usage

// +(page|layout).ts

import { createLoadVerifier } from 'svecure';
import { PageLoad } from './$types';

const withVerification = createLoadVerifier<PageLoad>(() => {
	// ...your verification logic
	return true;
});

export const load = withVerification(() => {
	return {
		// ...your data
	};
});
// +(page|layout).server.ts

import { createLoadVerifier } from 'svecure';
import { PageServerLoad } from './$types';

const withVerification = createLoadVerifier<PageServerLoad>(() => {
	// ...your verification logic
	return true;
});

export const load = withVerification(() => {
	return {
		// ...your data
	};
});

Custom error messages

If the verification fails, the load functino will by default throw a SvelteKit error with a status of 401 and a message of Unauthorized. You can customize this by passing a second argument to createLoadVerifier:

const withVerification = createLoadVerifier<PageLoad>(
	() => {
		// ...your verification logic
		return false;
	},
	{
		status: 403,
		message: 'You are not allowed to access this page'
	}
);

Or with a custom function (if you for example need to redirect to a login page):

import { redirect } from '@sveltejs/kit';

const withVerification = createLoadVerifier<PageLoad>(
	() => {
		// ...your verification logic
		return false;
	},
	() => {
		throw redirect(302, '/login');
	}
);

Package Sidebar

Install

npm i svecure

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

7.55 kB

Total Files

14

Last publish

Collaborators

  • afroborg