Next-auth: Custom provider returns a Missing or invalid provider account

Created on 26 Aug 2020  路  2Comments  路  Source: nextauthjs/next-auth

Your question
We run our own oAuth provider at work, and I'm trying to write an integration for it in next-auth, however i'm running into an issue when signing in where it's erroring with Missing or invalid provider account. having debugged the code, it seems to be missing an id on the providerAccount in the callback-handler.js.

What are you trying to do
Using this as my custom provider, I should be able to login to our own oAuth2 service.

{
      id: 'openimp',
      name: 'OpenImp',
      type: 'oauth',
      version: '2.0',
      accessTokenUrl: 'https://auth.example.com/oauth/token',
      authorizationUrl: 'https://auth.example.com/dialog/authorise?response_type=code',
      profileUrl: 'https://auth.example.com/api/userinfo',
      scope: [],
      params: { grant_type: 'authorization_code' },
      profile: (profile) => {
        return {
          username: profile.identifier,
          name: profile.cn + ' ' + profile.sn,
          email: profile.email,
        };
      },
      clientId: 'blah',
      clientSecret: 'secretblah',
    }

instead it returns an error of:

[next-auth][error][oauth_callback_handler_error] Error: Missing or invalid provider account
    at /Users/jared/Projects/ssh-key-service/src/frontend/node_modules/next-auth/dist/server/lib/callback-handler.js:33:15
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/jared/Projects/ssh-key-service/src/frontend/node_modules/next-auth/dist/server/lib/callback-handler.js:20:103)
    at _next (/Users/jared/Projects/ssh-key-service/src/frontend/node_modules/next-auth/dist/server/lib/callback-handler.js:22:194)
    at /Users/jared/Projects/ssh-key-service/src/frontend/node_modules/next-auth/dist/server/lib/callback-handler.js:22:364
    at new Promise (<anonymous>)
    at /Users/jared/Projects/ssh-key-service/src/frontend/node_modules/next-auth/dist/server/lib/callback-handler.js:22:97
    at /Users/jared/Projects/ssh-key-service/src/frontend/node_modules/next-auth/dist/server/lib/callback-handler.js:186:17
    at /Users/jared/Projects/ssh-key-service/src/frontend/node_modules/next-auth/dist/server/routes/callback.js:108:54
    at Generator.next (<anonymous>) 
https://next-auth.js.org/errors#oauth_callback_handler_error

The providerAccount looks like:

{
  provider: 'openimp',
  type: 'oauth',
  id: undefined,
  refreshToken: undefined,
  accessToken: 'long token',
  accessTokenExpires: null
}

Is my code wrong, or is our oAuth2 service oncorrectly setup?

Feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [ ] Found the documentation helpful
  • [x] Found documentation but was incomplete
  • [ ] Could not find relevant documentation
  • [ ] Found the example project helpful
  • [x] Did not find the example project helpful
question

Most helpful comment

Hi there,

If you return the username as id from in profile function, that should resolve your problem!

profile: (profile) => {
  return {
    // Return Username as ID
    // username: profile.identifier,
    id: profile.identifier,
    name: profile.cn + ' ' + profile.sn,
    email: profile.email,
  };
},

Note: The ID can be a string or an int, it only needs to be unique to your provider.

All 2 comments

Hi there,

If you return the username as id from in profile function, that should resolve your problem!

profile: (profile) => {
  return {
    // Return Username as ID
    // username: profile.identifier,
    id: profile.identifier,
    name: profile.cn + ' ' + profile.sn,
    email: profile.email,
  };
},

Note: The ID can be a string or an int, it only needs to be unique to your provider.

that did work indeed... thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmi3y picture dmi3y  路  3Comments

readywater picture readywater  路  3Comments

ryanbahan picture ryanbahan  路  3Comments

bscaspar picture bscaspar  路  3Comments

alephart picture alephart  路  3Comments