강의

멘토링

커뮤니티

Inflearn Community Q&A

ohsubin's profile image
ohsubin

asked

Next + Create a SNS service with React Query

Logging in with next-auth

next-auth Login 시 middleware 이슈 질문 드립니다.

Resolved

Written on

·

1.3K

·

Edited

0

안녕하세요.

next-auth 로그인시 해결되지 않는 부분이 있어서 질문드립니다. 로그인을 계속 실패하고 있습니다.

next-auth의 버젼 (4였다가 삭제하고 3으로도 시도 해봤습니다.)

  "dependencies": {
    "next-auth": "^5.0.0-beta.3",
  },


로그인을 시도했을때 뜨는 화면:

The Middleware "/src/middleware" must export a middleware or a default function

This error happened while generating the page. Any console logs will be displayed in the terminal window.

스크린샷 2024-01-09 오후 6.07.15.png

로그인을 시도했을때 콘솔 화면:

스크린샷 2024-01-09 오후 6.08.24.png

로그인을 시도했을때 네트워크 화면 :

스크린샷 2024-01-09 오후 6.09.37.png

middleware.ts code 입니다.

import { auth as middleware } from "./auth";

// See "Matching Paths" below to learn more
export const config = {
  matcher: ["/compose/tweet", "/home", "/explore", "/messages", "/search"],
};

 

auth.ts code 입니다.

 

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

console.log("-", process.env.AUTH_URL);
export const {
  handlers: { GET, POST },
  auth,
  signIn,
} = NextAuth({
  pages: {
    signIn: "/i/flow/login",
    newUser: "/i/flow/signup",
  },
  providers: [
    CredentialsProvider({
      async authorize(credentials) {
        const authResponse = await fetch(`${process.env.AUTH_URL}/api/login`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            id: credentials.username,
            password: credentials.password,
          }),
        });

        if (!authResponse.ok) {
          return null;
        }

        const user = await authResponse.json();
        return user;
      },
    }),
  ],
});

 

handers.ts의 로그인쪽 코드입니다.

import { http, HttpResponse, StrictResponse } from "msw";
import { faker } from "@faker-js/faker";


export const handlers = [
  http.post("/api/login", () => {
    console.log("로그인");
    return HttpResponse.json({ id: "zerohch0", nickname: "제로초", image: "/5Udwvqim.jpg" },, {
      headers: {
        "Set-Cookie": "connect.sid=msw-cookie;HttpOnly;Path=/",
      },
    });
  }),
];
reactnext.jsreact-querynext-authmsw

Answer 1

0

zerocho님의 프로필 이미지
zerocho
Instructor

에러메시지 그대로 middleware.ts 코드가 틀렸습니다.

ohsubin님의 프로필 이미지
ohsubin
Questioner

감사합니다!

ohsubin's profile image
ohsubin

asked

Ask a question