Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding PasswordLess login to sign in options #785

Merged

Conversation

JohnAllenTech
Copy link
Contributor

@JohnAllenTech JohnAllenTech commented Feb 28, 2024

✨ Codu Pull Request 💻

Codu Logo

Pull Request details

  • Added passwordless option to next-auth
  • Emails are sent through AWS SNS
  • New Environment vars added to sample.env will need to be populated before deploy
  • Added callback page letting user know email has been sent
  • Fixed auth error page light/dark mode

Any Breaking changes

  • Potentially env variables will need to be populated

Associated Screenshots

image

Sign in page dark mode

image

Sign in page light mode

image

Email callback page dark mode

image

Email callback page light mode

image

PasswordLess email

image

Email expired or already used page

Summary by CodeRabbit

  • New Features
    • Introduced passwordless sign-in functionality.
    • Added a new email input field and sign-in button with conditional display based on environment settings.
    • Enhanced email verification process with custom email templates including a passwordless login link.
    • Implemented theme mode constants export for improved theme toggling.
  • Documentation
    • Updated README with instructions for setting up Passwordless authentication locally.
  • Chores
    • Added email server configuration for AWS SNS in sample.env for passwordless email delivery.

@JohnAllenTech JohnAllenTech requested a review from a team as a code owner February 28, 2024 20:47
Copy link

vercel bot commented Feb 28, 2024

@sofeel is attempting to deploy a commit to the Codú Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Feb 28, 2024

Walkthrough

The project has been updated to include passwordless authentication via email, enhancing user experience by simplifying the login process. This involves adding a new email input field in the UI, configuring an email provider in the authentication server, and setting up an AWS SNS-based email server for delivery. Additionally, minor UI adjustments and the exportation of constants for theme toggling have been made to improve usability and customization.

Changes

Files Summary
app/(app)/get-started/_client.tsx Added userEmail state, email input, sign-in button, removed LockClosedIcon, minor UI changes.
server/auth.ts Added EmailProvider, sendPasswordLessEmail, updated authOptions and routes.
components/Theme/ThemeToggle/ThemeToggle.tsx Exported THEME_MODES constant.
sample.env Added AWS SNS email server configuration.
utils/createPasswordLessEmailTemplate.ts Added function for generating passwordless login email template.
README.md Added setup instructions for Passwordless authentication.
utils/sendEmail.ts Updated to export nodemailerSesTransporter for email sending.

"In a world of code and pixel sprites,
A rabbit hopped, bringing login delights.
🌟 No keys, no fuss, just a click,
Into your account, oh so quick!
With emails sent through the cloud so bright,
Our rabbit friend cheers, 'To passwordless nights!'
🐰✨"

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 6

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8272366 and e5e9344.
Files selected for processing (7)
  • app/(app)/auth/error/page.tsx (1 hunks)
  • app/(app)/auth/page.tsx (1 hunks)
  • app/(app)/get-started/_client.tsx (2 hunks)
  • components/Theme/ThemeToggle/ThemeToggle.tsx (1 hunks)
  • sample.env (1 hunks)
  • server/auth.ts (2 hunks)
  • utils/createPasswordLessEmailTemplate.ts (1 hunks)
Additional comments: 6
components/Theme/ThemeToggle/ThemeToggle.tsx (1)
  • 7-7: Exporting THEME_MODES as a constant is a good practice for ensuring consistency and reusability of theme mode values across the application. Well done on implementing this change.
app/(app)/auth/page.tsx (1)
  • 10-58: The implementation of the PostAuthPage component effectively utilizes the useTheme hook and the THEME_MODES constant for theme management. The conditional rendering of the Codú logo based on the current theme is a nice touch. Good job on these changes.
