Skip to content

Commit

Permalink
articles added
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMelior committed Jul 20, 2024
1 parent bcb5468 commit db80d16
Show file tree
Hide file tree
Showing 11 changed files with 1,325 additions and 104 deletions.
47 changes: 0 additions & 47 deletions projects/best-practice/app-router-auth/en.mdx
Original file line number Diff line number Diff line change
@@ -1,47 +0,0 @@
---
title: 'Next.js: Authentication'
description: 'Best practices for Server Components, Actions, Middleware'
tags: ['typescript', 'cookie', 'ssr', 'prisma']
---

<AuthExample />

---

## About this approach

Getting user data: `const user = await getUser();`. It's very simple :)

- hi
- man `test` good!
- i'm good

test))

```typescript filename="features/auth/api/jwt.ts" githubPath="src/features/auth/api/jwt.ts"
import { SignJWT, jwtVerify } from 'jose';
import { SessionPayload } from '../types/definitions';

const secretKey = process.env.SESSION_SECRET;
const encodedKey = new TextEncoder().encode(secretKey);

export async function encrypt(payload: SessionPayload) {
return new SignJWT(payload)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.setExpirationTime('7d')
.sign(encodedKey);
}

export async function decrypt(session: string | undefined = '') {
try {
const { payload } = await jwtVerify(session, encodedKey, {
algorithms: ['HS256'],
});
return payload;
} catch (error) {
// console.log('Failed to verify session');
return null;
}
}
```
1 change: 1 addition & 0 deletions projects/best-practice/app-router-auth/examples/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const AuthExample = async () => {
<div className='grid sm:grid-cols-2 gap-12'>
<FormLoginExample className='w-full' isDisabled={Boolean(user)} />
<CodeBlock
hideHeader
dict={dict.ui}
text={
'# Administrator:\n- Username: admin\n- Password: pass\n\n# User:\n- Username: user\n- Password: pass'
Expand Down
Loading

0 comments on commit db80d16

Please sign in to comment.