diff --git a/.github/ISSUE_TEMPLATE/2_bug_provider.yml b/.github/ISSUE_TEMPLATE/2_bug_provider.yml index 630d8af92c..fa87b24a59 100644 --- a/.github/ISSUE_TEMPLATE/2_bug_provider.yml +++ b/.github/ISSUE_TEMPLATE/2_bug_provider.yml @@ -61,6 +61,7 @@ body: - "Kinde" - "Line" - "LinkedIn" + - "Loops" - "Mailchimp" - "Mail.ru" - "Mastodon" diff --git a/docs/pages/getting-started/authentication/email.mdx b/docs/pages/getting-started/authentication/email.mdx index 81731d714a..ce51246686 100644 --- a/docs/pages/getting-started/authentication/email.mdx +++ b/docs/pages/getting-started/authentication/email.mdx @@ -72,6 +72,14 @@ This login mechanism starts by the user providing their email address at the log >
Postmark
+ + + +
Loops
+ + +### Loops Setup + + + +### Database Adapter + +Please make sure you've [setup a database adapter](/getting-started/database), as mentioned earlier, +a database is required for passwordless login to work as verification tokens need to be stored. + +### Create your Transactional Email Template on Loops + +Loops have provided a super handy [guide](https://loops.so/docs/transactional/guide) to help you get started with creating your transactional email template. +This provider only passes one data varaiable into the template, `url` which is the magic link to sign in. This is case sensitive, so make sure you use `url` in your template.
+On the last page of Template creation, you'll need to copy the `TRANSACTIONAL ID`. If you skipped this step, don't worry, you can get this at any from the Template edit page. + +### Create an API Key on Loops + +You'll need to create an API key to authenticate with Loops. This key should be kept secret and not shared with anyone. +You can Generate a key by going to the [API Settings Page](https://app.loops.so/settings?page=api) and clicking Generate. +You should name the key something that makes sense to you, like "Auth.js". + +### Setup Environment Variables + +To implement Loops, you need to set up the following environment variables. You should have these from the previous steps. + +```bash filename=".env" +AUTH_LOOPS_KEY=abc123 +AUTH_LOOPS_TRANSACTIONAL_ID=def456 +``` + +### Setup Provider + +Let's enable `Loops` as a sign-in option for our Auth.js configuration. You'll have to import the `Loops` provider from the package and pass it to the providers array we set up earlier in the Auth.js config file: + + + + +```ts filename="./auth.ts" +import NextAuth from "next-auth" +import Loops from "next-auth/providers/loops" + +export const { handlers, auth, signIn, signOut } = NextAuth({ + providers: [ + Loops({ + apiKey: process.env.AUTH_LOOPS_KEY, + transactionalId: process.env.AUTH_LOOPS_TRANSACTIONAL_ID, + }), + ], +}) +``` + + + + +```ts filename="./src/auth.ts" +import SvelteKitAuth from "@auth/sveltekit" +import Loops from "@auth/sveltekit/providers/loops" +import { + AUTH_LOOPS_KEY, + AUTH_LOOPS_TRANSACTIONAL_ID, +} from "$env/static/private" + +export const { handle, signIn, signOut } = SvelteKitAuth({ + providers: [ + Loops({ + apiKey: AUTH_LOOPS_KEY, + transactionalId: AUTH_LOOPS_TRANSACTIONAL_ID, + }), + ], +}) +``` + +```ts filename="./src/hooks.server.ts" +export { handle } from "./auth" +``` + + + + +### Add Signin Button + +Next, we add a signin button somewhere in your application like the Navbar. This will send an email to the user containing the magic link to sign in. + + + + +```tsx filename="./components/sign-in.tsx" +import { signIn } from "../../auth.ts" + +export function SignIn() { + return ( +
{ + "use server" + await signIn("loops", formData) + }} + > + + +
+ ) +} +``` + +
+ + +```ts filename="src/routes/+page.svelte" + + + +
+ +
+ +``` + +
+
+ +### Signin + +Start your application, click on the signin button we just added, and you should see Auth.js built-in sign in page with the option to sign in with your email. +A user can enter their email, click "Sign in with Loops", and receive their beautifully formatted signin email. +Clicking on the link in the email will redirect the user to your application, landing already authenticated! + +
- + diff --git a/docs/pages/getting-started/providers/loops.mdx b/docs/pages/getting-started/providers/loops.mdx new file mode 100644 index 0000000000..77a5672f38 --- /dev/null +++ b/docs/pages/getting-started/providers/loops.mdx @@ -0,0 +1,103 @@ +import { Callout } from "nextra/components" +import { Code } from "@/components/Code" + + + +# Loops Provider + +## Overview + +The Loops provider uses email to send "magic links" that contain URLs with verification tokens can be used to sign in. + +Adding support for signing in via email in addition to one or more OAuth services provides a way for users to sign in if they lose access to their OAuth account (e.g. if it is locked or deleted). + +The Loops provider can be used in conjunction with (or instead of) one or more OAuth providers. + +## How it works + +On initial sign in, a **Verification Token** is sent to the email address provided. By default this token is valid for 24 hours. If the Verification Token is used within that time (i.e. by clicking on the link in the email) an account is created for the user and they are signed in. + +If someone provides the email address of an _existing account_ when signing in, an email is sent and they are signed into the account associated with that email address when they follow the link in the email. + + + The Loops provider can be used with both JSON Web Token and database managed + sessions, however **you must configure a database** to use it. It is not + possible to enable email sign in without using a database. + + +## Configuration + +### Add and Verify your Domain on Loops + +First, you'll need to have completed the steps covered in the ['Start here'](https://loops.so/docs/start-here) Loops documentation. +The main thing required is to [set up your domain records](https://loops.so/docs/start-here#1-set-up-your-domain-records). + +### Generate an API Key + +Next, you will have to generate an API key in the [Loops Dashboard](https://loops.so/api-keys). You can save this API key as the `AUTH_LOOPS_KEY` environment variable. + +```sh +AUTH_LOOPS_KEY=abc +``` + +### Create a Transactional Email Template on Loops + +The easiest way to achieve this is using the [Loops email editor](https://loops.so/docs/creating-emails/editor) to create a transactional email template. +If you're new to Loops, you can find rich documentation [here](https://loops.so/docs/transactional/guide). + +
+Copy the Transactional ID value from the last page of the template creation +process, and save this as the `AUTH_LOOPS_TRANSACTIONAL_ID` environment +variable. If you're following these steps, you should now have two environment +variables set up for Loops. + +```sh +AUTH_LOOPS_KEY=abc +AUTH_LOOPS_TRANSACTIONAL_ID=def +``` + + + When creating your email template, make sure to include the `url` variable in + the template. This is the URL that will sent to the user, allowing them to + signin. + + + + +### Configure AuthJS with the Loops Provider +```ts filename="./auth.ts" +import NextAuth from "next-auth" +import Loops from "next-auth/providers/loops" + +export const { handlers, auth, signIn, signOut } = NextAuth({ + adapter: ..., // database adapter of your choosing + providers: [ + Loops({ + apiKey: process.env.AUTH_LOOPS_KEY, + transactionalId: process.env.AUTH_LOOPS_TRANSACTIONAL_ID, + }), + ], +}) +``` + + + +### Configure AuthJS with the Loops Provider +```ts filename="./src/auth.ts" +import { SvelteKitAuth } from "@auth/sveltekit" +import Loops from "@auth/sveltekit/providers/loops" +import { AUTH_LOOPS_KEY, AUTH_LOOPS_TRANSACTIONAL_ID } from "@env/static/private" + +export const { handle, signIn, signOut } = SvelteKitAuth({ + adapter: ..., // database adapter of your choosing + providers: [ + Loops({ + apiKey: AUTH_LOOPS_KEY, + transactionalId: AUTH_LOOPS_TRANSACTIONAL_ID, + }), + ], +}) +``` + + + diff --git a/docs/public/img/providers/loops.svg b/docs/public/img/providers/loops.svg new file mode 100644 index 0000000000..8f831f634d --- /dev/null +++ b/docs/public/img/providers/loops.svg @@ -0,0 +1,3 @@ + + + diff --git a/packages/core/src/providers/email.ts b/packages/core/src/providers/email.ts index 6690cf2d1e..cbd1e79fd1 100644 --- a/packages/core/src/providers/email.ts +++ b/packages/core/src/providers/email.ts @@ -26,21 +26,25 @@ export default function Email(config: NodemailerUserConfig): NodemailerConfig { // when started working on https://github.com/nextauthjs/next-auth/discussions/1465 export type EmailProviderType = "email" +export type EmailProviderSendVerificationRequestParams = { + identifier: string + url: string + expires: Date + provider: EmailConfig + token: string + theme: Theme + request: Request +} + export interface EmailConfig extends CommonProviderOptions { id: string type: EmailProviderType name: string from?: string maxAge?: number - sendVerificationRequest: (params: { - identifier: string - url: string - expires: Date - provider: EmailConfig - token: string - theme: Theme - request: Request - }) => Awaitable + sendVerificationRequest: ( + params: EmailProviderSendVerificationRequestParams + ) => Awaitable /** Used to hash the verification token. */ secret?: string /** Used with HTTP-based email providers. */ diff --git a/packages/core/src/providers/loops.ts b/packages/core/src/providers/loops.ts new file mode 100644 index 0000000000..dd76fa1eb8 --- /dev/null +++ b/packages/core/src/providers/loops.ts @@ -0,0 +1,79 @@ +/** + *
+ * Built-in Loops integration. + * + * + * + *
+ * + * @module providers/loops + */ + +import type { EmailConfig, EmailUserConfig } from "./email.js" + +export type LoopsUserConfig = Omit, "options" | "type"> + +export interface LoopsConfig + extends Omit { + id: string + apiKey: string + transactionalId: string + sendVerificationRequest: (params: Params) => Promise + options: LoopsUserConfig +} + +type Params = Parameters[0] & { + provider: LoopsConfig +} + +/** + * + * @param config + * @returns LoopsConfig + * @requires LoopsUserConfig + * @example + * ```ts + * Loops({ + * apiKey: process.env.AUTH_LOOPS_KEY, + * transactionalId: process.env.AUTH_LOOPS_TRANSACTIONAL_ID, + * }) + * ``` + * + * @typedef LoopsUserConfig + */ + +export default function Loops(config: LoopsUserConfig): LoopsConfig { + return { + id: "loops", + apiKey: "", + type: "email", + name: "Loops", + from: "Auth.js ", + maxAge: 24 * 60 * 60, + transactionalId: config.transactionalId || "", + async sendVerificationRequest(params: Params) { + const { identifier: to, provider, url } = params + if (!provider.apiKey || !provider.transactionalId) + throw new TypeError("Missing Loops API Key or TransactionalId") + + const res = await fetch("https://app.loops.so/api/v1/transactional", { + method: "POST", + headers: { + Authorization: `Bearer ${provider.apiKey}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + transactionalId: provider.transactionalId, + email: to, + dataVariables: { + url: url, + }, + }), + }) + if (!res.ok) { + throw new Error("Loops Send Error: " + JSON.stringify(await res.json())) + } + }, + options: config, + } +}