File size: 3,422 Bytes
62f971f 3679583 20ba598 62f971f 20ba598 62f971f 20ba598 83bfa8d 62f971f 20ba598 83bfa8d 20ba598 62f971f 20ba598 3679583 20ba598 3679583 20ba598 3679583 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | import { UserProfile } from '../types';
export class AuthService {
static async login(id: string, pin: string): Promise<{ user: UserProfile; customToken?: string }> {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, pin })
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
throw new Error(errData.error || 'Invalid Student ID or PIN.');
}
return await response.json();
}
static async loginWithEmail(email: string, password: string): Promise<{ user: UserProfile; customToken?: string }> {
const response = await fetch('/api/auth/login-email', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
throw new Error(errData.error || 'Invalid Email address or Password.');
}
return await response.json();
}
static async registerStudent(data: { fullName: string; class: string; department?: string; pin: string }): Promise<{ user: UserProfile; customToken?: string }> {
const response = await fetch('/api/auth/register/student', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
throw new Error(errData.error || 'Student registration failed.');
}
return await response.json();
}
static async registerStudentWithEmail(data: { fullName: string; class: string; department?: string; email: string; password: string }): Promise<{ user: UserProfile; customToken?: string }> {
const response = await fetch('/api/auth/register/student-email', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
throw new Error(errData.error || 'Student Email registration failed.');
}
return await response.json();
}
static async registerTeacher(data: { fullName: string; subject: string; position: string; classesManaged: string[]; pin: string }): Promise<{ user: UserProfile; customToken?: string }> {
const response = await fetch('/api/auth/register/teacher', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
throw new Error(errData.error || 'Teacher registration failed.');
}
return await response.json();
}
static async registerTeacherWithEmail(data: { fullName: string; subject: string; position: string; classesManaged: string[]; email: string; password: string }): Promise<{ user: UserProfile; customToken?: string }> {
const response = await fetch('/api/auth/register/teacher-email', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
if (!response.ok) {
const errData = await response.json().catch(() => ({}));
throw new Error(errData.error || 'Teacher Email registration failed.');
}
return await response.json();
}
}
|