1.0.0 • Published 5 months ago

@svelte-dev/auth-github v1.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

Logo

@svelte-dev/auth-github strategy

The Github strategy is used to authenticate users against a github account. It extends the OAuth2Strategy.

For more details: https://github.com/willin/svelte-turbo

Supported runtimes

RuntimeHas Support
Node.js
Cloudflare
Vercel

Usage

Create an OAuth application

Follow the steps on the GitHub documentation to create a new application and get a client ID and secret.

Create the strategy instance

import { GitHubStrategy } from '@svelte-dev/auth-github';

let gitHubStrategy = new GitHubStrategy(
  {
    clientID: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    callbackURL: 'https://example.com/auth/github/callback'
  },
  async ({ accessToken, extraParams, profile }) => {
    // Get the user data from your DB or API using the tokens and profile
    return User.findOrCreate({ email: profile.emails[0].value });
  }
);

auth.use(gitHubStrategy);

Setup your routes

<form action="/auth/github" method="get">
  <button>Login with Github</button>
</form>
// routes/auth/github/+server
import { authenticator } from '~/auth.server';
import type { RequestHandler } from './$types';

export const POST: RequestHandler = async (event) => {
  return authenticator.authenticate('github', event);
};
// routes/auth/github/callback/+server
import { authenticator } from '~/auth.server';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ event }) => {
  return authenticator.authenticate('github', event, {
    successRedirect: '/dashboard',
    failureRedirect: '/login'
  });
};

赞助 Sponsor

维护者 Owner: Willin Wang

如果您对本项目感兴趣,可以通过以下方式支持我:

Donation ways:

许可证 License

Apache-2.0