#res.

1 messages · Page 1 of 1 (latest)

dusk gobletBOT
#

Hi @glacial pecan. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error message you're seeing, TypeError: res.status is not a function, typically indicates that the res object you're trying to use does not have a status method. This could be due to a few reasons:

  1. The res object is not correctly passed into your function. Ensure that your function signature correctly receives the req (request) and res (response) objects. Here's an example from the Next.js documentation (https://nextjs.org/docs/pages/building-your-application/routing/api-routes):
export default function handler(req, res) {
  res.status(200).json({ message: 'Hello from Next.js!' })
}
  1. You're trying to use Express.js-like helper methods in a context where they are not available. In Next.js, these helper methods are available in API Routes, as described in the Next.js documentation (https://nextjs.org/docs/pages/building-your-application/routing/api-routes). If you're not in an API Route, these methods may not be available.

  2. You're using a custom server or a different framework that does not provide these helper methods on the res object.

Without more context or code, it's hard to provide a more specific solution. Please ensure that you're using the res object in the correct context and that it's being passed into your function correctly.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@glacial pecan kapa.ai is still learning and improving, please let us know how it did by reacting below

glacial pecan
#

@dusk goblet import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next';
import { cookies } from 'next/headers'; // Unused import
import { logger } from '@utils/logger';

interface ResponseObject {
status: number;
body: {
error?: string;
};
}

export const GET = async (
req: NextApiRequest,
res: NextApiResponse<{ error?: string }>
) => {

logger.log('Full request URL:', req.url);

const queryParams = new URLSearchParams(req.url?.split('?')[1] || '');
const code = queryParams.get('code');

if (!code) {
logger.error('No code provided in the query.');
const errorResponse: ResponseObject = {
status: 400,
body: { error: 'No code provided in the query.' }
};
return res.status(errorResponse.status).json(errorResponse.body);
}

// Handle possible string array for req.query.error and req.query.error_description
if (req.query?.error) {
const errorDescription = typeof req.query.error_description === 'string' ? req.query.error_description : undefined;
const error = typeof req.query.error === 'string' ? req.query.error : undefined;
logger.error('Discord OAuth error:', errorDescription || error);

const errorResponse: ResponseObject = {
  status: 400,
  body: { error: errorDescription || error }
};
return res.status(errorResponse.status).json(errorResponse.body);

}

logger.log('Extracted code:', code);

try {
const oauthResult = await axios.post(
'https://discord.com/api/oauth2/token',
new URLSearchParams({
client_id: process.env.DISCORD_CLIENT_ID!,
client_secret: process.env.DISCORD_CLIENT_SECRET!,
code,
grant_type: 'authorization_code',
redirect_uri: process.env.DISCORD_REDIRECT_URI!,
scope: 'identify guilds'
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);

const token: string = oauthResult.data.access_token;
logger.log('Received token from Discord:', token);



const successResponse: ResponseObject = {
  status: 302,
  body: {}
};
res.status(successResponse.status).setHeader('Location', process.env.DASHBOARD_URL!).end();

} catch (error: any) {
logger.error('OAuth Error:', error.response?.data || error.message);
const errorResponse: ResponseObject = {
status: 500,
body: { error: 'Internal server error.' }
};
return res.status(errorResponse.status).json(errorResponse.body);
}
};

dusk gobletBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The code you've shared is a Next.js API route handler for a GET request. It's designed to handle OAuth authentication with Discord. Here's a breakdown of what it does:

  1. It logs the full request URL.
  2. It extracts the code parameter from the query string. If no code is provided, it logs an error and sends a 400 status response with an error message.
  3. If there's an error in the query parameters, it logs the error and sends a 400 status response with the error description.
  4. If the code is present and there are no errors in the query parameters, it makes a POST request to the Discord OAuth2 token endpoint to exchange the code for an access token.
  5. If the request to Discord is successful, it logs the received token and sends a 302 status response, redirecting the client to a dashboard URL.
  6. If there's an error in the request to Discord, it logs the error and sends a 500 status response with an error message.

This code uses several features of Next.js and Node.js, including:

  • The NextApiRequest and NextApiResponse types from Next.js, which are used to type the req (request) and res (response) objects.
  • The axios library to make HTTP requests.
  • The URLSearchParams interface to work with the query string of the URL.
  • Environment variables (like process.env.DISCORD_CLIENT_ID) to securely store and access sensitive information.
  • The res.status().json() and res.status().setHeader().end() chains from Next.js to send responses with specific HTTP status codes, JSON bodies, and headers.
#

Remember to replace process.env.DISCORD_CLIENT_ID, process.env.DISCORD_CLIENT_SECRET, process.env.DISCORD_REDIRECT_URI, and process.env.DASHBOARD_URL with your actual Discord client ID, client secret, redirect URI, and dashboard URL respectively. These should be stored as environment variables for security reasons.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: