Skip to content

Commit 431396c

Browse files
docs: jwt signing example (#268)
* add jwt example * update code snippet to match recommendation * Update advanced/user-auth/jwt.mdx Co-authored-by: Mayank Shouche <[email protected]> --------- Co-authored-by: Mayank Shouche <[email protected]>
1 parent ecf74f5 commit 431396c

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

advanced/user-auth/jwt.mdx

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ If you don’t have a dashboard, or if you want to keep your dashboard and docs
1515
Create a login flow that does the following:
1616
- Authenticate the user
1717
- Create a JWT containing the authenticated user's info in the [UserInfo](./sending-data) format
18-
- Sign the JWT with the secret
18+
- Sign the JWT with the secret, using the ES256 algorithm
1919
- Create a redirect URL back to your docs, including the JWT as the hash
2020
</Step>
2121
<Step title="Configure your User Auth settings">
@@ -25,11 +25,48 @@ If you don’t have a dashboard, or if you want to keep your dashboard and docs
2525

2626
## Example
2727

28-
I want to set up authentication for my docs hosted at `docs.foo.com`. I want my docs to be completely separate from my dashboard (or I don’t have a dashboard at all).
28+
I want to set up authentication for my docs hosted at `docs.foo.com`. I want my docs
29+
to be completely separate from my dashboard (or I don’t have a dashboard at all).
2930

30-
To set up authentication with Mintlify, I go to my Mintlify dashboard and generate a JWT secret. I create a web URL `https://foo.com/docs-login` that initiates a login flow for my users. At the end of this login flow, once I have verified the identity of the user, I create a JWT containing the user’s custom data according to Mintlify’s specification. I sign this JWT with my Mintlify secret, create a redirect URL of the form `https://docs.foo.com#{SIGNED_JWT}`, and redirect the user.
31+
To set up authentication with Mintlify, I go to my Mintlify dashboard and generate a
32+
JWT secret. I create a web URL `https://foo.com/docs-login` that initiates a login flow
33+
for my users. At the end of this login flow, once I have verified the identity of the user,
34+
I create a JWT containing the user’s custom data according to Mintlify’s specification.
35+
I use a JWT library to sign this JWT with my Mintlify secret, create a redirect URL of the
36+
form `https://docs.foo.com#{SIGNED_JWT}`, and redirect the user.
3137

32-
I then go to the Mintlify dashboard settings and enter `https://foo.com/docs-login` for the Login URL field.
38+
I then go to the Mintlify dashboard settings and enter `https://foo.com/docs-login` for the
39+
Login URL field.
40+
41+
Here's what the code might look like:
42+
43+
```ts
44+
import * as jose from 'jose';
45+
import { Request, Response } from 'express';
46+
47+
const TWO_WEEKS_IN_MS = 1000 * 60 * 60 * 24 * 7 * 2;
48+
49+
const signingKey = await jose.importPKCS8(process.env.MINTLIFY_PRIVATE_KEY, 'ES256');
50+
51+
export async function handleRequest(req: Request, res: Response) {
52+
const userInfo = {
53+
expiresAt: Math.floor((Date.now() + TWO_WEEKS_IN_MS) / 1000),
54+
groups: res.locals.user.groups,
55+
content: {
56+
firstName: res.locals.user.firstName,
57+
lastName: res.locals.user.lastName,
58+
},
59+
};
60+
61+
const jwt = await new jose.SignJWT(userInfo)
62+
.setProtectedHeader({ alg: 'ES256' })
63+
.setExpirationTime('10 s')
64+
.sign(signingKey);
65+
66+
return res.redirect(`https://docs.foo.com#${jwt}`);
67+
}
68+
69+
```
3370

3471
## Preserving Anchors
3572

0 commit comments

Comments
 (0)