Насторил работу блога через Backend
This commit is contained in:
parent
014439d565
commit
edd730b438
33 changed files with 1019 additions and 200 deletions
45
frontend/src/pages/api/auth/sign-up.ts
Normal file
45
frontend/src/pages/api/auth/sign-up.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue