diff --git a/packages/auth-express/readme.md b/packages/auth-express/readme.md index b65fcd002..0b5135b6e 100644 --- a/packages/auth-express/readme.md +++ b/packages/auth-express/readme.md @@ -229,19 +229,31 @@ app.use(oAuthRouter); ### Custom UI: Magic Link - `routerPath: string`, required, This is the path relative to the `baseUrl` configured when creating the `ExpressAuth` object. This path is used to build the URL for the callback path configured by the router factory. -- `callback: (express.RouteHandler | express.ErrorHandler)[]`, required, Once the authentication flow completes, this callback will be called, and you must return a terminating Express route handler here. Typically, you'll redirect to elsewhere in your app based on `req.isSignUp`. - `failureUrl: string`, required, URL to redirect to in case of a failure during the Magic Link process. +- `callback: (express.RouteHandler | express.ErrorHandler)[]`, required, Once the authentication flow completes, this callback will be called, and you must return a terminating Express route handler here. Typically, you'll redirect to elsewhere in your app based on `req.isSignUp`. +- `send: (express.RouteHandler | express.ErrorHandler)[]`, this route handler stack will be called when a request is made to send a magic link to a registered email address. Typically, you'll return some HTML or a redirect here that indicates that the user should check their email. +- `signup: (express.RouteHandler | express.ErrorHandler)[]`, this route handler stack will be called when a request is made to register an email address. Typically, you'll return some HTML or a redirect here that indicates that the user should check their email. ```ts const magicLinkRouter = auth.createMagicLinkRouter( "/auth/magic-link", + "/login-failure", { callback: [ (req: expressAuth.CallbackRequest, res) => { res.redirect("/"); }, ], - failureUrl: "/login-failure" + send: [ + (req, res) => { + res.redirect("/check-email.html"); + }, + ], + signUp: [ + (req, res) => { + res.redirect("/check-email.html"); + } + ] } ); diff --git a/packages/auth-express/src/index.ts b/packages/auth-express/src/index.ts index 1ba80e8ed..268774c45 100644 --- a/packages/auth-express/src/index.ts +++ b/packages/auth-express/src/index.ts @@ -184,13 +184,8 @@ export class ExpressAuth { createMagicLinkRouter = ( routerPath: string, - { - callback, - failureUrl, - }: { - callback: RouterStack; - failureUrl: string; - } + failurePath: string, + stacks: Record ) => { const router = Router(); @@ -198,17 +193,19 @@ export class ExpressAuth { "/send", this.magicLink.send( new URL(`${routerPath}/callback`, this.options.baseUrl).toString(), - failureUrl - ) + new URL(failurePath, this.options.baseUrl).toString() + ), + ...stacks.send ); router.post( "/signup", this.magicLink.signUp( new URL(`${routerPath}/callback`, this.options.baseUrl).toString(), - failureUrl - ) + new URL(failurePath, this.options.baseUrl).toString() + ), + ...stacks.signUp ); - router.get("/callback", this.magicLink.callback, ...callback); + router.get("/callback", this.magicLink.callback, ...stacks.callback); return Router().use(routerPath, router); }; @@ -638,6 +635,13 @@ export class ExpressAuth { ["email"], "email missing from request body" ); + console.log( + `magic link signup: ${JSON.stringify( + { callbackUrl, failureUrl, email }, + null, + 2 + )}` + ); const { verifier } = await ( await this.core ).signupWithMagicLink(email, callbackUrl, failureUrl); @@ -659,6 +663,13 @@ export class ExpressAuth { ["email"], "email missing from request body" ); + console.log( + `magic link send: ${JSON.stringify( + { callbackUrl, failureUrl, email }, + null, + 2 + )}` + ); const { verifier } = await ( await this.core ).signinWithMagicLink(email, callbackUrl, failureUrl);