add registration and session logic
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
'use server'
|
||||
|
||||
const FRUITS_URL = 'https://www.fruityvice.com/api/fruit/';
|
||||
const ALL = 'all'
|
||||
|
||||
export async function fetchFruits() {
|
||||
try {
|
||||
const response = await fetch(FRUITS_URL + ALL, { 'method': 'GET' });
|
||||
let parsed = await response.json();
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
throw new Error('Failed to fetch fruits data.');
|
||||
}
|
||||
}
|
||||
|
||||
export async function testConnect() {
|
||||
try {
|
||||
const response = await fetch('http://localhost:8080/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
"fullName": "Vladislav Sergevich",
|
||||
"email": "s2@e.ru",
|
||||
"password": "1121231412"
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
let parsed = await response.json();
|
||||
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import * as z from 'zod'
|
||||
|
||||
export const SignupFormSchema = z.object({
|
||||
name: z
|
||||
.string()
|
||||
.min(2, { error: 'Name must be at least 2 characters long.' })
|
||||
.trim(),
|
||||
email: z.email({ error: 'Please enter a valid email.' }).trim(),
|
||||
password: z
|
||||
.string()
|
||||
.min(5, { error: 'Be at least 5 characters long' })
|
||||
.regex(/[a-zA-Z]/, { error: 'Contain at least one letter.' })
|
||||
.regex(/[0-9]/, { error: 'Contain at least one number.' })
|
||||
.trim(),
|
||||
})
|
||||
|
||||
export type FormState =
|
||||
| {
|
||||
errors?: {
|
||||
name?: string[]
|
||||
email?: string[]
|
||||
password?: string[]
|
||||
}
|
||||
message?: string
|
||||
}
|
||||
| undefined
|
||||
|
||||
export type FormAction = (
|
||||
state: FormState,
|
||||
formData: FormData
|
||||
) => Promise<FormState>
|
||||
@@ -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')
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'server-only'
|
||||
import { cookies } from 'next/headers'
|
||||
import { decrypt } from '@/app/lib/session'
|
||||
|
||||
export async function updateSession() {
|
||||
const session = (await cookies()).get('session')?.value
|
||||
const payload = await decrypt(session)
|
||||
|
||||
if (!session || !payload) {
|
||||
return null
|
||||
}
|
||||
|
||||
const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
|
||||
|
||||
const cookieStore = await cookies()
|
||||
cookieStore.set('session', session, {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
expires: expires,
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user