0.0.1 • Published 6 years ago

microauth-discord v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

microauth-discord

Discord oauth for micro

Add discord authentication to your micro in few lines of code. This module is a part of microauth collection.

Installation

npm install --save microauth-discord
# or
yarn add microauth-discord

Usage

app.js

const { send } = require("micro")
const microAuthDiscord = require("microauth-discord")

const options = {
  clientId: "CLIENT_ID",
  clientSecret: "CLIENT_SECRET",
  callbackUrl: "http://localhost:3000/auth/discord/callback",
  path: "/auth/discord",
  scope: "identify email"
}

const discordAuth = microAuthDiscord(options)

// third `auth` argument will provide error or result of authentication
// so it will { err: errorObject } or { result: {
//  provider: 'discord',
//  accessToken: 'blahblah',
//  info: userInfo
// }}

module.exports = discordAuth(async (req, res, auth) => {
  if (!auth) {
    return send(res, 404, "Not Found")
  }

  if (auth.err) {
    // Error handler
    return send(res, 403, "Forbidden")
  }

  return `Hello ${auth.result.info.username}#${auth.result.info.discriminator}`
})

Run:

micro app.js

Now visit http://localhost:3000/auth/discord

Author

Giovanni Cavallo

0.0.1

6 years ago