Reputation:
I want to use next-auth with the email provider (passwordless signin) for a login page, and use MongoDB to store a new user etc. As for the email sending I would like to use AWS SES.
Current status: I have implemented a boilerplate next app with create-next-app and installed mongodb, next-auth and the email-provider. I have added a lib/mongodb.js file to connect to my database and a [...nextauth.js] file to setup the next-auth functionalities. I have added the email provider and also setup AWS SES with 2 verified identities.
lib/mongodb.js
// This approach is taken from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb
import { MongoClient } from 'mongodb';
const uri = process.env.MONGODB_URI;
const options = {
useUnifiedTopology: true,
useNewUrlParser: true,
};
let client;
let clientPromise;
if (!process.env.MONGODB_URI) {
throw new Error('Please add your Mongo URI to .env.local');
}
if (process.env.NODE_ENV === 'development') {
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options);
global._mongoClientPromise = client.connect();
}
clientPromise = global._mongoClientPromise;
} else {
client = new MongoClient(uri, options);
clientPromise = client.connect();
}
export default clientPromise;
[...nextauth.js]
import NextAuth from 'next-auth';
import { MongoDBAdapter } from '@next-auth/mongodb-adapter';
import EmailProvider from 'next-auth/providers/email';
import clientPromise from '../../../lib/mongodb';
const THIRTY_DAYS = 30 * 24 * 60 * 60;
const THIRTY_MINUTES = 30 * 60;
export default NextAuth({
secret: process.env.SECRET,
session: {
strategy: 'jwt',
maxAge: THIRTY_DAYS,
updateAge: THIRTY_MINUTES,
},
adapter: MongoDBAdapter(clientPromise),
providers: [
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
],
});
Problem: When I npm run dev my project and go to the default sigin path, the e-mail login form is shown as expected. But when I enter an email address and click on "signin with email" nothing happens. I see a connection in my MongoDB Atlas database, but nothing is created and no email gets sent. The page reloads and the network tab in the dev-tools shows a 302 redirection.
I would just like to know which parts dont work, since no error gets thrown and I am not experienced at all. Most tutorials or code snippets either use oauth, as postgres db or prisma. But since I am using mongodb and no oauth it is hard to find a lot of sourcecode and explanations.
I wondered: AWS SES seems to work fine on its own, I sent an email from the AWS console, do I need to do more than reference my SMTP credentials in an .env file and import them like that? For example setup nodemailer? It was mentioned in the next-auth docs but not as obligatory and its own documenation says its not necessarily needed for simple email sendings with SES.
Sould I try a different email sending tool, like sendgrid? Should I try a different db?
Most mistakes I made so far in programming were pretty damn stupid, so I would also not be surprised if I am overlooking something obvious here. Thanks in advance!
Upvotes: 0
Views: 1001
Reputation: 568
I had a similar problem. I couldn't get AWS SES configured as you have it. I had to provide a single connect string for AWS.
This did not work
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
}),
This did
EmailProvider({
server: `smtp://${process.env.EMAIL_SERVER_USER}:${process.env.EMAIL_SERVER_PASSWORD}@${process.env.EMAIL_SERVER_HOST}`,
from: process.env.EMAIL_FROM,
}),
If I included the port (25) in that connect string it would fail to connect.
Upvotes: 1