2024-08-23 02:26:22 +00:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
|
import { inject, injectable } from 'tsyringe'
|
|
|
|
|
import { setCookie } from 'hono/cookie'
|
|
|
|
|
import { zValidator } from '@hono/zod-validator'
|
|
|
|
|
import type { HonoTypes } from '../types'
|
|
|
|
|
import { requireAuth } from '../middleware/auth.middleware'
|
|
|
|
|
import type { Controller } from '$lib/server/api/interfaces/controller.interface'
|
|
|
|
|
import { IamService } from '$lib/server/api/services/iam.service'
|
|
|
|
|
import { LuciaProvider } from '$lib/server/api/providers'
|
|
|
|
|
import { limiter } from '$lib/server/api/middleware/rate-limiter.middleware'
|
|
|
|
|
import { updateProfileDto } from '$lib/dtos/update-profile.dto'
|
|
|
|
|
import { updateEmailDto } from '$lib/dtos/update-email.dto'
|
|
|
|
|
import { StatusCodes } from '$lib/constants/status-codes'
|
2024-07-21 19:05:48 +00:00
|
|
|
|
2024-07-31 01:50:46 +00:00
|
|
|
@injectable()
|
|
|
|
|
export class IamController implements Controller {
|
2024-08-23 02:26:22 +00:00
|
|
|
controller = new Hono<HonoTypes>()
|
2024-07-21 19:05:48 +00:00
|
|
|
|
2024-07-31 01:50:46 +00:00
|
|
|
constructor(
|
2024-08-23 02:26:22 +00:00
|
|
|
@inject(IamService) private readonly iamService: IamService,
|
|
|
|
|
@inject(LuciaProvider) private lucia: LuciaProvider,
|
|
|
|
|
) {}
|
2024-07-31 01:50:46 +00:00
|
|
|
|
|
|
|
|
routes() {
|
|
|
|
|
return this.controller
|
2024-08-23 02:26:22 +00:00
|
|
|
.get('/', requireAuth, async (c) => {
|
|
|
|
|
const user = c.var.user
|
|
|
|
|
return c.json({ user })
|
2024-08-15 23:46:58 +00:00
|
|
|
})
|
2024-08-23 02:26:22 +00:00
|
|
|
.put('/update/profile', requireAuth, zValidator('json', updateProfileDto), limiter({ limit: 30, minutes: 60 }), async (c) => {
|
|
|
|
|
const user = c.var.user
|
|
|
|
|
const { firstName, lastName, username } = c.req.valid('json')
|
|
|
|
|
const updatedUser = await this.iamService.updateProfile(user.id, { firstName, lastName, username })
|
|
|
|
|
if (!updatedUser) {
|
|
|
|
|
return c.json("Username already in use", StatusCodes.BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
return c.json({ user: updatedUser }, StatusCodes.OK)
|
2024-08-19 03:28:15 +00:00
|
|
|
})
|
|
|
|
|
.post('/update/email', requireAuth, zValidator('json', updateEmailDto), limiter({ limit: 10, minutes: 60 }), async (c) => {
|
2024-08-23 02:26:22 +00:00
|
|
|
const user = c.var.user
|
|
|
|
|
const { email } = c.req.valid('json')
|
|
|
|
|
const updatedUser = await this.iamService.updateEmail(user.id, { email })
|
|
|
|
|
if (!updatedUser) {
|
|
|
|
|
return c.json("Email already in use", StatusCodes.BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
return c.json({ user: updatedUser }, StatusCodes.OK)
|
2024-08-19 03:28:15 +00:00
|
|
|
})
|
2024-08-15 23:46:58 +00:00
|
|
|
.post('/logout', requireAuth, async (c) => {
|
2024-08-23 02:26:22 +00:00
|
|
|
const sessionId = c.var.session.id
|
|
|
|
|
await this.iamService.logout(sessionId)
|
|
|
|
|
const sessionCookie = this.lucia.createBlankSessionCookie()
|
2024-08-15 23:46:58 +00:00
|
|
|
setCookie(c, sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
path: sessionCookie.attributes.path,
|
|
|
|
|
maxAge: sessionCookie.attributes.maxAge,
|
|
|
|
|
domain: sessionCookie.attributes.domain,
|
|
|
|
|
sameSite: sessionCookie.attributes.sameSite as any,
|
|
|
|
|
secure: sessionCookie.attributes.secure,
|
|
|
|
|
httpOnly: sessionCookie.attributes.httpOnly,
|
2024-08-23 02:26:22 +00:00
|
|
|
expires: sessionCookie.attributes.expires,
|
|
|
|
|
})
|
|
|
|
|
return c.json({ status: 'success' })
|
|
|
|
|
})
|
2024-07-31 01:50:46 +00:00
|
|
|
}
|
|
|
|
|
}
|