-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcb5468
commit db80d16
Showing
11 changed files
with
1,325 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.