Skip to main content
Latest
File

oauth2

Discord

Note: discord_client_id and discord_client_secret need to be set as environment variables.

api/oauth2/discord/login.ts

import { Get, v } from 'darkflare'
import { Discord } from 'darkflare/oauth2'

Get({}, async c => {
  Discord.redirect(c) // only 'read:user' scope

  // alternatively, you can specify a custom set of scopes:
  Discord.redirect(c, ['read:user', 'user:email'])
})

api/oauth2/discord/callback.ts

import { Get, v } from 'darkflare'
import { Discord } from 'darkflare/oauth2'

Get({
  query: v.Object({
    code: v.String()
  })
}, async c => {
  const token = await Discord.getAccessToken(c)
  const user = await Discord.getUser(token)

  return `Welcome, ${user.name}!`
})

Github

Note: github_client_id and github_client_secret need to be set as environment variables.

api/oauth2/github/login.ts

import { Get, v } from 'darkflare'
import { Github } from 'darkflare/oauth2'

Get({}, async c => {
  Github.redirect(c) // only 'read:user' scope

  // alternatively, you can specify a custom set of scopes:
  Github.redirect(c, ['read:user', 'user:email'])
})

api/oauth2/github/callback.ts

import { Get, v } from 'darkflare'
import { Github } from 'darkflare/oauth2'

Get({
  query: v.Object({
    code: v.String()
  })
}, async c => {
  const token = await Github.getAccessToken(c)
  const user = await Github.getUser(token)

  return `Welcome, ${user.name}!`
})

Spotify

Note: spotify_client_id and spotify_client_secret need to be set as environment variables.

api/oauth2/spotify/login.ts

import { Get, v } from 'darkflare'
import { Spotify } from 'darkflare/oauth2'

Get({}, async c => {
  Spotify.redirect(c) // only 'read:user' scope

  // alternatively, you can specify a custom set of scopes:
  Spotify.redirect(c, ['read:user', 'user:email'])
})

api/oauth2/spotify/callback.ts

import { Get, v } from 'darkflare'
import { Spotify } from 'darkflare/oauth2'

Get({
  query: v.Object({
    code: v.String()
  })
}, async c => {
  const token = await Spotify.getAccessToken(c)
  const user = await Spotify.getUser(token)

  return `Welcome, ${user.name}!`
})