wasp/web/docs/integrations/github.md
2023-01-31 11:18:52 +01:00

2.6 KiB

title
GitHub

import useBaseUrl from '@docusaurus/useBaseUrl';

GitHub

To implement GitHub Auth, you'll need to add the Auth object with the following configuration to your main.wasp file:

app Example {
  wasp: {
    version: "^0.8.0"
  },

  title: "Example",

  auth: {
    userEntity: User,
    externalAuthEntity: SocialLogin,
    methods: {
      gitHub: {}
    },
    onAuthFailedRedirectTo: "/login"
  },
}

//...

entity User {=psl
    id          Int     @id @default(autoincrement())
    username    String  @unique
    password    String
    externalAuthAssociations  SocialLogin[]
psl=}

entity SocialLogin {=psl
  id          Int       @id @default(autoincrement())
  provider    String
  providerId  String
  user        User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId      Int
  createdAt   DateTime  @default(now())
  @@unique([provider, providerId, userId])
psl=}

For more info on the specific fields, check out this Auth section of the docs.

You'll also need to add these environment variables to your .env.server file at the root of your project:

GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret

We will cover how to get these values in the next section.

GitHub Auth

To use GitHub as an authentication method (covered here), you'll first need to create a GitHub OAuth App and provide Wasp with your client key and secret. Here is how to do so:

  1. Log into your GitHub account and navigate to: https://github.com/settings/developers
  2. Select "New OAuth App"
  3. Supply required information

<img alt="GitHub Applications Screenshot" src={useBaseUrl('img/integrations-github-1.png')} width="400px" />

  • For "Authorization callback URL", if you just want to test your local app, put in: http://localhost:3000/auth/login/github
  • Once you know on which URL your API server will be deployed, you can create a new app with that URL instead.
    • For example: https://someotherhost.com/auth/login/github
  1. Hit "Register application"
  2. Copy your Client ID and Client secret, and paste them into your environment variables named GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRETin your .env.server file.
  3. Now when youre user logs in with GitHub, you can access the logged in user on the client via the useAuth() hook, and on the server via the context.user object as described here!