-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d0af38
commit 96202a5
Showing
4 changed files
with
62 additions
and
69 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import NextAuth, { NextAuthOptions } from "next-auth"; | ||
import CredentialsProvider from "next-auth/providers/credentials"; | ||
import axios from "axios"; | ||
|
||
export const authOptions: NextAuthOptions = { | ||
providers: [ | ||
CredentialsProvider({ | ||
name: "Credentials", | ||
credentials: { | ||
email: { label: "Email", type: "text" }, | ||
password: { label: "Password", type: "password" }, | ||
}, | ||
async authorize(credentials) { | ||
const { email, password } = credentials ?? {}; | ||
|
||
try { | ||
const response = await axios.post( | ||
`${process.env.NEXT_PUBLIC_API_URL}/login`, | ||
{ email, password } | ||
); | ||
|
||
const { token, user } = response.data; | ||
|
||
return { id: user.id, name: user.name, email: user.email, token }; | ||
} catch (error) { | ||
console.error("Login failed:", error); | ||
return null; | ||
} | ||
}, | ||
}), | ||
], | ||
session: { | ||
strategy: "jwt", | ||
}, | ||
callbacks: { | ||
async jwt({ token, user }) { | ||
if (user) { | ||
token.id = user.id; | ||
token.name = user.name; | ||
token.email = user.email; | ||
token.accessToken = user.token; | ||
} | ||
return token; | ||
}, | ||
async session({ session, token }) { | ||
session.user.id = token.id; | ||
session.user.name = token.name; | ||
session.user.email = token.email; | ||
session.accessToken = token.accessToken as string; | ||
return session; | ||
}, | ||
}, | ||
secret: process.env.NEXTAUTH_SECRET, | ||
}; | ||
|
||
const handler = NextAuth(authOptions); | ||
|
||
export { handler as GET, handler as POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters