Skip to content

Commit c83e41e

Browse files
committed
docs: update getting-started
1 parent 9433cb3 commit c83e41e

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

docs/README.md

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const { getSession, commitSession, destroySession } = sessionStorage
9595

9696
Now that we have everything set up, we can start implementing the Strategy Instance.
9797

98-
### 1. Implementing the Strategy Instance.
98+
### 1. Implementing Strategy Instance
9999

100100
> [!IMPORTANT]
101101
> A random 64-character hexadecimal string is required to generate the TOTP codes.
@@ -120,7 +120,7 @@ export const authenticator = new Authenticator<User>()
120120
authenticator.use(
121121
new TOTPStrategy(
122122
{
123-
secret: process.env.ENCRYPTION_SECRET || 'MY_64_HEX_SECRET',
123+
secret: process.env.ENCRYPTION_SECRET,
124124
emailSentRedirect: '/verify',
125125
magicLinkPath: '/verify',
126126
successRedirect: '/dashboard',
@@ -133,9 +133,9 @@ authenticator.use(
133133
```
134134

135135
> [!TIP]
136-
> You can customize the cookie behavior by passing `cookieOptions` to the `TOTPStrategy` instance. Check [Customization](https://github.com/dev-xo/remix-auth-totp/blob/main/docs/customization.md) to learn more.
136+
> You can customize the cookie behavior by passing a `cookieOptions` property to the `TOTPStrategy` instance. Check [Customization](https://github.com/dev-xo/remix-auth-totp/blob/main/docs/customization.md) to learn more.
137137
138-
### 2: Implementing the Strategy Logic.
138+
### 2: Implementing Strategy Logic
139139

140140
The Strategy Instance requires the following method: `sendTOTP`.
141141

@@ -154,11 +154,12 @@ authenticator.use(
154154
)
155155
```
156156

157-
### 3. Creating and Storing the User.
157+
### 3. Handling User Creation
158158

159159
The Strategy returns a `verify` method that allows handling our own logic. This includes creating the user, updating the session, etc.<br />
160160

161-
When using Cloudflare D1, you may want to perform the lookup in `action` or `loader` after committing the session, by passing the `context` binding to a `findOrCreateUserByEmail` function.
161+
> [!TIP]
162+
> When using Cloudflare D1, consider performing user lookups in the `action` or `loader` functions after committing the session. You can pass the `context` binding to a `findOrCreateUserByEmail` function to handle database operations.
162163
163164
This should return the user data that will be stored in Session.
164165

@@ -171,11 +172,9 @@ authenticator.use(
171172
},
172173
async ({ email }) => {
173174
// Get user from database.
174-
let user = await db.user.findFirst({
175-
where: { email },
176-
})
175+
let user = await db.user.findFirst({ where: { email } })
177176

178-
// Create a new user if it doesn't exist.
177+
// Create a new user (if it doesn't exist).
179178
if (!user) {
180179
user = await db.user.create({
181180
data: { email },
@@ -204,8 +203,17 @@ authenticator.use(
204203

205204
Last but not least, we need to create the routes for the authentication flow.
206205

206+
We'll require the following routes:
207+
208+
- `login.tsx` - Handles the login form submission.
209+
- `verify.tsx` - Handles the TOTP verification form submission.
210+
- `logout.tsx` - Handles the logout.
211+
- `dashboard.tsx` - Handles the authenticated route (optional).
212+
207213
### `login.tsx`
208214

215+
This route is used to handle the login form submission.
216+
209217
```tsx
210218
// app/routes/login.tsx
211219
import { redirect } from 'react-router'
@@ -271,6 +279,8 @@ export default function Login() {
271279

272280
### `verify.tsx`
273281

282+
This route is used to handle the TOTP verification form submission.
283+
274284
For the verify route, we are leveraging `@mjackson/headers` to parse the cookie. Created by Michael Jackson, the CO-Founder of Remix/React Router.
275285

276286
```tsx
@@ -389,8 +399,32 @@ export default function Verify() {
389399
}
390400
```
391401

402+
### `logout.tsx`
403+
404+
This route is used to destroy the session and redirect to the login page.
405+
406+
```tsx
407+
// app/routes/logout.tsx
408+
import { sessionStorage } from '~/lib/auth-session.server'
409+
import { redirect } from 'react-router'
410+
411+
export async function loader({ request }: Route.LoaderArgs) {
412+
// Get the session.
413+
const session = await sessionStorage.getSession(request.headers.get('Cookie'))
414+
415+
// Destroy the session and redirect to login.
416+
return redirect('/auth/login', {
417+
headers: {
418+
'Set-Cookie': await sessionStorage.destroySession(session),
419+
},
420+
})
421+
}
422+
```
423+
392424
### `dashboard.tsx`
393425

426+
This route is used to display the authenticated user's dashboard (optional).
427+
394428
```tsx
395429
// app/routes/dashboard.tsx
396430
import { Link } from 'react-router'
@@ -422,26 +456,6 @@ export default function Account() {
422456
}
423457
```
424458

425-
### `logout.tsx`
426-
427-
```tsx
428-
// app/routes/logout.tsx
429-
import { sessionStorage } from '~/lib/auth-session.server'
430-
import { redirect } from 'react-router'
431-
432-
export async function loader({ request }: Route.LoaderArgs) {
433-
// Get the session.
434-
const session = await sessionStorage.getSession(request.headers.get('Cookie'))
435-
436-
// Destroy the session and redirect to login.
437-
return redirect('/auth/login', {
438-
headers: {
439-
'Set-Cookie': await sessionStorage.destroySession(session),
440-
},
441-
})
442-
}
443-
```
444-
445459
## Next Steps
446460

447461
🎉 Done! You've completed the basic setup.

0 commit comments

Comments
 (0)