Skip to main content
View on YouTube to comment and like!

Syntax Video

Next.js + NextAuth - Email Magic Link Login | Local Testing with Mailhog

Chapter 1 00:00

Transcript

if you've ever been testing sending emails in development like testing magic Link login or different things like that you've probably dealt with an inbox that looks like this so this is me just testing my email integration and I'm using next off so if I go and sign in here this is actually sending a real email so it's hitting an SMTP server that SMTP server is then going to receive the mail and then send it to my actual email account and then I can head over here to my Gmail and actually get the email and I bet if you've ever done any sort of testing with email this is is probably the workflow that you have of course we can click the link and now we're logged in but imagine having to do this over and over and sometimes the SMTP server is slow sometimes your email client is slow to load well the answer to this is local email testing and in this video I'm going to show you how I set up mail hog so mail hog is essentially a local SMTP server and once you have it running locally any emails that are sent to it are just going to show up in this inbox here there's no waiting around every single email that gets sent to it is just instantly visible right here so if I update my environment variables to point to this

local mail server instead now whenever I log in that email will be instantly available over here inside of mail hog and then I can just instantly sign in so this is also great if you're trying to test like email layouts and different stuff like that you don't have to wait for this hop to the SMTP server and then back to the client you can instantly log in and start developing so in this video I'm going to show you how I got all of this set up to get email login with nextjs and next off with a nice local Dev experience so that sounds good to you let's dive in my name is CJ welcome to syntax [Music] so for this example I actually modified the next start repo that I created a few months back this essentially just shows you how to get up and going with nextjs and various other packages to get a nice full stack development flow and this setup actually uses login with Google oaf but I essentially took this app as a base and then added the email provider instead and so if you take a look at next off they have a lot of different providers of various ooth but they also

have the email provider and this provider is allows you to do magic Link login that is there's no password associated with the account whenever someone tries to log in it sends them an email they click on it and then they are now logged in and you can see here there's a few different ways to integrate it the first one is to use HTTP so there's a lot of email provider apis out there like s Grid or mail gun or AWS sees and essentially what you can do is you can add the configuration here and then you add your own sin verification request function and then inside of there you can hit whatever API you want so in this example they're calling the Sy grid API you you can call any other API that would allow you to send emails for my example we used SMTP credentials so pretty much any email provider that you sign up with even if they have an API they pretty much always make SMTP credentials available to you and then you can use those credentials to send emails directly so to get this running with nextto you do need to install node mailer which is essentially a node.js package that can send emails over SMTP and then from there you can update your environment variables so here you can see I have a few different

setup I have my email from so this will be who the email is coming from from the server host when you're running in production this will be like send Grid or mail gun or resend or any of the other various uh mail hosts then we set up our Port here we're using mail hog which runs on 1025 by default so that's the port and then with mail hog you can actually just pass in anything you want for the username and password because it's just running locally in this nextjs starter project also have a way of working with environment variables in a type Safeway and here you can see I have all of those environment variables set up and then whenever we set up our config we can actually use those environment variables so here you can see I'm bringing in that EnV and then right here under providers I'm bringing in email provider which just comes from next o providers you can see I pass in all of my various credentials and so now next o is ready to send emails we just need an email server so there's a couple of different ways to set up mhog you can see in their docs you can install it with Brew if you're on a Mac they also have instructions for installing on Linux or you could even install it as a goang module but my favorite way to do

it is just to do it with Docker so my nextjs starter project comes with a Docker compose that spins up a postgress database for you so this is where all the data is stored where all the users are stored but because we already have this Docker compose we can just add a new section to spin up our mail hog image so you can see here I just call it mathog I specify the mail hog image which is hosted on dockerhub and then I specify my ports here now the default SMTP Port is 1025 and then the default HTTP Port that is where you're going to visit the dashboard to actually see your emails that's 8025 you can see here I'm also passing these in as environment variables because if for whatever reason either of these two ports were taken up on your machine you could put them on whatever ports you want this is just essentially going to map the port on your computer to the port running inside of the container and so in your EnV you're just going to want to make sure that you have those two Port variables set here from there just make sure that you have Docker running now I'm on a Mac I have Docker desktop running but there are alternatives as well I actually made a video a while back on how to use Docker for developers and I I talked about a lot of the Alternatives there so definitely check that out if you don't

want to use Docker desktop but with Docker desktop running we can now run docker pose up it's going to spin up our postgress database but as you can see it also spins up mail hog and from there we can actually visit it in the browser and there we go we have our mail hog dashboard from there because our email provider that we set up here in next off is pointed at our mail hog server all of our emails are going to show up inside of the mail hog dashboard so now if I try to sign in that is going to send the email but it went through mail hog so mail hog essentially just captures the email it doesn't go anywhere after that and if you head to the mail hog dashboard you can actually see the email so here we can see who it got sent to and then when I click sign in that's going to redirect me back to Local Host and I can now use the app as this user so this is awesome but one of the other cool things about this is mhog just traps every single email that gets sent to it so you can set your email as anything you want so let's actually just sign out and I'll try to log in as a different email so I'll just do my personal email like this but that also shows up in the inbox over here as well so this is really great if you're doing like multi-user testing or maybe you're checking different types of roles but

