
NextAuth와 middleware (페이지 접근 권한 제어)
auth.ts
import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials";
export const {
handlers: {GET, POST},
auth : ...,
signIn,
} = NextAuth({
pages: {
signIn : '/i/flow/login',
newUser: '/i/flow/singup',
},
providers: [
CredentialsProvider({
async authroize(credentails)
const authResponse= await fetch("${process.env.NEXT_PUIBLIC_BASE_URL}/users/login",{
method: "POST",
headers: {
"Content-Type" : "application/json",
},
body : JSON.stringify({
id: credentials.username,
password: credentails.password
});
}
if(!authResponse.ok) {
return null
}
const user = await authResponse.json()
return user
})
]
})
middleware.ts
export {auth as middleware} from "./auth"
export const config = {
matcher: ['/compose/tweet', '/home', '/explore', '/messages', '/search'],
}
src/app/api/auth/[...nextauth]/route.ts
import {GET, POST} from @/auth