... | ... | @@ -118,75 +118,6 @@ By including these variables in the .env file, you ensure that Vercel can access |
|
|
|
|
|

|
|
|
|
|
|
### Add API route
|
|
|
Create a file called [...nextauth].js in pages/api/auth. This contains the dynamic route handler for NextAuth.js which will also contain all of your global NextAuth.js configurations.
|
|
|
|
|
|
```sh
|
|
|
import NextAuth from 'next-auth'
|
|
|
import CredentialsProvider from 'next-auth/providers/credentials'
|
|
|
import User from '../../../backend/endpoints/users/userModel'
|
|
|
import { compare } from 'bcryptjs'
|
|
|
import { connectMongo } from '../../../backend/utils/connectMongo'
|
|
|
|
|
|
export default NextAuth({
|
|
|
providers: [
|
|
|
CredentialsProvider({
|
|
|
name: 'Credentials',
|
|
|
credentials: {
|
|
|
email: { label: 'Email', type: 'email' },
|
|
|
password: { label: 'Password', type: 'password' },
|
|
|
isAdministrator: { label: 'IsAdministrator', type: 'text' }
|
|
|
},
|
|
|
authorize: async (credentials) => {
|
|
|
try {
|
|
|
await connectMongo()
|
|
|
const { email, password } = credentials
|
|
|
|
|
|
const user = await User.findOne({ email })
|
|
|
console.log("email: ", email)
|
|
|
console.log("user:", user)
|
|
|
|
|
|
if (!user) {
|
|
|
throw new Error('No user found')
|
|
|
}
|
|
|
|
|
|
const isValidPassword = await compare(password, user.password)
|
|
|
|
|
|
if (!isValidPassword) {
|
|
|
throw new Error('Invalid password')
|
|
|
}
|
|
|
|
|
|
return user
|
|
|
} catch (error) {
|
|
|
throw new Error('Authentication failed')
|
|
|
}
|
|
|
},
|
|
|
}),
|
|
|
],
|
|
|
callbacks: {
|
|
|
session: async (session) => {
|
|
|
const email = session?.token?.email
|
|
|
let user = await User.findOne({ email })
|
|
|
delete user.password
|
|
|
session.session.user =
|
|
|
{
|
|
|
id: user.id,
|
|
|
email: user.email,
|
|
|
firstName: user.firstName,
|
|
|
lastName: user.lastName,
|
|
|
birthdate: user.birthdate,
|
|
|
isAdministrator: user.isAdministrator,
|
|
|
isEventmanager: user.isEventmanager
|
|
|
}
|
|
|
return session
|
|
|
},
|
|
|
async jwt({ token }) {
|
|
|
return token
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
```
|
|
|
For more information, visit the [NextAuth.js documentation](https://next-auth.js.org/getting-started/example)
|
|
|
|
|
|
# Tests
|
|
|
To run the tests you have to execute
|
... | ... | |