Skip to content

thgh/payload-plugin-oauth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

oAuth plugin for Payload CMS

Software License Issues NPM

Features

  • Configures passport-oauth2
  • Mounts authorize & callback route
  • Adds sign in button on login page

Installation

npm install payload-plugin-oauth2
# or
yarn add payload-plugin-oauth2

Usage

// payload.config.ts
import { oAuthPlugin } from 'payload-plugin-oauth2'

export default buildConfig({
  serverURL: process.env.SERVER_URL,
  collections: [Users],
  plugins: [
    oAuthPlugin({
      clientID: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      authorizationURL: process.env.OAUTH_SERVER + '/oauth/authorize',
      tokenURL: process.env.OAUTH_SERVER + '/oauth/token',
      callbackURL: process.env.SERVER_URL + CALLBACK_PATH,
      scope: 'basic',
      async userinfo(accessToken) {
        const { data: user } = await axios.get(OAUTH_SERVER + '/oauth/me', {
          params: { access_token: accessToken },
        })
        return {
          sub: user.ID,

          // Custom fields to fill in if user is created
          name: user.display_name || user.user_nicename || 'Naamloos',
          email: user.user_email,
          role: user.capabilities?.administrator ? 'admin' : 'user',
        }
      },
    }),
  ],
})

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Contributions and feedback are very welcome.

To get it running:

  1. Clone the project.
  2. npm install
  3. npm run build

Credits

License

The MIT License (MIT). Please see License File for more information.