add registration and session logic
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import 'server-only'
|
||||
import { cookies } from 'next/headers'
|
||||
import { SignJWT, jwtVerify } from 'jose'
|
||||
/* import { SessionPayload } from '@/app/lib/definitions' */
|
||||
|
||||
const secretKey = process.env.SESSION_SECRET
|
||||
const encodedKey = new TextEncoder().encode(secretKey)
|
||||
|
||||
export async function encrypt(payload: any) {
|
||||
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')
|
||||
}
|
||||
}
|
||||
|
||||
export async function createSession(userId: string) {
|
||||
const expiresAt = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
|
||||
const session = await encrypt({ userId, expiresAt })
|
||||
const cookieStore = await cookies()
|
||||
|
||||
cookieStore.set('session', session, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
expires: expiresAt,
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteSession() {
|
||||
const cookieStore = await cookies()
|
||||
cookieStore.delete('session')
|
||||
}
|
||||
Reference in New Issue
Block a user