11import { Hono } from 'hono' ;
22import { setCookie } from 'hono/cookie' ;
33import { verify } from 'hono/jwt' ;
4- import { ZodError } from 'zod' ;
54
65import { authService } from '../services/auth' ;
76import { github as githubConfig , jwt as jwtConfig } from '../config' ;
@@ -14,7 +13,7 @@ import {
1413 ResetPasswordInputSchema ,
1514 ResendSetPasswordInputSchema
1615} from '../types' ;
17- import { jsonResponse } from '../utils/validate' ;
16+ import { jsonResponse , parseAndValidate } from '../utils/validate' ;
1817
1918const auth = new Hono ( ) ;
2019
@@ -134,8 +133,7 @@ auth.get('/callback/github', async (c) => {
134133// Step 1: Register with email only (sends set-password link)
135134auth . post ( '/register' , async ( c ) => {
136135 try {
137- const body = await c . req . json ( ) ;
138- const input = RegisterInputSchema . parse ( body ) ;
136+ const input = await parseAndValidate ( c , RegisterInputSchema ) ;
139137
140138 await authService . registerWithEmail ( input . email ) ;
141139
@@ -146,10 +144,6 @@ auth.post('/register', async (c) => {
146144 201
147145 ) ;
148146 } catch ( error ) {
149- if ( error instanceof ZodError ) {
150- return c . json ( { error : 'Invalid email format' } , 400 ) ;
151- }
152-
153147 if ( error instanceof Error && error . message === 'Email already registered' ) {
154148 return c . json ( { error : 'Email already registered' } , 409 ) ;
155149 }
@@ -162,8 +156,7 @@ auth.post('/register', async (c) => {
162156// Step 2: Set password using token from email
163157auth . post ( '/set-password' , async ( c ) => {
164158 try {
165- const body = await c . req . json ( ) ;
166- const input = SetPasswordInputSchema . parse ( body ) ;
159+ const input = await parseAndValidate ( c , SetPasswordInputSchema ) ;
167160
168161 const user = await authService . setPassword ( input . token , input . password ) ;
169162 const tokens = await authService . createTokens ( user ) ;
@@ -176,10 +169,6 @@ auth.post('/set-password', async (c) => {
176169
177170 return jsonResponse ( c , LoginResponseSchema , tokens ) ;
178171 } catch ( error ) {
179- if ( error instanceof ZodError ) {
180- return c . json ( { error : 'Invalid token or password format' } , 400 ) ;
181- }
182-
183172 if ( error instanceof Error && error . message === 'Invalid or expired token' ) {
184173 return c . json ( { error : 'Invalid or expired link' } , 400 ) ;
185174 }
@@ -192,8 +181,7 @@ auth.post('/set-password', async (c) => {
192181// Login with email and password
193182auth . post ( '/login' , async ( c ) => {
194183 try {
195- const body = await c . req . json ( ) ;
196- const input = LoginInputSchema . parse ( body ) ;
184+ const input = await parseAndValidate ( c , LoginInputSchema ) ;
197185
198186 const user = await authService . loginWithPassword ( input . email , input . password ) ;
199187 const tokens = await authService . createTokens ( user ) ;
@@ -206,10 +194,6 @@ auth.post('/login', async (c) => {
206194
207195 return jsonResponse ( c , LoginResponseSchema , tokens ) ;
208196 } catch ( error ) {
209- if ( error instanceof ZodError ) {
210- return c . json ( { error : 'Invalid email or password format' } , 400 ) ;
211- }
212-
213197 if ( error instanceof Error && error . message === 'Invalid credentials' ) {
214198 return c . json ( { error : 'Invalid email or password' } , 401 ) ;
215199 }
@@ -222,8 +206,7 @@ auth.post('/login', async (c) => {
222206// Request password reset
223207auth . post ( '/forgot-password' , async ( c ) => {
224208 try {
225- const body = await c . req . json ( ) ;
226- const input = ForgotPasswordInputSchema . parse ( body ) ;
209+ const input = await parseAndValidate ( c , ForgotPasswordInputSchema ) ;
227210
228211 await authService . requestPasswordReset ( input . email ) ;
229212
@@ -232,10 +215,6 @@ auth.post('/forgot-password', async (c) => {
232215 message : 'If an account exists with this email, you will receive a password reset link.'
233216 } ) ;
234217 } catch ( error ) {
235- if ( error instanceof ZodError ) {
236- return c . json ( { error : 'Invalid email format' } , 400 ) ;
237- }
238-
239218 console . error ( 'Password reset request error:' , error ) ;
240219 return c . json ( {
241220 message : 'If an account exists with this email, you will receive a password reset link.'
@@ -246,8 +225,7 @@ auth.post('/forgot-password', async (c) => {
246225// Reset password with token
247226auth . post ( '/reset-password' , async ( c ) => {
248227 try {
249- const body = await c . req . json ( ) ;
250- const input = ResetPasswordInputSchema . parse ( body ) ;
228+ const input = await parseAndValidate ( c , ResetPasswordInputSchema ) ;
251229
252230 const user = await authService . resetPassword ( input . token , input . password ) ;
253231 const tokens = await authService . createTokens ( user ) ;
@@ -260,10 +238,6 @@ auth.post('/reset-password', async (c) => {
260238
261239 return jsonResponse ( c , LoginResponseSchema , tokens ) ;
262240 } catch ( error ) {
263- if ( error instanceof ZodError ) {
264- return c . json ( { error : 'Invalid token or password format' } , 400 ) ;
265- }
266-
267241 if ( error instanceof Error && error . message === 'Invalid or expired token' ) {
268242 return c . json ( { error : 'Invalid or expired reset link' } , 400 ) ;
269243 }
@@ -276,19 +250,14 @@ auth.post('/reset-password', async (c) => {
276250// Resend set-password email
277251auth . post ( '/resend-set-password' , async ( c ) => {
278252 try {
279- const body = await c . req . json ( ) ;
280- const input = ResendSetPasswordInputSchema . parse ( body ) ;
253+ const input = await parseAndValidate ( c , ResendSetPasswordInputSchema ) ;
281254
282255 await authService . resendSetPasswordEmail ( input . email ) ;
283256
284257 return c . json ( {
285258 message : 'If a pending account exists with this email, a new link has been sent.'
286259 } ) ;
287260 } catch ( error ) {
288- if ( error instanceof ZodError ) {
289- return c . json ( { error : 'Invalid email format' } , 400 ) ;
290- }
291-
292261 console . error ( 'Resend set-password error:' , error ) ;
293262 return c . json ( {
294263 message : 'If a pending account exists with this email, a new link has been sent.'
0 commit comments