atinux / nuxt-auth-utils

Add Authentication to Nuxt applications with secured & sealed cookies sessions.
MIT License
976 stars 91 forks source link

Discord Always Fails #81

Closed ilrein closed 7 months ago

ilrein commented 7 months ago

Hello, I was giving discord a try:

{
    label: 'Discord',
    icon: 'i-simple-icons-discord',
    color: 'indigo',
    click: () => {
      loading.value = true
      window.location.href = '/api/auth/discord'
    }
  }

Inside of ~/server/api/auth/discord.get.ts I have

  export default oauth.discordEventHandler({
  config: {
    emailRequired: true,
    profileRequired: true,
  },
  async onSuccess(event, { user, tokens }) {
    console.log('Discord OAuth success:', user)
    await setUserSession(event, {
      user
    })
    return sendRedirect(event, '/dashboard')
  },
  // Optional, will return a json error and 401 status code by default
  onError(event, error) {
    console.error('Discord OAuth error:', event, error)
    return sendRedirect(event, '/')
  },
})

I am able to load the discord authorization screen, but when it performs the callback, I get an error:

Discord OAuth error: H3Event {
  __is_event__: true,
  node:
   { req:
      IncomingMessage {
        _events: [Object],
        _readableState: [ReadableState],
        _maxListeners: undefined,
        socket: [Socket],
        httpVersionMajor: 1,
        httpVersionMinor: 1,
        httpVersion: '1.1',
        complete: true,
        rawHeaders: [Array],
        rawTrailers: [],
        joinDuplicateHeaders: null,
        aborted: false,
        upgrade: false,
        url: '/api/auth/discord?code=04yK3Z0BVKV2B83O3UVZooaTZa2G0f',
        method: 'GET',
        statusCode: null,
        statusMessage: null,
        client: [Socket],
        _consuming: false,
        _dumped: false,
        originalUrl: '/api/auth/discord?code=04yK3Z0BVKV2B83O3UVZooaTZa2G0f',
        [Symbol(shapeMode)]: true,
        [Symbol(kCapture)]: false,
        [Symbol(kHeaders)]: [Object],
        [Symbol(kHeadersCount)]: 36,
        [Symbol(kTrailers)]: null,
        [Symbol(kTrailersCount)]: 0 },
     res:
      ServerResponse {
        _events: [Object: null prototype],
        _eventsCount: 1,
        _maxListeners: undefined,
        outputData: [],
        outputSize: 0,
        writable: true,
        destroyed: false,
        _last: false,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        maxRequestsOnConnectionReached: false,
        _defaultKeepAlive: true,
        useChunkedEncodingByDefault: true,
        sendDate: true,
        _removedConnection: false,
        _removedContLen: false,
        _removedTE: false,
        strictContentLength: false,
        _contentLength: null,
        _hasBody: true,
        _trailer: '',
        finished: false,
        _headerSent: false,
        _closed: false,
        _header: null,
        _keepAliveTimeout: 5000,
        _onPendingData: [Function: bound updateOutgoingData],
        req: [IncomingMessage],
        _sent100: false,
        _expect_continue: false,
        _maxRequestsPerSocket: 0,
        [Symbol(shapeMode)]: false,
        [Symbol(kCapture)]: false,
        [Symbol(kBytesWritten)]: 0,
        [Symbol(kNeedDrain)]: false,
        [Symbol(corked)]: 0,
        [Symbol(kChunkedBuffer)]: [],
        [Symbol(kChunkedLength)]: 0,
        [Symbol(kSocket)]: [Socket],
        [Symbol(kOutHeaders)]: null,
        [Symbol(errored)]: null,
        [Symbol(kHighWaterMark)]: 16384,
        [Symbol(kRejectNonStandardBodyWrites)]: false,
        [Symbol(kUniqueHeaders)]: null } },
  web: undefined,
  context:
   { _nitro: { routeRules: {} },
     nitro: { errors: [], runtimeConfig: [Object] },
     matchedRoute: { path: '/api/auth/discord', handlers: [Object] },
     params: {} },
  _method: 'GET',
  _path: '/api/auth/discord?code=04yK3Z0BVKV2B83O3UVZooaTZa2G0f',
  _headers: undefined,
  _requestBody: undefined,
  _handled: false,
  fetch: [Function (anonymous)],
  '$fetch': [Function (anonymous)],
  waitUntil: [Function (anonymous)],
  captureError: [Function (anonymous)] } Discord login failed: Unknown error

Any ideas?

atinux commented 7 months ago

Weird, it works for me locally though. Did you set everything right in the Discord app?

ilrein commented 7 months ago

Now I feel silly. It worked for me for the first time. Odd.

Bryceed commented 5 months ago

Discord redirect_uri does not matches with the actual project domain

image image

Probably this will not be a problem in production, but since there's no config value to send in auth/discord.ts, this will need more workarounds than necessary. There's a minimal way to replace theses settings without removing the port number in development environment?