47 lines
1.4 KiB
TypeScript

import { Request, Response } from 'express';
import { AuthRequest } from '../../../middleware/auth';
import { authService } from '../../../services/authService';
export async function login(req: Request, res: Response) {
try {
const { email, password } = req.body;
const { user, token } = await authService.login(email, password);
res.json({ user, token });
} catch {
res.status(401).json({ error: 'Invalid credentials' });
}
}
export async function signIn(req: Request, res: Response) {
try {
const { email, password, displayName, avatarUrl } = req.body;
const user = await authService.createUser({email : email, password : password, displayName : displayName, avatarUrl : avatarUrl, permissions : {}});
res.json({ user });
} catch {
res.status(401).json({ error: 'Invalid signIn credentials' });
}
}
export async function getCurrentUser(req: AuthRequest, res: Response) {
try {
if (!req.user) {
res.status(401).json({ error: 'Not authenticated' });
return
}
res.json(req.user);
} catch {
res.status(500).json({ error: 'Server error' });
}
}
export async function refreshToken(req: AuthRequest, res: Response) {
try {
if (!req.user) {
return res.status(401).json({ error: 'Not authenticated' });
}
const token = await authService.generateToken(req.user.id);
res.json({ token });
} catch {
res.status(500).json({ error: 'Failed to refresh token' });
}
}