feat: add 2FA, secure cookies and publishing tools
This commit is contained in:
@@ -8,6 +8,7 @@ export interface ApiUser {
|
||||
email: string;
|
||||
name: string;
|
||||
role: ApiRole;
|
||||
impersonatedBy?: string;
|
||||
}
|
||||
|
||||
export interface Profile {
|
||||
@@ -118,11 +119,17 @@ export interface HomeBanner extends HomeBannerInput {
|
||||
courseImageUrl: string | null;
|
||||
}
|
||||
|
||||
interface Session {
|
||||
token: string;
|
||||
export interface Session {
|
||||
user: ApiUser;
|
||||
}
|
||||
|
||||
export type LoginResponse = Session | { twoFactorRequired: true; challengeToken: string };
|
||||
|
||||
export interface TwoFactorStatus {
|
||||
enabled: boolean;
|
||||
backupCodesRemaining: number;
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(message: string, public status: number) {
|
||||
super(message);
|
||||
@@ -149,10 +156,7 @@ export function clearSession() {
|
||||
}
|
||||
|
||||
export async function downloadCourseAsset(assetId: string, filename: string) {
|
||||
const session = getSession();
|
||||
const headers = new Headers();
|
||||
if (session) headers.set('Authorization', `Bearer ${session.token}`);
|
||||
const response = await fetch(`${API_URL}/courses/assets/${assetId}/download`, { headers });
|
||||
const response = await fetch(`${API_URL}/courses/assets/${assetId}/download`, { credentials: 'include' });
|
||||
if (!response.ok) {
|
||||
const body = await response.json().catch(() => ({}));
|
||||
throw new ApiError(body.error || 'Não foi possível baixar este material.', response.status);
|
||||
@@ -169,12 +173,10 @@ export async function downloadCourseAsset(assetId: string, filename: string) {
|
||||
}
|
||||
|
||||
export async function apiRequest<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
const session = getSession();
|
||||
const headers = new Headers(options.headers);
|
||||
if (options.body) headers.set('Content-Type', 'application/json');
|
||||
if (session) headers.set('Authorization', `Bearer ${session.token}`);
|
||||
|
||||
const response = await fetch(`${API_URL}${path}`, { ...options, headers });
|
||||
const response = await fetch(`${API_URL}${path}`, { ...options, headers, credentials: 'include' });
|
||||
if (response.status === 204) return undefined as T;
|
||||
|
||||
const body = await response.json().catch(() => ({}));
|
||||
@@ -185,20 +187,16 @@ export async function apiRequest<T>(path: string, options: RequestInit = {}): Pr
|
||||
}
|
||||
|
||||
async function uploadRequest<T>(path: string, file: File): Promise<T> {
|
||||
const session = getSession();
|
||||
const headers = new Headers({ 'Content-Type': file.type || 'application/octet-stream' });
|
||||
if (session) headers.set('Authorization', `Bearer ${session.token}`);
|
||||
const response = await fetch(`${API_URL}${path}`, { method: 'PUT', headers, body: file });
|
||||
const response = await fetch(`${API_URL}${path}`, { method: 'PUT', headers, body: file, credentials: 'include' });
|
||||
const body = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new ApiError(body.error || 'Não foi possível concluir o envio do vídeo.', response.status);
|
||||
return body as T;
|
||||
}
|
||||
|
||||
async function uploadImageRequest<T>(path: string, file: File): Promise<T> {
|
||||
const session = getSession();
|
||||
const headers = new Headers({ 'Content-Type': file.type || 'application/octet-stream' });
|
||||
if (session) headers.set('Authorization', `Bearer ${session.token}`);
|
||||
const response = await fetch(`${API_URL}${path}`, { method: 'POST', headers, body: file });
|
||||
const response = await fetch(`${API_URL}${path}`, { method: 'POST', headers, body: file, credentials: 'include' });
|
||||
const body = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new ApiError(body.error || 'Não foi possível enviar a imagem de capa.', response.status);
|
||||
return body as T;
|
||||
@@ -206,7 +204,10 @@ async function uploadImageRequest<T>(path: string, file: File): Promise<T> {
|
||||
|
||||
export const authApi = {
|
||||
async login(email: string, password: string) {
|
||||
return apiRequest<Session>('/auth/login', { method: 'POST', body: JSON.stringify({ email, password }) });
|
||||
return apiRequest<LoginResponse>('/auth/login', { method: 'POST', body: JSON.stringify({ email, password }) });
|
||||
},
|
||||
async verifyTwoFactor(challengeToken: string, code: string) {
|
||||
return apiRequest<Session>('/auth/2fa/verify', { method: 'POST', body: JSON.stringify({ challengeToken, code }) });
|
||||
},
|
||||
async register(name: string, email: string, password: string) {
|
||||
return apiRequest<Session>('/auth/register', { method: 'POST', body: JSON.stringify({ name, email, password }) });
|
||||
@@ -214,6 +215,12 @@ export const authApi = {
|
||||
async me() {
|
||||
return apiRequest<{ user: ApiUser }>('/auth/me');
|
||||
},
|
||||
async logout() {
|
||||
return apiRequest<void>('/auth/logout', { method: 'POST' });
|
||||
},
|
||||
async stopImpersonation() {
|
||||
return apiRequest<{ user: ApiUser }>('/auth/stop-impersonation', { method: 'POST' });
|
||||
},
|
||||
async acceptInvitation(token: string, name: string, password: string) {
|
||||
return apiRequest<Session>('/auth/accept-invitation', { method: 'POST', body: JSON.stringify({ token, name, password }) });
|
||||
},
|
||||
@@ -223,6 +230,21 @@ export const authApi = {
|
||||
async forgotPassword(email: string) {
|
||||
return apiRequest<void>('/auth/forgot-password', { method: 'POST', body: JSON.stringify({ email }) });
|
||||
},
|
||||
async twoFactorStatus() {
|
||||
return apiRequest<{ data: TwoFactorStatus }>('/auth/2fa');
|
||||
},
|
||||
async setupTwoFactor() {
|
||||
return apiRequest<{ data: { secret: string; otpauthUrl: string; qrCode: string } }>('/auth/2fa/setup', { method: 'POST' });
|
||||
},
|
||||
async enableTwoFactor(code: string) {
|
||||
return apiRequest<{ data: { backupCodes: string[] } }>('/auth/2fa/enable', { method: 'POST', body: JSON.stringify({ code }) });
|
||||
},
|
||||
async regenerateBackupCodes(code: string) {
|
||||
return apiRequest<{ data: { backupCodes: string[] } }>('/auth/2fa/backup-codes', { method: 'POST', body: JSON.stringify({ code }) });
|
||||
},
|
||||
async disableTwoFactor(code: string, password: string) {
|
||||
return apiRequest<void>('/auth/2fa/disable', { method: 'POST', body: JSON.stringify({ code, password }) });
|
||||
},
|
||||
};
|
||||
|
||||
export const adminApi = {
|
||||
@@ -230,6 +252,15 @@ export const adminApi = {
|
||||
async listUsers() {
|
||||
return apiRequest<{ data: ManagedUser[] }>('/admin/users');
|
||||
},
|
||||
async impersonate(userId: string) {
|
||||
return apiRequest<{ data: ApiUser }>(`/admin/users/${userId}/impersonate`, { method: 'POST' });
|
||||
},
|
||||
async bulkUpdateUsers(userIds: string[], isActive: boolean) {
|
||||
return apiRequest<{ data: ManagedUser[] }>('/admin/users/bulk', {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify({ userIds, isActive }),
|
||||
});
|
||||
},
|
||||
async updateUser(userId: string, update: Partial<Pick<ManagedUser, 'name' | 'email' | 'role' | 'isActive'>>) {
|
||||
return apiRequest<{ data: ManagedUser }>(`/admin/users/${userId}`, {
|
||||
method: 'PATCH',
|
||||
@@ -239,6 +270,12 @@ export const adminApi = {
|
||||
async invite(email: string, role: 'instructor' = 'instructor') {
|
||||
return apiRequest<{ data: { inviteUrl: string } }>('/admin/invitations', { method: 'POST', body: JSON.stringify({ email, role }) });
|
||||
},
|
||||
async bulkInvite(emails: string[]) {
|
||||
return apiRequest<{ data: Array<{ email: string; inviteUrl?: string; error?: string }> }>('/admin/invitations/bulk', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ emails }),
|
||||
});
|
||||
},
|
||||
async passwordReset(userId: string) {
|
||||
return apiRequest<{ data: { resetUrl: string } }>(`/admin/users/${userId}/password-reset`, { method: 'POST' });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user