| 1 | import { authProxyService } from '@/services/auth-proxy.service'; |
| 2 | import { loginSchema, refreshSchema, registerSchema, revokeSchema } from '@/validation/auth.schema'; |
| 3 | import { AsyncHandler } from '@chatapp/common'; |
| 4 | |
| 5 | export const registerUser: AsyncHandler = async (req, res, next) => { |
| 6 | try { |
| 7 | const payload = registerSchema.parse(req.body); |
| 8 | const response = await authProxyService.register(payload); |
| 9 | res.status(201).json(response); |
| 10 | } catch (error) { |
| 11 | next(error); |
| 12 | } |
| 13 | }; |
| 14 | |
| 15 | export const loginUser: AsyncHandler = async (req, res, next) => { |
| 16 | try { |
| 17 | const payload = loginSchema.parse(req.body); |
| 18 | const tokens = await authProxyService.login(payload); |
| 19 | res.json(tokens); |
| 20 | } catch (error) { |
| 21 | next(error); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | export const refreshTokens: AsyncHandler = async (req, res, next) => { |
| 26 | try { |
| 27 | const payload = refreshSchema.parse(req.body); |
| 28 | const tokens = await authProxyService.refresh(payload); |
| 29 | res.json(tokens); |
| 30 | } catch (error) { |
| 31 | next(error); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | export const revokeTokens: AsyncHandler = async (req, res, next) => { |
| 36 | try { |
| 37 | const payload = revokeSchema.parse(req.body); |
| 38 | await authProxyService.revoke(payload); |
| 39 | res.status(204).send(); |
| 40 | } catch (error) { |
| 41 | next(error); |
| 42 | } |
| 43 | }; |