Насторил работу блога через Backend

This commit is contained in:
Web-serfer 2026-04-15 02:12:25 +05:00
parent 014439d565
commit edd730b438
33 changed files with 1019 additions and 200 deletions

View file

@ -0,0 +1,45 @@
import type { APIRoute } from 'astro';
import PocketBase from 'pocketbase';
export const POST: APIRoute = async ({ request, cookies }) => {
try {
const pb = new PocketBase(import.meta.env.POCKETBASE_URL);
const data = await request.json();
const { email, password } = data;
if (!email || !password) {
return new Response(JSON.stringify({
success: false,
error: 'Email и пароль обязательны'
}), { status: 400 });
}
const authRecord = await pb.collection('users').authWithPassword(email, password);
cookies.set('pb_auth', JSON.stringify(pb.authStore.exportToCookie()), {
path: '/',
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 60 * 60 * 24 * 7,
});
return new Response(JSON.stringify({
success: true,
user: {
id: authRecord.id,
name: authRecord.name,
email: authRecord.email,
}
}), { status: 200 });
} catch (error: any) {
console.error('Sign in error:', error);
return new Response(JSON.stringify({
success: false,
error: error.message || 'Неверный email или пароль'
}), { status: 401 });
}
};

View file

@ -0,0 +1,10 @@
import type { APIRoute } from 'astro';
export const POST: APIRoute = async ({ cookies }) => {
cookies.delete('pb_auth', { path: '/' });
return new Response(JSON.stringify({
success: true,
message: 'Вы успешно вышли из аккаунта'
}), { status: 200 });
};

View file

@ -0,0 +1,45 @@
import type { APIRoute } from 'astro';
import PocketBase from 'pocketbase';
export const POST: APIRoute = async ({ request, redirect }) => {
try {
const pb = new PocketBase(import.meta.env.POCKETBASE_URL);
const data = await request.json();
const { name, email, phone, password } = data;
if (!email || !password) {
return new Response(JSON.stringify({
success: false,
error: 'Email и пароль обязательны'
}), { status: 400 });
}
const record = await pb.collection('users').create({
name,
email,
phone,
password,
passwordConfirm: password,
});
await pb.collection('users').authWithPassword(email, password);
return new Response(JSON.stringify({
success: true,
record: {
id: record.id,
name: record.name,
email: record.email,
}
}), { status: 201 });
} catch (error: any) {
console.error('Sign up error:', error);
return new Response(JSON.stringify({
success: false,
error: error.message || 'Ошибка при регистрации'
}), { status: 400 });
}
};