Skip to content

Commit

Permalink
Updates based on testing magic links
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Mar 22, 2024
1 parent 8866826 commit 8c6865c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
16 changes: 14 additions & 2 deletions packages/auth-express/readme.md
Expand Up @@ -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");
}
]
}
);

Expand Down
35 changes: 23 additions & 12 deletions packages/auth-express/src/index.ts
Expand Up @@ -184,31 +184,28 @@ export class ExpressAuth {

createMagicLinkRouter = (
routerPath: string,
{
callback,
failureUrl,
}: {
callback: RouterStack;
failureUrl: string;
}
failurePath: string,
stacks: Record<keyof typeof this.magicLink, RouterStack>
) => {
const router = Router();

router.post(
"/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);
};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 8c6865c

Please sign in to comment.