Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nextjs14 with next-connect not working #239

Open
epubreader opened this issue Dec 22, 2023 · 1 comment
Open

nextjs14 with next-connect not working #239

epubreader opened this issue Dec 22, 2023 · 1 comment

Comments

@epubreader
Copy link

TypeError: res.setHeader is not a function
at strategy.redirect (webpack-internal:///(rsc)/./node_modules/passport/lib/middleware/authenticate.js:322:21)
at stored (webpack-internal:///(rsc)/./node_modules/passport-oauth2/lib/strategy.js:285:22)
at NullStore.store (webpack-internal:///(rsc)/./node_modules/passport-oauth2/lib/state/null.js:3:5)
at OAuth2Strategy.authenticate (webpack-internal:///(rsc)/./node_modules/passport-oauth2/lib/strategy.js:296:38)
at attempt (webpack-internal:///(rsc)/./node_modules/passport/lib/middleware/authenticate.js:353:22)
at authenticate (webpack-internal:///(rsc)/./node_modules/passport/lib/middleware/authenticate.js:354:11)
at eval (webpack-internal:///(rsc)/./app/api/auth/[provider]/route.js:55:11)
at new Promise ()
at authenticate (webpack-internal:///(rsc)/./app/api/auth/[provider]/route.js:46:42)
at Array.eval (webpack-internal:///(rsc)/./app/api/auth/[provider]/route.js:61:28)
at next (webpack-internal:///(rsc)/./node_modules/next-connect/dist/esm/router.js:67:34)
at Array.initialize (webpack-internal:///(rsc)/./node_modules/passport/lib/middleware/initialize.js:86:9)
at Router.exec (webpack-internal:///(rsc)/./node_modules/next-connect/dist/esm/router.js:68:22)
at EdgeRouter.run (webpack-internal:///(rsc)/./node_modules/next-connect/dist/esm/edge.js:47:63)
at GET (webpack-internal:///(rsc)/./app/api/auth/[provider]/route.js:106:23)
at /home/mason/project/epubpub/nextjs/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:63257
at /home/mason/project/epubpub/nextjs/node_modules/next/dist/server/lib/trace/tracer.js:133:36

`import passport from "passport";
import { Strategy as GitHubOAuth2Strategy } from "passport-github";
import { OAuth2Strategy as GoogleOAuth2Strategy } from "passport-google-oauth";

//import api from "@/api";

const SUPPORTED_PROVIDERS = ["google", "github"];
import { NextResponse } from "next/server";

import { createEdgeRouter } from "next-connect";

passport.use(
new GitHubOAuth2Strategy(
{
clientID: process.env.GITHUB_CLIENT_ID ?? "",
clientSecret: process.env.GITHUB_CLIENT_SECRET ?? "",
callbackURL: process.env.GITHUB_CALLBACK_URL ?? "",
scope: ["user:email"],
},
(accessToken, _, profile, cb) => {
return cb(null, { accessToken, profile });
}
)
);
passport.use(
new GoogleOAuth2Strategy(
{
clientID: process.env.GOOGLE_CLIENT_ID ?? "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? "",
callbackURL: process.env.GOOGLE_CALLBACK_URL ?? "",
scope: "profile email",
},
(accessToken, _, profile, cb) => {
return cb(null, { accessToken, profile });
}
)
);

const authenticate = (method, req, res) =>
new Promise((resolve, reject) => {
passport.authenticate(method, { session: false }, (error, token) => {
if (error) {
reject(error);
} else {
resolve(token);
}
})(req, res);
});

const router = createEdgeRouter();

router.use(passport.initialize()).get(async (req, res) => {
const { provider } = res.params;
try {
const user = await authenticate(provider, req, res);
return NextResponse.json({ user });
} catch (error) {
console.error(error);
return NextResponse.json(error.message, { status: 401 });
}
});

export async function GET(req, ctx) {
// Validate provider
const { provider } = ctx.params;

if (typeof provider !== "string") {
return Response.json(
{
success: false,
error: "Invalid OAuth2 provider.",
},
{
status: 400,
}
);
}
if (!SUPPORTED_PROVIDERS.includes(provider)) {
return Response.json(
{
success: false,
error: ${provider} is not a supported OAuth2 provider.,
},
{
status: 400,
}
);
}

// Check if we're already logged in
// const user = await api.me(req, res);
// if (user.success && user?.payload) {
// // If User hasn't completed account creation then redirect
// if (user.payload.user?.state !== "COMPLETE") {
// res.redirect("/profile");
// return;
// }
// res.redirect("/");
// return;
// }

try {
const opts = {
session: false,
};

return router.run(req, ctx);
//await passport.authenticate(provider, opts)(req, new NextResponse());

} catch (err) {
console.error(err);
return Response.json(
{
success: false,
error: Failed to authenticate with ${provider}. Please try again later.,
},
{
status: 500,
}
);
}
}
`

@mkbctrl
Copy link

mkbctrl commented Dec 26, 2023

please make sure your code is properly wrapped. It's pain to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants