first commit
This commit is contained in:
52
backend/src/routes/auth.ts
Normal file
52
backend/src/routes/auth.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Router } from 'express';
|
||||
import { body } from 'express-validator';
|
||||
import { validateRequest } from '../middleware/validateRequest';
|
||||
import { authController } from '../controllers/authController';
|
||||
import { authMiddleware } from '../middleware/authMiddleware';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Register
|
||||
router.post('/register', [
|
||||
body('username').isLength({ min: 3, max: 30 }).matches(/^[a-zA-Z0-9_]+$/),
|
||||
body('password').isLength({ min: 8 }),
|
||||
body('confirmPassword').isLength({ min: 8 }).custom((value, { req }) => {
|
||||
if (value !== req.body.password) {
|
||||
throw new Error('密码和确认密码不匹配');
|
||||
}
|
||||
return true;
|
||||
}),
|
||||
body('firstName').optional().isLength({ max: 50 }),
|
||||
body('lastName').optional().isLength({ max: 50 }),
|
||||
validateRequest
|
||||
], authController.register);
|
||||
|
||||
// Login
|
||||
router.post('/login', [
|
||||
body('username').isLength({ min: 3, max: 30 }),
|
||||
body('password').notEmpty(),
|
||||
validateRequest
|
||||
], authController.login);
|
||||
|
||||
// Logout
|
||||
router.post('/logout', authMiddleware, authController.logout);
|
||||
|
||||
// Setup TOTP
|
||||
router.post('/setup-totp', authMiddleware, authController.setupTOTP);
|
||||
|
||||
// Verify TOTP
|
||||
router.post('/verify-totp', [
|
||||
body('token').notEmpty(),
|
||||
validateRequest
|
||||
], authMiddleware, authController.verifyTOTP);
|
||||
|
||||
// Get current user
|
||||
router.get('/me', authMiddleware, authController.getCurrentUser);
|
||||
|
||||
// Refresh token
|
||||
router.post('/refresh', authController.refreshToken);
|
||||
|
||||
// Debug session (for development)
|
||||
router.get('/debug-session', authMiddleware, authController.debugSession);
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user