Skip to content

Commit

Permalink
feat: ✨ update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
willin committed Dec 12, 2023
1 parent 37238f9 commit d565f53
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 393 deletions.
8 changes: 8 additions & 0 deletions .changeset/tricky-vans-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@svelte-dev/auth-oauth2": patch
"@svelte-dev/session": patch
"@svelte-dev/auth": patch
"@svelte-dev/i18n": patch
---

chore: update readme
3 changes: 3 additions & 0 deletions apps/web/src/routes/(zh)/i18n/+page.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
title: '@svelte-dev/i18n'
desc: 一个简单好用的 Svelte 国际化脚手架工具
---

待完善。
3 changes: 3 additions & 0 deletions apps/web/src/routes/en/i18n/+page.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
---
title: '@svelte-dev/i18n'
desc: A simple and easy-to-use Svelte I18n management library
---

TBD.
152 changes: 1 addition & 151 deletions packages/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,157 +4,7 @@

[![github](https://img.shields.io/github/followers/willin.svg?style=social&label=Followers)](https://github.com/willin) [![npm](https://img.shields.io/npm/v/@svelte-dev/auth.svg)](https://npmjs.org/package/@svelte-dev/auth) [![npm](https://img.shields.io/npm/dm/@svelte-dev/auth.svg)](https://npmjs.org/package/@svelte-dev/auth) [![npm](https://img.shields.io/npm/dt/@svelte-dev/auth.svg)](https://npmjs.org/package/@svelte-dev/auth)

Simple Authentication for [Svlelte](https://svelte.dev/).

## Features

- Full **Server-Side** Authentication
- Complete **TypeScript** Support
- **Strategy**-based Authentication
- Easily handle **success and failure**
- Implement **custom** strategies
- Supports persistent **sessions**

## Overview

Svelte Auth is a complete open-source authentication solution for Svelte applications.

Heavily inspired by [Passport.js](https://passportjs.org) and [Remix-Auth](https://github.com/sergiodxa/remix-auth), but completely rewrote it from scratch to work on top of the [Web Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). Svelte Auth can be dropped in to any Svelte-based application with minimal setup.

As with Passport.js, it uses the strategy pattern to support the different authentication flows. Each strategy is published individually as a separate npm package.

## Installation

To use it, install it from npm (yarn or bun):

```bash
npm install @svelte-dev/auth @svelte-dev/session
```

## Usage

API Specification: [Documentation](https://svelte-auth.js.cool/docs/)

Here's an simple Example:

```ts
// hooks.server.ts
import { env } from '$env/dynamic/private';
import { sequence } from '@sveltejs/kit/hooks';
import { handleAuth } from '@svelte-dev/auth';
import { OAuth2Strategy } from '@svelte-dev/auth-oauth2';

const oauthStrategy = new OAuth2Strategy(
{
clientID: env.SSO_ID,
clientSecret: env.SSO_SECRET,
callbackURL: env.SSO_CALLBACK_URL || 'http://localhost:8788/auth/oauth2/callback'
},
async ({ profile }) => {
// Get the user data from your DB or API using the tokens and profile
return profile;
}
);

export const handle = handleAuth({
// Auth Options
autoRouting: true,
strategies: [oauthStrategy],
sessionKey: 'user',
sessionErrorKey: 'auth:error',
sessionStrategyKey: 'strategy',
successRedirect: '/',
failureRedirect: '/',
// Session Storage Options
adapter: {
name: 'cookie',
options: {
chunk: true
}
},
session: {
secrets: ['s3cr3t']
},
cookie: {
secure: !!env.SSO_CALLBACK_URL,
sameSite: 'lax',
path: '/',
httpOnly: !!env.SSO_CALLBACK_URL
}
});
```

That's it.

## Advanced Usage

If you did not set `authRouting`. You need to add a login handler `src/routes/auth/[provider]/+server.ts`:

```ts
import { redirect, type RequestEvent } from '@sveltejs/kit';

export const GET = async (event: RequestEvent) => {
const { request } = event;
const provider = event.params.provider ?? 'github';
return await event.locals.auth.authenticate(event, provider, {
successRedirect: '/dashboard',
failureRedirect: '/error'
});
};
```

Then, add a callback handler `src/routes/auth/[provider]/callback/+server.ts.ts`:

```ts
// same as before...
import type { RequestEvent } from '@sveltejs/kit';

export const GET = async (event: RequestEvent) => {
const provider = event.params.provider ?? 'github';

return await event.locals.auth.authenticate(event, provider, {
successRedirect: '/dashboard',
failureRedirect: '/error'
});
};
```

### Typescript

Modify `app.d.ts`, here is an example:

```ts
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
interface Locals {
auth: Auth;
session: SessionStorage<{ user: any }>;
user:
| {
invalid?: boolean;
[key: string]: unknown;
}
| unknown;
}
// interface PageData {}
interface Platform {
env: {
SSO_ID: string;
SSO_SECRET: string;
};
context: {
waitUntil(promise: Promise<unknown>): void;
};
caches: CacheStorage & { default: Cache };
}
}
}

export {};
```
[中文文档](https://svelte.js.cool/auth) | [English Docs](https://svelte.js.cool/en/auth)

## 赞助 Sponsor

Expand Down
4 changes: 4 additions & 0 deletions packages/i18n/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @svelte-dev/i18n

[![github](https://img.shields.io/github/followers/willin.svg?style=social&label=Followers)](https://github.com/willin) [![npm](https://img.shields.io/npm/v/@svelte-dev/i18n.svg)](https://npmjs.org/package/@svelte-dev/i18n) [![npm](https://img.shields.io/npm/dm/@svelte-dev/i18n.svg)](https://npmjs.org/package/@svelte-dev/i18n) [![npm](https://img.shields.io/npm/dt/@svelte-dev/i18n.svg)](https://npmjs.org/package/@svelte-dev/i18n)

[中文文档](https://svelte.js.cool/i18n) | [English Docs](https://svelte.js.cool/en/i18n)

## 赞助 Sponsor

维护者 Owner: [Willin Wang](https://willin.wang)
Expand Down
Loading

0 comments on commit d565f53

Please sign in to comment.