@@ -5,6 +5,7 @@ import { GenericResponses } from '~/lib/response-schemas'
55import { getConnection } from '~/lib/db/connection'
66import { user } from '~/lib/db/schema'
77import { eq } from 'drizzle-orm'
8+ import { setCookie } from 'hono/cookie'
89
910const bodySchema = z . object ( {
1011 email : z . string ( ) . email ( ) . openapi ( {
@@ -17,11 +18,11 @@ const bodySchema = z.object({
1718 } ) ,
1819 name : z . string ( ) . min ( 1 ) . openapi ( {
1920 description : "User's display name" ,
20- example : 'John Doe ' ,
21+ example : 'display name ' ,
2122 } ) ,
2223 username : z . string ( ) . min ( 3 ) . openapi ( {
2324 description : "User's unique username (required)" ,
24- example : 'johndoe ' ,
25+ example : 'username ' ,
2526 } ) ,
2627} )
2728
@@ -98,9 +99,12 @@ export const AuthRegisterRoute = (handler: AppHandler) => {
9899 password,
99100 name,
100101 } ,
102+ asResponse : true ,
101103 } )
102104
103- if ( ! result . user ) {
105+ const authData = ( await result . json ( ) ) as any
106+
107+ if ( ! authData . user ) {
104108 return ctx . json (
105109 {
106110 success : false ,
@@ -116,26 +120,41 @@ export const AuthRegisterRoute = (handler: AppHandler) => {
116120 username,
117121 updatedAt : new Date ( ) ,
118122 } )
119- . where ( eq ( user . id , result . user . id ) )
123+ . where ( eq ( user . id , authData . user . id ) )
120124 . returning ( )
121125
122126 const finalUsername = updatedUsers . length > 0 ? updatedUsers [ 0 ] ! . username : username
123127
128+ const cookieHeaders = result . headers . get ( 'set-cookie' )
129+ if ( cookieHeaders ) {
130+ const cookieMatches = cookieHeaders . match ( / ( [ ^ = ] + ) = ( [ ^ ; ] + ) / )
131+ if ( cookieMatches && cookieMatches [ 1 ] && cookieMatches [ 2 ] ) {
132+ const name = cookieMatches [ 1 ] . trim ( )
133+ const value = cookieMatches [ 2 ] . trim ( )
134+ setCookie ( ctx , name , value , {
135+ httpOnly : true ,
136+ secure : true ,
137+ sameSite : 'Lax' ,
138+ path : '/' ,
139+ maxAge : 60 * 60 * 24 * 30 ,
140+ } )
141+ }
142+ }
143+
124144 return ctx . json (
125145 {
126146 success : true ,
127147 message : 'User registered successfully' ,
128148 user : {
129- id : result . user . id ,
130- email : result . user . email ,
131- name : result . user . name ,
149+ id : authData . user . id ,
150+ email : authData . user . email ,
151+ name : authData . user . name ,
132152 username : finalUsername ,
133153 } ,
134154 } ,
135155 201 ,
136156 )
137157 } catch ( error : any ) {
138- console . error ( 'Registration error:' , error )
139158 return ctx . json (
140159 {
141160 success : false ,
0 commit comments