36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import { Request, Response } from 'express';
|
|
import { AuthRequest } from '../../../middleware/auth';
|
|
import { authService } from '../../../services/authService.js';
|
|
|
|
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 getCurrentUser(req: AuthRequest, res: Response) {
|
|
try {
|
|
if (!req.user) {
|
|
return res.status(401).json({ error: 'Not authenticated' });
|
|
}
|
|
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' });
|
|
}
|
|
} |