| 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(); |
| } |
| } |
|
|