|
1 |
| -# create-svelte |
| 1 | + |
2 | 2 |
|
3 |
| -Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte). |
| 3 | +# svelte-auth-github strategy |
4 | 4 |
|
5 |
| -Read more about creating a library [in the docs](https://kit.svelte.dev/docs/packaging). |
| 5 | +The Github strategy is used to authenticate users against a github account. It extends the OAuth2Strategy. |
6 | 6 |
|
7 |
| -## Creating a project |
| 7 | +For more details: <https://github.com/willin/svelte-auth> |
8 | 8 |
|
9 |
| -If you're seeing this, you've probably already done this step. Congrats! |
| 9 | +## Supported runtimes |
10 | 10 |
|
11 |
| -```bash |
12 |
| -# create a new project in the current directory |
13 |
| -npm create svelte@latest |
| 11 | +| Runtime | Has Support | |
| 12 | +| ---------- | ----------- | |
| 13 | +| Node.js | ✅ | |
| 14 | +| Cloudflare | ✅ | |
| 15 | +| Vercel | ✅ | |
14 | 16 |
|
15 |
| -# create a new project in my-app |
16 |
| -npm create svelte@latest my-app |
17 |
| -``` |
| 17 | +## Usage |
18 | 18 |
|
19 |
| -## Developing |
| 19 | +### Create an OAuth application |
20 | 20 |
|
21 |
| -Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: |
| 21 | +Follow the steps on [the GitHub documentation](https://docs.github.com/en/developers/apps/building-oauth-apps/creating-an-oauth-app) to create a new application and get a client ID and secret. |
22 | 22 |
|
23 |
| -```bash |
24 |
| -npm run dev |
| 23 | +### Create the strategy instance |
25 | 24 |
|
26 |
| -# or start the server and open the app in a new browser tab |
27 |
| -npm run dev -- --open |
28 |
| -``` |
| 25 | +```ts |
| 26 | +import { GitHubStrategy } from '@svelte-dev/auth-github'; |
29 | 27 |
|
30 |
| -Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app. |
| 28 | +let gitHubStrategy = new GitHubStrategy( |
| 29 | + { |
| 30 | + clientID: 'YOUR_CLIENT_ID', |
| 31 | + clientSecret: 'YOUR_CLIENT_SECRET', |
| 32 | + callbackURL: 'https://example.com/auth/github/callback' |
| 33 | + }, |
| 34 | + async ({ accessToken, extraParams, profile }) => { |
| 35 | + // Get the user data from your DB or API using the tokens and profile |
| 36 | + return User.findOrCreate({ email: profile.emails[0].value }); |
| 37 | + } |
| 38 | +); |
31 | 39 |
|
32 |
| -## Building |
| 40 | +auth.use(gitHubStrategy); |
| 41 | +``` |
33 | 42 |
|
34 |
| -To build your library: |
| 43 | +### Setup your routes |
35 | 44 |
|
36 |
| -```bash |
37 |
| -npm run package |
| 45 | +```html |
| 46 | +<form action="/auth/sso" method="get"> |
| 47 | + <button>Login with SSO</button> |
| 48 | +</form> |
38 | 49 | ```
|
39 | 50 |
|
40 |
| -To create a production version of your showcase app: |
| 51 | +```tsx |
| 52 | +// routes/auth/github/+server |
| 53 | +import { authenticator } from '~/auth.server'; |
| 54 | +import type { RequestHandler } from './$types'; |
| 55 | + |
| 56 | +export const POST: RequestHandler = async (event) => { |
| 57 | + return authenticator.authenticate('github', event); |
| 58 | +}; |
| 59 | +``` |
41 | 60 |
|
42 |
| -```bash |
43 |
| -npm run build |
| 61 | +```tsx |
| 62 | +// routes/auth/github/callback/+server |
| 63 | +import { authenticator } from '~/auth.server'; |
| 64 | +import type { PageServerLoad } from './$types'; |
| 65 | + |
| 66 | +export const load: PageServerLoad = async ({ event }) => { |
| 67 | + return authenticator.authenticate('github', event, { |
| 68 | + successRedirect: '/dashboard', |
| 69 | + failureRedirect: '/login' |
| 70 | + }); |
| 71 | +}; |
44 | 72 | ```
|
45 | 73 |
|
46 |
| -You can preview the production build with `npm run preview`. |
| 74 | +## 赞助 Sponsor |
47 | 75 |
|
48 |
| -> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment. |
| 76 | +维护者 Owner: [Willin Wang](https://willin.wang) |
49 | 77 |
|
50 |
| -## Publishing |
| 78 | +如果您对本项目感兴趣,可以通过以下方式支持我: |
51 | 79 |
|
52 |
| -Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)). |
| 80 | +- 关注我的 Github 账号:[@willin](https://github.com/willin) [](https://github.com/willin) |
| 81 | +- 参与 [爱发电](https://afdian.net/@willin) 计划 |
| 82 | +- 支付宝或微信[扫码打赏](https://user-images.githubusercontent.com/1890238/89126156-0f3eeb80-d516-11ea-9046-5a3a5d59b86b.png) |
53 | 83 |
|
54 |
| -To publish your library to [npm](https://www.npmjs.com): |
| 84 | +Donation ways: |
55 | 85 |
|
56 |
| -```bash |
57 |
| -npm publish |
58 |
| -``` |
| 86 | +- Github: <https://github.com/sponsors/willin> |
| 87 | +- Paypal: <https://paypal.me/willinwang> |
| 88 | +- Alipay or Wechat Pay: [QRCode](https://user-images.githubusercontent.com/1890238/89126156-0f3eeb80-d516-11ea-9046-5a3a5d59b86b.png) |
| 89 | + |
| 90 | +## 许可证 License |
| 91 | + |
| 92 | +Apache-2.0 |
0 commit comments