Skip to content

Commit

Permalink
wip(idp): fixing various issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Dec 14, 2024
1 parent 03df7fa commit 9765233
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
9 changes: 3 additions & 6 deletions idp/src/account/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ router.post(
async (c) => {
const body = c.req.valid('json') as AccountResetPasswordOptions
await resetPassword(body)
c.status(200)
return c.body(null)
return c.body(null, 200)
},
)

Expand All @@ -82,8 +81,7 @@ router.post(
async (c) => {
const body = c.req.valid('json') as AccountConfirmEmailOptions
await confirmEmail(body)
c.status(200)
return c.body(null)
return c.body(null, 200)
},
)

Expand All @@ -97,8 +95,7 @@ router.post(
async (c) => {
const body = c.req.valid('json') as AccountSendResetPasswordEmailOptions
await sendResetPasswordEmail(body)
c.status(204)
return c.body(null)
return c.body(null, 204)
},
)

Expand Down
22 changes: 13 additions & 9 deletions idp/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import '@/infra/env.ts'
import { Hono } from 'hono'
import { jwt } from 'hono/jwt'
import { logger } from 'hono/logger'
import accountRouter from '@/account/router.ts'
import { getConfig } from '@/config/config.ts'
import healthRouter from '@/health/router.ts'
Expand All @@ -29,6 +30,8 @@ import { User } from '@/user/model.ts'

const app = new Hono()

app.use(logger())

app.onError((error, c) => {
if (error.message === 'Unauthorized') {
return c.json(
Expand Down Expand Up @@ -66,15 +69,16 @@ app.use('*', async (c, next) => {

app.use('/v3/*', async (c, next) => {
const jwtMiddleware = jwt({ secret: getConfig().token.jwtSigningKey })
switch (c.req.path) {
case '/v3/token':
case '/v3/users/me/picture:extension':
case '/v3/health':
case '/v3/accounts':
case '/version':
return await next()
default:
return await jwtMiddleware(c, next)
if (
c.req.path.startsWith('/v3/users/me/picture') ||
c.req.path === '/v3/token' ||
c.req.path === '/v3/health' ||
c.req.path === '/v3/accounts' ||
c.req.path === '/version'
) {
return await next()
} else {
return await jwtMiddleware(c, next)
}
})

Expand Down
6 changes: 2 additions & 4 deletions idp/src/health/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ const router = new Hono()

router.get('', async (c) => {
if (!postgres.connected) {
c.status(503)
return c.body(null)
return c.body(null, 503)
}
if (!(await meilisearch.isHealthy())) {
c.status(503)
return c.body(null)
return c.body(null, 503)
}
return c.text('OK')
})
Expand Down
31 changes: 15 additions & 16 deletions idp/src/user/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,34 @@ import {
UserUpdateFullNameOptions,
UserUpdatePasswordOptions,
} from '@/user/service.ts'
import { extname, join } from 'node:path'
import { basename, extname, join } from 'node:path'
import { Buffer } from 'node:buffer'
import { UserListOptions } from '@/user/service.ts'

const router = new Hono()

router.get('/me', async (c) => {
c.json(await getUser(c.get('user').id))
return c.json(await getUser(c.get('user').id))
})

router.get('/me/picture:extension', async (c) => {
router.get('/me/:filename', async (c) => {
const { filename } = c.req.param()
if (basename(filename, extname(filename)) !== 'picture') {
return c.body(null, 404)
}
const accessToken = c.req.query('access_token')
if (!accessToken) {
throw newMissingQueryParamError('access_token')
}
const userId = await getUserIdFromAccessToken(accessToken)
const { buffer, extension, mime } = await getUserPicture(userId)
if (extension !== c.req.param('extension')) {
if (extension !== extname(c.req.param('filename'))) {
throw newPictureNotFoundError()
}
c.res.headers.append(
'Content-Disposition',
`attachment; filename=picture.${extension}`,
)
c.res.headers.append('Content-Type', mime)
return c.body(buffer)
return c.body(buffer, 200, {
'Content-Type': mime,
'Content-Disposition': `attachment; filename=picture${extension}`,
})
})

router.post(
Expand Down Expand Up @@ -182,8 +184,7 @@ router.delete(
async (c) => {
const body = c.req.valid('json') as UserDeleteOptions
await deleteUser(c.get('user').id, body)
c.status(204)
return c.body(null)
return c.body(null, 204)
},
)

Expand Down Expand Up @@ -221,8 +222,7 @@ router.post(
const { id } = c.req.param()
const body = c.req.valid('json') as UserSuspendOptions
await suspendUser(id, body)
c.status(200)
return c.body(null)
return c.body(null, 200)
},
)

Expand All @@ -238,8 +238,7 @@ router.post(
const { id } = c.req.param()
const body = c.req.valid('json') as UserMakeAdminOptions
await makeAdminUser(id, body)
c.status(200)
return c.body(null)
return c.body(null, 200)
},
)

Expand Down

0 comments on commit 9765233

Please sign in to comment.