utils/createPasswordLessEmailTemplate.ts (4)
  • 1-1: The function createPasswordLessEmailTemplate is well-named, clearly indicating its purpose. However, it's important to ensure that the URL parameter is always validated before being passed to this function to prevent injection attacks.
  • 409-486: The conditional comments and the alternative rendering for Microsoft Outlook (<!--[if mso]>) and other clients (<!--[if !mso]><!-- -->) are correctly implemented. However, ensure thorough testing across different email clients to verify that the button renders as expected. Email client rendering can be notoriously inconsistent, and what works in one client may not work in another.
  • 1-683: The template makes good use of semantic HTML and accessibility considerations, such as alt text for images (not shown but assumed to be part of the template's images if any are used). However, ensure that any dynamic content, such as URLs or user-generated content, is properly sanitized to prevent XSS attacks. This is particularly important for email templates, which can be a vector for phishing attacks.
  • 1-683: The responsive design considerations using media queries (@media only screen and (max-width: 600px)) are a good practice for ensuring the email looks good on mobile devices. However, testing on actual devices and using email testing services like Litmus or Email on Acid can help catch any unforeseen issues.

@JohnAllenTech
Copy link
Contributor Author

8e6fc49d-d378-4905-b5b1-ec4755d7d9cb.mp4

New user signing up with only email

@JohnAllenTech
Copy link
Contributor Author

7d4b4c68-cc8a-43a5-bfdb-84e7199e50a2.mp4

Existing user signing in with passwordless

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e5e9344 and 0e18498.
Files selected for processing (1)
  • app/(app)/auth/error/page.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/(app)/auth/error/page.tsx

@@ -4,7 +4,7 @@ import { MoonIcon, SunIcon } from "@heroicons/react/20/solid";
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";

const THEME_MODES = {
export const THEME_MODES = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed this exported so I could use it to determine the coco log that should be displayed. ie black or white

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0e18498 and 5623b4b.
Files selected for processing (4)
  • app/(app)/auth/error/page.tsx (1 hunks)
  • app/(app)/auth/page.tsx (1 hunks)
  • app/(app)/get-started/_client.tsx (2 hunks)
  • server/auth.ts (2 hunks)
Files skipped from review as they are similar to previous changes (4)
  • app/(app)/auth/error/page.tsx
  • app/(app)/auth/page.tsx
  • app/(app)/get-started/_client.tsx
  • server/auth.ts


useEffect(() => {
// toLowerCase can maybe be removed. Just want to make sure its not TRUE in Prod
setIsAlpha(process.env.ALPHA?.toLowerCase() === "true");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the correct way to go about alpha stuff. Further guidance needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look later today and make a little guide so you can see how I was handling it.

Copy link
Contributor

@NiallJoeMaher NiallJoeMaher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small one, this can just be !!process.env.ALPHA because it will either have a value or not. That's usually all we check for elsewhere. No pattern on what that value is.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5623b4b and 0ffa731.
Files selected for processing (1)
  • app/(app)/get-started/_client.tsx (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/(app)/get-started/_client.tsx

@JohnAllenTech
Copy link
Contributor Author

Small one, this can just be !!process.env.ALPHA because it will either have a value or not. That's usually all we check for elsewhere. No pattern on what that value is.

Made that change. Let me know if anything else is needed. You will have to set up the email creds to get this to work also.

@NiallJoeMaher
Copy link
Contributor

I haven't forgot about this, just wanted to see if there was an alternative to all the SMTP credentials.

What about using key/secret key with AWS? Does that work?

Adapted from the nodmailer docs:

let nodemailer = require("nodemailer");
let aws = require("@aws-sdk/client-ses");
process.env.ACCESS_KEY_ID = "....";
process.env.SECRET_ACCESS_KEY = "....";
const ses = new aws.SES({
  apiVersion: "2010-12-01",
  region: "eu-west-1",
});
let transporter = nodemailer.createTransport({
  SES: { ses, aws },
});

@JohnAllenTech
Copy link
Contributor Author

I haven't forgot about this, just wanted to see if there was an alternative to all the SMTP credentials.

What about using key/secret key with AWS? Does that work?

Adapted from the nodmailer docs:

let nodemailer = require("nodemailer");
let aws = require("@aws-sdk/client-ses");
process.env.ACCESS_KEY_ID = "....";
process.env.SECRET_ACCESS_KEY = "....";
const ses = new aws.SES({
  apiVersion: "2010-12-01",
  region: "eu-west-1",
});
let transporter = nodemailer.createTransport({
  SES: { ses, aws },
});

I had tried this initially but ill try again tonight to be sure.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0ffa731 and 7b4f464.
Files selected for processing (4)
  • README.md (1 hunks)
  • app/(app)/get-started/_client.tsx (2 hunks)
  • server/auth.ts (2 hunks)
  • utils/sendEmail.ts (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • app/(app)/get-started/_client.tsx
  • server/auth.ts
Additional comments: 1
utils/sendEmail.ts (1)
  • 21-21: The export of nodemailerSesTransporter is correctly implemented, ensuring that the SES transporter is available for use elsewhere in the application. This change aligns with the PR's objective of enhancing email delivery capabilities.

@@ -36,7 +36,7 @@ const sendEmail = async (config: MailConfig) => {
const to = emailSchema.parse(recipient);
// send some mail
return new Promise((resolve, reject) => {
transporter.sendMail(
nodemailerSesTransporter.sendMail(
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sendEmail function utilizes the nodemailerSesTransporter to send emails. This function is well-structured and includes error handling and logging. However, consider abstracting the logging mechanism to a separate utility for better maintainability and to facilitate centralized logging in the future.

-          console.log("Error sending mail:", err);
+          // Consider using a centralized logging utility here
-          console.log(info.envelope);
-          console.log(info.messageId);
+          // Consider using a centralized logging utility here

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
nodemailerSesTransporter.sendMail(
nodemailerSesTransporter.sendMail(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create an issue for this comment and resolve the comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JohnAllenTech, I attempted to create the GitHub issue as requested, but encountered an issue with the GitHub CLI not recognizing @coderabbitai[bot] as a valid user for assignment. This is likely because bots cannot be directly assigned to issues in the same way that human users can.

However, the issue regarding abstracting the logging mechanism in the sendEmail function has been acknowledged. For tracking and implementation purposes, it would be beneficial for a member of the project team to manually create this issue on GitHub, ensuring it includes the context of the PR and the specific comment for clarity.

If there's anything more I can assist with or any other way you'd like to proceed, please let me know!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Create a GitHub issue for abstracting the logging mechanism in the sendEmail function
gh issue create --title "Abstract logging mechanism in sendEmail function" --body "The `sendEmail` function in `utils/sendEmail.ts` currently uses direct calls to `console.log` for logging errors and information. It's suggested to abstract these logging calls to a separate utility for better maintainability and to facilitate centralized logging in the future.

This improvement was suggested in the context of PR https://github.com/codu-code/codu/pull/785, specifically in this comment: https://github.com/codu-code/codu/pull/785#discussion_r1516922759

@JohnAllenTech requested the creation of this issue to track the suggested improvement." --assignee @coderabbitai[bot]

Length of output: 1013

Comment on lines +107 to +115
### Setting up Passwordless auth locally

In order to use Passwordless login locally you need to have a `ACCESS_KEY` and `SECRET_KEY` value.

Niall has written a [tutorial](https://www.codu.co/articles/sending-emails-with-aws-ses-and-nodemailer-in-node-js-xfuucrri) on how to send emails with AWS SES and shows how to get these values.

Check out the example .env file [here](./sample.env) to see how to populate these values

**Note: Currenly the AWS region of the SNS service is hardcoded to "eu-west-1" it may be necessary to change this if your SNS service is in a different region**
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The instructions for setting up Passwordless authentication locally are clear and helpful. Including a tutorial link for obtaining ACCESS_KEY and SECRET_KEY values is a thoughtful addition. However, the note about the hardcoded AWS region ("eu-west-1") is crucial. It's recommended to make this configuration more flexible by allowing it to be set via environment variables, enhancing the application's adaptability to different deployment environments.

- **Note: Currently the AWS region of the SNS service is hardcoded to "eu-west-1" it may be necessary to change this if your SNS service is in a different region**
+ **Note: Ensure to set the AWS region in your environment variables to match the region of your SNS service.**

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
### Setting up Passwordless auth locally
In order to use Passwordless login locally you need to have a `ACCESS_KEY` and `SECRET_KEY` value.
Niall has written a [tutorial](https://www.codu.co/articles/sending-emails-with-aws-ses-and-nodemailer-in-node-js-xfuucrri) on how to send emails with AWS SES and shows how to get these values.
Check out the example .env file [here](./sample.env) to see how to populate these values
**Note: Currenly the AWS region of the SNS service is hardcoded to "eu-west-1" it may be necessary to change this if your SNS service is in a different region**
### Setting up Passwordless auth locally
In order to use Passwordless login locally you need to have a `ACCESS_KEY` and `SECRET_KEY` value.
Niall has written a [tutorial](https://www.codu.co/articles/sending-emails-with-aws-ses-and-nodemailer-in-node-js-xfuucrri) on how to send emails with AWS SES and shows how to get these values.
Check out the example .env file [here](./sample.env) to see how to populate these values
**Note: Ensure to set the AWS region in your environment variables to match the region of your SNS service.**

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because we dont set the AWS region in env variables. Dont want to add it as an env var as that would make this PR a breaking change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JohnAllenTech, understood. To ensure clarity for future contributors and users, it might be beneficial to document the hardcoded AWS region limitation in the README or relevant documentation. This can serve as a temporary measure until a more flexible solution is implemented. Appreciate your attention to maintaining the stability of the PR.

@JohnAllenTech
Copy link
Contributor Author

@NiallJoeMaher should be good to go. No longer using the email creds. Now piggy backing on the current sendEmail function

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7b4f464 and 8d2bca5.
Files selected for processing (1)
  • sample.env (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • sample.env

Copy link

vercel bot commented Mar 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
codu ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 9, 2024 3:24pm

server/auth.ts Outdated
EmailProvider({
server: {},
sendVerificationRequest: sendPasswordLessEmail,
from: process.env.EMAIL_FROM,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a new variable? Could we use ADMIN_EMAIL. I think this is the very last thing (I kinda promise 😂).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point. Lemme change that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually even better I think its not needed. Will confirm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NiallJoeMaher sorted in #19379a8

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8d2bca5 and 19379a8.
Files selected for processing (1)
  • server/auth.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • server/auth.ts

Copy link
Contributor

@NiallJoeMaher NiallJoeMaher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🌮 I'm sure you'll be happy to get this merged 😹

@NiallJoeMaher NiallJoeMaher merged commit 690d751 into codu-code:develop Mar 9, 2024
4 checks passed
@JohnAllenTech
Copy link
Contributor Author

🌮 I'm sure you'll be happy to get this merged 😹

Best tasting taco so far alright

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants