... | @@ -148,3 +148,79 @@ NEXTAUTH_URL=http://localhost:3000/ |
... | @@ -148,3 +148,79 @@ NEXTAUTH_URL=http://localhost:3000/ |
|
By including these variables in the .env file, you ensure that Vercel can access and update them according to the required website address. Since we are using NextAuth.js for session management, which also relies on localhost, it is crucial to include these variables in the Vercel environment as well. You can accomplish this by navigating to the project's settings and adding them under the "environment variables" section.
|
|
By including these variables in the .env file, you ensure that Vercel can access and update them according to the required website address. Since we are using NextAuth.js for session management, which also relies on localhost, it is crucial to include these variables in the Vercel environment as well. You can accomplish this by navigating to the project's settings and adding them under the "environment variables" section.
|
|
|
|
|
|

|
|

|
|
|
|
|
|
|
|
## NextAuth.js
|
|
|
|
To add nextauth to your project, ensure that you´re in the nextjs-blog directory and run the following command:
|
|
|
|
```sh
|
|
|
|
npm install next-auth
|
|
|
|
```
|
|
|
|
|
|
|
|
### 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) |
|
|
|
\ No newline at end of file |