now we can see there's the email to my personal account I can log in and there we go now you might be wondering how we're able to actually see the user's Avatar there because the default email integration sets the image to nothing so we would have just seen like a fallback image but I actually added a custom integration with the gravitar API so this API has been around for a long time essentially anybody that signs up on gravitar with their email can set their preferred Avatar their preferred display name and then any other service that uses the gravitar API can actually pull that data in and so I've set up gravitar accounts for all of these emails that I've been logging in with let's actually try another one like that and we'll go ahead and log in there it is sign in and you can see it has my nice little coding carton image but essentially each one of these accounts has signed up on gravitar filled out their profile and so now they have a default Avatar and then also in the profile you'll see that they have their display name set as well so to get this set up there's a couple of different things you could do so next

off has callbacks that you can tap into they also have events and so here inside of events you have access to sign in whenever a user signs in I believe there's also create user and then also like Link account so you could tap into these various events reach out to the gravitar API and then update the user in the database but I wanted a more direct integration essentially whenever the user gets created I want that image and display name to be there automatically so I created a custom database adapter so here you can see I have my drizzle email adapter and essentially I just wrapped the drizzle adapter and then overrode the create user method so if we take a look at this file I brought in the drizzle adapter and then I did some typescript Fu here to get the correct types but essentially I want to create my own function that behaves exactly as the drizzle adapter so I did that here we have the database that gets passed in and potentially the schema which is optional then inside this function I create the real drizzle adapter so this is essentially what you would pass in if you were just going to use this adapter directly and then from there I return

all of the properties of the base adapter but then just override any functions that I want to and in this case I want to override the create user function so this method gets called anytime a user is about to get put into the database and what I do before putting them into the database is make a call out to the gravitar API so the way their API works is you take a sha 256 hash of the email and then you can make an API request with that hash and if that user has filled out their gravitar profile you can get back their Avatar URL and display name and there's a few other properties you could get as well so and so essentially if that user does exist on gravitar I set their image I set display name and then if they don't I just set some fallback information because even with the default adapter the name actually just is null cuz it's not set to anything so if they weren't found on gravitar I actually just take the part of their email before the at sign and set that as their name and then set their image to just a default Avatar so UI avatars is another service where you can essentially pass in a name you can also customize like the colors and

everything else this will just give you a default Avatar you've probably seen some of these before that just have like the capital letters that correspond on to the user's name so in this case because we pass in their email name it's going to pick the letters off of that and then have just a default image there now there are probably other apis you could tap into you could probably check like if it's a Gmail address then maybe you make a request to a Google API to get their actual profile and then get their information that way but this is just one way to basically customize the data before it gets put into the database now there's one more customization you can do here with next off which is to customize the email that actually gets sent to the user whenever they're logging in and so here in the options that we passed to email provider we can pass in send verification request and then write our custom code inside of here if you take a look at the next off documentation they actually show you what the built-in function is doing and essentially it's creating a transport with node mailer which is that node package that allows us to send emails over SMTP but they have some built-in functions called text and HTML which

essentially just style those emails so if you implement this method yourself you could create a completely custom email that maybe has your brand logo for a bit more customization also you could actually customize the login flow right so by default this just sends a link that the user needs to click on but what if you wanted to implement like a one-time six-digit sign-in code instead of having the user click a link you could Implement that with this so another method you can override is called generate verification token and so inside of here instead of generating a big long string like is the default with login with email you could generate a random six-digit or six character code that code will then be accessible inside of here and then you could craft your email to show that code instead now from there you'd actually need to build a custom page where the user enters in their signin code because that's doesn't come built in with next off but next off also has a rest API so there's essentially endpoints you could hit if you wanted to pass in that custom verification token from your own UI now

another fun thing to pair this with would be react email so react email essentially gives you components that you can use that are just jsx they look like react but they've been tested to render nicely across a lot of email clients because if you haven't worked with creating emails from scratch with HTML it's actually a pain to make sure they look good across all types of email clients and devices and so there are packages like react email which gives you all kinds of cool components like an image component and a button component and they also have templates so you can see here they've recreated a lot of these transactional emails that you might have gotten from various services but if you click on them they actually show you the code you would need with react email to generate the HTML that you could then send inside of an email and so if you're going to create custom emails I highly recommend go with something like react email there's also mjml there's probably a lot of others if you know of any others let us know down in the comments as well so that's it for this video all the code that I showed you today is going to be available on the email- login branch on that next starter template link is in the description if you have any questions

throw them down in the comments if you're going to use mhog in your app let me know and if you have any other tips for other tools or various things for working with emails in react I'd love to know that as well all right I'll see you in the next one [Music]

Quote it with credit and a link. Quoting Syntax