We are implementing Firebase email link login (https://firebase.google.com/docs/auth/web/email-link-auth) and I'd like to customize the mail sent, but it isn't showing up in the list of templates in Firebase. Is it possible to customize this email?
1 Answers
Firebase does not provide a template in the Authentication category by default.
If you would like to send a custom email with the sign in link you will have to use a custom SMTP server and email account. I have created a guide in the past that will work for you here.
You would have to connect to that email account through a library such as nodemailer. Assuming you are using the admin sdk, you should generate the sign in URL through a method such as #generateSignInWithEmailLink. Here is a link to the docs page.
// Admin SDK API to generate the sign in with email link.
const useremail = 'user@example.com';
getAuth()
.generateSignInWithEmailLink(useremail, actionCodeSettings)
.then((link) => {
// Construct sign-in with email link template, embed the link and
// send using custom SMTP server - this is where you would implement nodemailer or other package
return sendSignInEmail(useremail, displayName, link);
})
.catch((error) => {
// Some error occurred.
});
In the past when I've gone through this process it's important to note that the #sendSignInEmail method is just a placeholder Firebase has used for the example. This wasn't very clear to me initially.
Something to note is that you should not be using the admin sdk in the front end. If you have access to Cloud Functions, create a function that sends this email triggered by a https request from your front end. Ensure that the cloud function checks the authentication of any caller, as by default they are insecure. You could achieve this by passing a user token through as a http variable that is verified inside the Cloud Function using a method such as #verifyIdToken. See the documentation for more details.
- 2,031
- 2
- 8
- 29
-
1Thanks for the detailed explanation, a little disappointing that they don't provide a less complex way to customize this email. – Jake Hall Aug 11 '23 at 15:25
-
1No problem. I felt the same way when I was doing it, just how it is unfortunately. – Joe Moore Aug 11 '23 at 16:12