#API endpoints in the App directory only work when the requests come from inside the application?

1 messages · Page 1 of 1 (latest)

ivory pulsar
#

I am using Next 14 and I am currently setting up Lucia Auth with a GitHub providing but having issues with the redirect URL. I believe that previously the API directory is publicly available for foreign requests and any endpoints in the application file based routing is private and only available for requests within the application.

So here is how it works step by step.

User clicks login and is sent to /auth/students/login/github and then successfully I authorise access to my github account and the callback url is supposed to be /auth/students/login/github/callback which unfortunately return and error status 500. So therefore, not too what the issue is. I've tried moving the callback URL into the API directory but the same issue appears. 🤷‍♂️

light surgeBOT
#

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

#

Based on the information provided in the extracts, it seems like you're trying to set up authentication using NextAuth.js with GitHub as a provider. The issue you're facing might be related to the setup of your API routes or the configuration of your NextAuth.js setup.

In Next.js, any file inside the pages/api directory is treated as an API endpoint and is server-side only. These API routes are publicly accessible and can handle requests from outside the application. They are not limited to internal requests only.

For your GitHub authentication, you need to set up a specific route for authentication as required by NextAuth.js. This is done by creating a new file in the pages/api/auth directory named [...nextauth].ts. This file should contain the configuration for your NextAuth.js setup with your GitHub OAuth credentials and the Prisma adapter.

Here's the boilerplate code for the [...nextauth].ts file:

import { NextApiHandler } from 'next';
import NextAuth from 'next-auth';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import GitHubProvider from 'next-auth/providers/github';
import prisma from '../../../lib/prisma';

const authHandler: NextApiHandler = (req, res) => NextAuth(req, res, options);
export default authHandler;

const options = {
  providers: [
    GitHubProvider({
      clientId: process.env.GITHUB_ID,
      clientSecret: process.env.GITHUB_SECRET,
    }),
  ],
  adapter: PrismaAdapter(prisma),
  secret: process.env.SECRET,
};

(Source: Vercel Guide (https://vercel.com/guides/nextjs-prisma-postgres))

If you're still facing issues, it might be due to the configuration of your GitHub OAuth credentials or the setup of your Prisma adapter. Make sure these are correctly configured. Also, ensure that your callback URL is correctly set up in your GitHub OAuth app settings.

#

If the issue persists, it might be a good idea to check the server logs for more detailed error messages or consider asking for help in the Next.js GitHub discussions or StackOverflow.

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

#

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

ivory pulsar
light surgeBOT
#

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

#

I'm sorry, but the information extracts provided do not contain specific details or instructions about setting up Lucia Auth with Next.js. The extracts mainly cover authentication setup using NextAuth.js with GitHub as a provider.

For Lucia Auth, I recommend following the instructions provided in the tutorial you mentioned or reaching out to Lucia Auth's support or community for more specific guidance.

If you encounter issues or errors, they might be more equipped to provide solutions as they are more familiar with the intricacies of their own service.

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