Skip to content

Commit 65caa89

Browse files
committed
yess
1 parent be305ba commit 65caa89

File tree

9 files changed

+75
-35
lines changed

9 files changed

+75
-35
lines changed

src/routes/auth/login.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GenericResponses } from '~/lib/response-schemas'
55
import { getConnection } from '~/lib/db/connection'
66
import { user } from '~/lib/db/schema'
77
import { eq } from 'drizzle-orm'
8+
import { setCookie } from 'hono/cookie'
89

910
const bodySchema = z.object({
1011
email: z.string().email().openapi({
@@ -70,9 +71,12 @@ export const AuthLoginRoute = (handler: AppHandler) => {
7071
email,
7172
password,
7273
},
74+
asResponse: true,
7375
})
7476

75-
if (!result.user) {
77+
const authData = (await result.json()) as any
78+
79+
if (!authData.user) {
7680
return ctx.json(
7781
{
7882
success: false,
@@ -82,25 +86,40 @@ export const AuthLoginRoute = (handler: AppHandler) => {
8286
)
8387
}
8488

85-
const dbUser = await drizzle.select().from(user).where(eq(user.id, result.user.id)).limit(1)
89+
const dbUser = await drizzle.select().from(user).where(eq(user.id, authData.user.id)).limit(1)
8690

8791
const username = dbUser.length > 0 ? dbUser[0]!.username : null
8892

93+
const cookieHeaders = result.headers.get('set-cookie')
94+
if (cookieHeaders) {
95+
const cookieMatches = cookieHeaders.match(/([^=]+)=([^;]+)/)
96+
if (cookieMatches && cookieMatches[1] && cookieMatches[2]) {
97+
const name = cookieMatches[1].trim()
98+
const value = cookieMatches[2].trim()
99+
setCookie(ctx, name, value, {
100+
httpOnly: true,
101+
secure: true,
102+
sameSite: 'Lax',
103+
path: '/',
104+
maxAge: 60 * 60 * 24 * 7,
105+
})
106+
}
107+
}
108+
89109
return ctx.json(
90110
{
91111
success: true,
92112
message: 'Login successful',
93113
user: {
94-
id: result.user.id,
95-
email: result.user.email,
96-
name: result.user.name,
114+
id: authData.user.id,
115+
email: authData.user.email,
116+
name: authData.user.name,
97117
username: username,
98118
},
99119
},
100120
200,
101121
)
102122
} catch (error: any) {
103-
console.error('Login error:', error)
104123
return ctx.json(
105124
{
106125
success: false,

src/routes/auth/logout.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { AppHandler } from '~/lib/handler'
33
import { createRoute } from '@hono/zod-openapi'
44
import { GenericResponses } from '~/lib/response-schemas'
55
import { requireAuth } from '~/lib/auth/middleware'
6+
import { deleteCookie } from 'hono/cookie'
67

78
const responseSchema = z.object({
89
success: z.boolean(),
910
message: z.string(),
1011
})
1112

1213
const openRoute = createRoute({
13-
path: '/auth/logout',
14+
path: '/logout',
1415
method: 'post',
1516
summary: 'Logout user',
1617
description: 'Logout the current user session.',
@@ -46,6 +47,12 @@ export const AuthLogoutRoute = (handler: AppHandler) => {
4647
headers,
4748
})
4849

50+
deleteCookie(ctx, 'better-auth.session_token', {
51+
path: '/',
52+
secure: true,
53+
sameSite: 'Lax',
54+
})
55+
4956
return ctx.json(
5057
{
5158
success: true,
@@ -54,7 +61,6 @@ export const AuthLogoutRoute = (handler: AppHandler) => {
5461
200,
5562
)
5663
} catch (error: any) {
57-
console.error('Logout error:', error)
5864
return ctx.json(
5965
{
6066
success: false,

src/routes/auth/profile.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const openRoute = createRoute({
3838
})
3939

4040
export const AuthProfileRoute = (handler: AppHandler) => {
41-
handler.use('/auth/profile', requireAuth)
41+
handler.use('/profile', requireAuth)
4242

4343
handler.openapi(openRoute, async ctx => {
4444
const user = ctx.get('user')
@@ -61,7 +61,6 @@ export const AuthProfileRoute = (handler: AppHandler) => {
6161
200,
6262
)
6363
} catch (error: any) {
64-
console.error('Profile error:', error)
6564
return ctx.json(
6665
{
6766
success: false,

src/routes/auth/register.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GenericResponses } from '~/lib/response-schemas'
55
import { getConnection } from '~/lib/db/connection'
66
import { user } from '~/lib/db/schema'
77
import { eq } from 'drizzle-orm'
8+
import { setCookie } from 'hono/cookie'
89

910
const 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,

src/routes/auth/update-profile.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const openRoute = createRoute({
6565
})
6666

6767
export const AuthUpdateProfileRoute = (handler: AppHandler) => {
68-
handler.use('/auth/profile', requireAuth)
68+
handler.use('/profile', requireAuth)
6969

7070
handler.openapi(openRoute, async ctx => {
7171
const { name, username, image } = ctx.req.valid('json')
@@ -101,8 +101,6 @@ export const AuthUpdateProfileRoute = (handler: AppHandler) => {
101101
.where(eq(user.id, currentUser.id))
102102
.returning()
103103

104-
// TODO: PLEASEE add R2 support to upload images. donm't forget
105-
106104
const updatedUser = updatedUsers[0]!
107105

108106
return ctx.json(

src/routes/user/save-asset.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import { getConnection } from '~/lib/db/connection'
77
import { eq, and } from 'drizzle-orm'
88
import { asset, savedAsset } from '~/lib/db/schema'
99

10-
const bodySchema = z.object({
11-
assetId: z.string().openapi({
10+
const paramsSchema = z.object({
11+
id: z.string().openapi({
12+
param: {
13+
name: 'id',
14+
in: 'path',
15+
},
1216
description: 'ID of the asset to save',
1317
example: 'asset_123',
1418
}),
@@ -27,19 +31,13 @@ const responseSchema = z.object({
2731
})
2832

2933
const openRoute = createRoute({
30-
path: '/saved-assets',
34+
path: '/saved-assets/{id}',
3135
method: 'post',
3236
summary: 'Save asset',
3337
description: "Save an asset to the current user's collection.",
3438
tags: ['User'],
3539
request: {
36-
body: {
37-
content: {
38-
'application/json': {
39-
schema: bodySchema,
40-
},
41-
},
42-
},
40+
params: paramsSchema,
4341
},
4442
responses: {
4543
201: {
@@ -55,10 +53,10 @@ const openRoute = createRoute({
5553
})
5654

5755
export const UserSaveAssetRoute = (handler: AppHandler) => {
58-
handler.use('/user/saved-assets', requireAuth)
56+
handler.use('/saved-assets/*', requireAuth)
5957

6058
handler.openapi(openRoute, async ctx => {
61-
const { assetId } = ctx.req.valid('json')
59+
const { id: assetId } = ctx.req.valid('param')
6260
const currentUser = ctx.get('user')
6361
const { drizzle } = getConnection(ctx.env)
6462

src/routes/user/saved-assets-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const openRoute = createRoute({
5252
})
5353

5454
export const UserSavedAssetsListRoute = (handler: AppHandler) => {
55-
handler.use('/user/saved-assets', requireAuth)
55+
handler.use('/saved-assets', requireAuth)
5656

5757
handler.openapi(openRoute, async ctx => {
5858
const currentUser = ctx.get('user')

src/routes/user/unsave-asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const openRoute = createRoute({
4747
})
4848

4949
export const UserUnsaveAssetRoute = (handler: AppHandler) => {
50-
handler.use('/user/saved-assets/*', requireAuth)
50+
handler.use('/saved-assets/*', requireAuth)
5151

5252
handler.openapi(openRoute, async ctx => {
5353
const { assetId } = ctx.req.valid('param')

src/scripts/db/seed.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ async function seed() {
123123
downloadCount: faker.number.int({ min: 0, max: 10000 }),
124124
viewCount: faker.number.int({ min: 0, max: 50000 }),
125125
hash: faker.string.alphanumeric(32),
126+
status: 'approved',
126127
isSuggestive: faker.datatype.boolean(),
127128
size: faker.number.int({ min: 100000, max: 10000000 }),
128129
extension: randomExtension,

0 commit comments

Comments
 (0)