我使用此代码能够将凭证 next-auth 提供程序与 cognito 一起使用作为 oauth 服务:这允许电子邮件和密码身份验证。我正在运行 next-auth@4.2.1:
import CognitoProvider from "next-auth/providers/cognito";
import NextAuth from 'next-auth'
import CredentialsProvider from "next-auth/providers/credentials"
import * as cognito from '../../../lib/cognito'
import { Auth } from 'aws-amplify';
export default NextAuth({
providers: [
CredentialsProvider({
credentials: {
username: { label: "Username", type: "text", placeholder: "jsmith" },
password: { label: "Password", type: "password" }
},
async authorize(credentials, req) {
try {
const user = await Auth.signIn(credentials.username, credentials.password);
return user
} catch (error) {
console.log('error signing in', error);
} …Run Code Online (Sandbox Code Playgroud) 如何registration使用自定义凭据提供程序 ( email+ password) 处理此问题?
目前我的[...nextauth].js样子是这样的:
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import axios from 'axios'
import jwt from "next-auth/jwt";
const YOUR_API_ENDPOINT = process.env.NEXT_PUBLIC_API_ROOT + 'auth/store'
const providers = [
Providers.Credentials({
name: 'Credentials',
authorize: async (credentials) => {
try {
const user = await axios.post(YOUR_API_ENDPOINT,
{
password: credentials.password,
username: credentials.email
},
{
headers: {
accept: '*/*',
'Content-Type': 'application/json'
}
})
console.log('ACCESS TOKEN ----> ' + JSON.stringify(user.data.access_token, null, 2));
if (user) {
return …Run Code Online (Sandbox Code Playgroud) 我使用 NextAuth.js 进行 Next.js 身份验证。登录工作正常,但页面仍然以错误的凭据重新加载。它没有显示任何错误。我需要处理错误以显示某种 toast 消息。
signIn("credentials", {
...values,
redirect: false,
})
.then(async () => {
await router.push("/dashboard");
})
.catch((e) => {
toast("Credentials do not match!", { type: "error" });
});
Run Code Online (Sandbox Code Playgroud) 我在 NextJs 项目上使用 NextAuth,但收到错误"Type error: Property 'session' does not exist on type '{}'."。session我按照此处的建议将该属性添加到我的 _app.tsx 中:
https://next-auth.js.org/getting-started/example
我还将该属性添加到了我的自定义MyApp类型接口中,但仍然收到错误。按照我的代码:
import { NextComponentType } from "next";
import { Session } from "next-auth";
export interface CustomAppProps extends AppProps {
Component: NextComponentType & { auth?: boolean; session?: Session };
}
function MyApp({ Component, pageProps: { session, ...pageProps } }: CustomAppProps) {
//...
});
Run Code Online (Sandbox Code Playgroud)
我该如何修复它?谢谢!
编辑#1(添加MyApp我当前的代码):
function MyApp({ Component, pageProps: { session, ...pageProps } }: CustomAppProps) {
return …Run Code Online (Sandbox Code Playgroud) 我将 next-auth 与 Prisma 和 Graphql 一起使用,我按照以下说明相应地设置了适配器和我的模型:https:
//next-auth.js.org/adapters/prisma
身份验证有效,但是当我检查会话对象时这里:
const { data: session, status } = useSession()
我没有看到身份证

我需要 ID 的原因是为了进行进一步的 GraphQL 查询。我现在使用电子邮件值通过电子邮件获取用户,但拥有可用的 ID 将是更好的选择。
我正在创建一个 next js 应用程序,使用 next-auth 来处理身份验证。
我有一个外部后端 api,所以我使用 Credentials Provider。
问题是后端发送 httponly cookie,但当我在客户端发出请求时,这些 cookie 没有附加到浏览器。
在 /pages/api/[...auth].js 中
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
import clientAxios from '../../../config/configAxios'
export default NextAuth({
providers: [
Providers.Credentials({
async authorize(credentials) {
try {
const login = await clientAxios.post('/api/login', {
username: credentials.username,
password: credentials.password,
is_master: credentials.is_master
})
const info = login.data.data.user
const token = {
accessToken: login.data.data.access_token,
expiresIn: login.data.data.expires_in,
refreshToken: login.data.data.refresh_token
}
// I can see cookies here
const cookies = login.headers['set-cookie']
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 prisma 客户端从 mongodb atlas 检索用户的数据,我编写此代码用于获取数据,它显示错误,这里 prisma 客户端代码写入作为 prisma 导入的 prismadb 文件中
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "./prismadb";
import { getServerSession } from "next-auth";
const serverAuth = async (req: NextApiRequest, res: NextApiResponse) => {
try {
const session = await getServerSession(req);
if (!session?.user?.email) {
throw new Error('Not signed in');
}
const currentUser = await prisma.user.findUnique({
where: {
email: session.user.email,
}
});
if (!currentUser) {
throw new Error('Not signed in');
}
return { currentUser };
} …Run Code Online (Sandbox Code Playgroud) 正如标题所示 - 我有一个 nextjs 应用程序在本地运行得非常好 - 但是,现在我已经将它部署到开发环境中,发生了一些非常奇怪的事情。
我已将应用程序部署到开发环境,并将NEXTAUTH_URL环境变量设置为我的开发域dev.mydomain.com。但是,当我登录时,它会将我重定向到localhost:3000. 奇怪的是,如果我的本地服务器正在运行,并且我更改了它运行的端口:4000,那么实际的网站会将我重定向到localhost:4000...就好像它知道我的代码在本地运行而无需引用它?
有谁知道这是怎么回事?我很困惑 - 我的代码库中没有任何对 localhost 的引用,更不用说特定的端口了!
我将 next auth v4 与 next js 13 beta 和服务器组件一起使用,一切正常。但我有一种情况,我需要知道登录的用户 ID,因为我正在使用下一个身份验证,我可以访问会话,我可以使用 useSession() 但随后我需要使该组件成为客户端组件,所以我想在服务器上使用它,我可以在 API 中使用 getServerSession 因为我可以访问 req & res 对象,但在下一个带有新应用程序目录的 js beta 中,我不能这样做。如果您知道如何解决该问题,请告诉我。谢谢
import { getServerSession } from "next-auth";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
const Test = async () => {
const user_id = 1; // How do I get user id from session, user_id is available in session
// I don't have access req & res object in server component.
const data = await getServerSession(request, response, authOptions); …Run Code Online (Sandbox Code Playgroud)