Nekonyx / next-auth-steam

Steam authentication provider for next-auth
70 stars 18 forks source link

Error at "app-router-get-server-session" example #13

Closed Noldius-Git closed 7 months ago

Noldius-Git commented 1 year ago

Auth doesn't work: "error - ReferenceError: res is not defined"

Maybe update to stable next app version, and try to resolve the bug.

Noldius-Git commented 1 year ago

The problem come from the API next route

import SteamProvider, { PROVIDER_ID } from 'next-auth-steam'
import NextAuth from 'next-auth/next'

import type { NextRequest, NextResponse } from 'next/server'
import { AuthOptions } from "next-auth";

export function getAuthOptions(req: NextRequest): AuthOptions {
    return {
        providers: req
            ? [
                SteamProvider(req, {
                    clientSecret: process.env.STEAM_SECRET!,
                    callbackUrl: 'http://localhost:3000/api/auth/callback',
                }),
            ]
            : [],
        callbacks: {
            jwt({ token, account, profile }) {
                if (account?.provider === PROVIDER_ID) {
                    token.steam = profile;
                }
                return token;
            },
            session({ session, token }) {
                if ('steam' in token) {
                    // @ts-expect-error
                    session.user.steam = token.steam;
                }
                return session;
            },
        },
    };
}

async function handler(
    req: NextRequest,
    res: NextResponse,
    ctx: { params: { nextauth: string[] } }
) {
    // @ts-expect-error
    return NextAuth(req, res, getAuthOptions(req));
}

export {
    handler as GET,
    handler as POST,
}
zoroblock commented 12 months ago

me to, how to solve this?

zoroblock commented 12 months ago

@Nekonyx hi, friend we need your help

adrianlee commented 11 months ago

@zoroblock @Noldius-Git Use NextApiRequest and NextApiResponse instead:

import SteamProvider, { PROVIDER_ID } from 'next-auth-steam'
import NextAuth, { AuthOptions } from 'next-auth/next'
import { NextApiRequest, NextApiResponse } from 'next';

export function getAuthOptions(req: NextApiRequest): AuthOptions {
    return {
        providers: req
            ? [
                SteamProvider(req, {
                    clientSecret: process.env.STEAM_SECRET!,
                    callbackUrl: 'http://localhost:3000/api/auth/callback',
                }),
            ]
            : [],
        callbacks: {
            jwt({ token, account, profile }) {
                if (account?.provider === PROVIDER_ID) {
                    token.steam = profile;
                }
                return token;
            },
            session({ session, token }) {
                if ('steam' in token) {
                    // @ts-expect-error
                    session.user.steam = token.steam;
                }
                return session;
            },
        },
    };
}

async function handler(
    req: NextApiRequest,
    res: NextApiResponse
) {
    return NextAuth(req, res, getAuthOptions(req));
}

export {
    handler as GET,
    handler as POST,
}
zoroblock commented 11 months ago

thanks bro ,but I have another problem @_@ @adrianlee


https://next-auth.js.org/errors#oauth_callback_error Unexpected token < in JSON at position 0 {
  error: SyntaxError: Unexpected token < in JSON at position 0
      at JSON.parse (<anonymous>)
      at parseJSONFromBytes (node:internal/deps/undici/undici:6662:19)
      at successSteps (node:internal/deps/undici/undici:6636:27)
      at node:internal/deps/undici/undici:1236:60
      at node:internal/process/task_queues:140:7
      at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
      at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
    name: 'OAuthCallbackError',
    code: undefined
  },
  providerId: 'steam',
  message: 'Unexpected token < in JSON at position 0'
}
avershion commented 10 months ago

@Nekonyx same issue for me, Steam doesn't authorize. Do you know does it require a particular secret?

divide29 commented 7 months ago

@Nekonyx same issue for me, Steam doesn't authorize. Do you know does it require a particular secret?

@Nekonyx same issue here too...

You have forgot to set your steam api key in the .env.local. Make sure you have added STEAM_SECRET="YOURKEY" from https://steamcommunity.com/dev/apikey

Nekonyx commented 7 months ago

Seems like all issues are caused by forgotten Steam API key. v0.2.0 now throws an error if clientSecret is not specified with some information. This should help to avoid such problems.