Giter VIP home page Giter VIP logo

sveltekit-route-hooks's Introduction

SvelteKit Route Hooks

Warning

It's not recommended to use this library in production, this was an experiment to see if it was possible to create route-scoped hooks in SvelteKit. It is, but it's not pretty and depends on adapter-specific implementation.

This library makes it easy to create hooks that are scoped to specific routes right where you expect them: in your routing tree. No more need for string pattern matching in your hook files when guarding routes!

Getting started

First, install the library:

npm install --save-dev sveltekit-route-hooks

Next, configure your project hooks to use the handlers:

// src/hooks.server.js
export { handle } from 'sveltekit-route-hooks';

Finally, use exported helpers to create hooks scoped to routes. Primitive authentication that runs on every request and then guards an admin route might look like:

// src/routes/+layout.server.js
import { handleRoute } from 'sveltekit-route-hooks';
import { db } from '$lib/db';

handleRoute(async function ({ event, resolve }) {
    const sessionId = event.cookies.get('session');
    const user = await db.getUserInfo({ sessionId });
    event.locals = {
        ...event.locals,
        user,
    };
    return await resolve(event);
});

// src/routes/admin/+layout.server.js
import { error } from '@sveltejs/kit';
import { handleRoute } from 'sveltekit-route-hooks';

handleRoute(async function ({ event, resolve }) {
    if (!event.locals?.user?.isAdmin) {
        throw error(403, 'Forbidden');
    }

    return await resolve(event);
});

sveltekit-route-hooks's People

Contributors

dslatkin avatar

Watchers

 avatar

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.