npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-guardian

v0.1.13

Published

Batteries included authentication for SvelteKit applications.

Downloads

947

Readme

🔐 Svelte Guardian

Batteries included authentication for SvelteKit applications.

🚀 Features

  • Secure credentuals authentication
  • Multiple authentication providers
  • Robust security measures
  • Flexible configuration
  • Comprehensive logging
  • Two-factor authentication support (incoming)

📦 Installation

pnpm add svelte-guardian # or use your favorite package manager

🔧 Basic Usage

import { guardianAuth } from 'svelte-guardian';

export const { handle } = guardianAuth({
	providers: {
		google: { enabled: true },
		credentials: { enabled: true }
	},
	security: {
		level: 'strict',
		maxLoginAttempts: 5
	}
});

📄 Documentation

[Full documentation to be made available here]

🔧 Configuration API

GuardianAuthOptions Interface

interface GuardianAuthOptions {
	// Provider Configurations
	providers: {
		google?: {
			enabled: boolean;
			clientId?: string;
			clientSecret?: string;
			strict?: boolean;
		};
		credentials?: {
			enabled: boolean;
			allowRegistration?: boolean;
			passwordless?: boolean;
		};
		github?: {
			enabled: boolean;
			clientId?: string;
			clientSecret?: string;
		};
		// Extensible for more providers
	};

	// Security Configurations
	security: {
		maxLoginAttempts?: number;
		lockoutDuration?: number;
		requireEmailVerification?: boolean;
		twoFactor?: {
			enabled: boolean;
			method?: 'email' | 'totp' | 'sms';
		};
		passwordPolicy?: {
			minLength?: number;
			requireUppercase?: boolean;
			requireLowercase?: boolean;
			requireNumbers?: boolean;
			requireSpecialChars?: boolean;
		};
	};

	// Customization Options
	events?: {
		onSignIn?: (user: User) => Promise;
		onRegistration?: (user: User) => Promise;
		onPasswordReset?: (user: User) => Promise;
	};

	// Advanced Configurations
	advanced?: {
		sessionStrategy?: 'jwt' | 'database';
		tokenEncryption?: boolean;
		rateLimiting?: {
			enabled: boolean;
			requestsPerMinute?: number;
		};
	};
}

🛡️ Enhanced Security Features

1. Two-Factor Authentication

guardianAuth({
	security: {
		twoFactor: {
			enabled: true,
			method: 'totp' // Time-based One-Time Password
		}
	}
});

2. Rate Limiting Configuration

guardianAuth({
	advanced: {
		rateLimiting: {
			enabled: true,
			requestsPerMinute: 10
		}
	}
});

📝 Comprehensive Logging

// src/lib/logger.ts
import { createLogger } from 'svelte-guardian/logger';

export const authLogger = createLogger({
	level: 'info',
	destinations: [
		{ type: 'console' },
		{
			type: 'file',
			path: './logs/auth.log',
			maxSize: '10M',
			maxFiles: 5
		},
		{
			type: 'remote',
			endpoint: 'https://your-logging-service.com/logs'
		}
	]
});

🔒 Environment Variables

Create a .env file in your project root:

# Authentication Providers
GUARDIAN_GOOGLE_CLIENT_ID=your_google_client_id
GUARDIAN_GOOGLE_CLIENT_SECRET=your_google_client_secret
GUARDIAN_GITHUB_CLIENT_ID=your_github_client_id
GUARDIAN_GITHUB_CLIENT_SECRET=your_github_client_secret

# Security
GUARDIAN_JWT_SECRET=your_jwt_secret
GUARDIAN_ENCRYPTION_KEY=your_encryption_key

# Database
DATABASE_URL=your_database_connection_string

🔬 Advanced Usage Example

import { guardianAuth, type User } from 'svelte-guardian';
import { authLogger } from '$lib/logger';

export const { handle } = guardianAuth({
	providers: {
		google: { enabled: true },
		credentials: {
			enabled: true,
			allowRegistration: true
		}
	},
	security: {
		maxLoginAttempts: 5,
		lockoutDuration: 15 * 60 * 1000,
		twoFactor: {
			enabled: true,
			method: 'totp'
		},
		passwordPolicy: {
			minLength: 12,
			requireUppercase: true,
			requireNumbers: true,
			requireSpecialChars: true
		}
	},
	events: {
		async onSignIn(user: User) {
			authLogger.info(`User signed in: ${user.email}`);
			// Additional custom logic
		},
		async onRegistration(user: User) {
			authLogger.info(`New user registered: ${user.email}`);
			// Send welcome email, etc.
		}
	},
	advanced: {
		sessionStrategy: 'database',
		rateLimiting: {
			enabled: true,
			requestsPerMinute: 10
		}
	}
});

Note:

To use this library, use must use prisma Plans are in place to migrate from this prisma-centric approach. In the meantime, check out the example schema in src/prisma/schema.prisma.

Roadmap and Feature Planning

Immediate Priorities (v0.2.0) Role-based Route Protection Custom Event Handlers Multi-Session Management Logging System Multi-Database Provider Support Comprehensive Documentation Robust Error Handling Middleware Customizable Password Policies

Short-term Goals (v0.3.0) Rate Limiting and Brute Force Protection Social Account Linking Additional Authentication Providers

GitHub OAuth Microsoft OAuth Facebook OAuth Custom SAML Provider

Two-Factor Authentication (2FA) Advanced Logging and Audit Trail Comprehensive Test Suite

Mid-term Goals (v0.4.0) Advanced Role and Permission Management Internationalization (i18n) Support

🛠 TODO

  • [ ] Implement two factor auth, rate limiting, refresh token rotation
  • [ ] Create comprehensive documentation site
  • [ ] Add more authentication providers and database providers
  • [ ] Implement more granular role-based access control
  • [ ] Develop admin dashboard for user management

🤝 Contributing

[Contribution guidelines]

📄 License

MIT License