Skip to content

Commit

Permalink
fix: rename user context to appUser
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Sep 24, 2023
1 parent 017df96 commit f779efd
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 24 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"db:drop": "drizzle-kit drop",
"pwa:generate-assets": "pwa-assets-generator --preset minimal public/logo.png",
"lint": "eslint --cache .",
"lint:fix": "eslint --cache --fix ."
"lint:fix": "eslint --cache --fix .",
"typecheck": "nuxi typecheck"
},
"devDependencies": {
"@antfu/eslint-config": "latest",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/api/auth/verify.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineEventHandler(async (event) => {
const result = await verifyRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
status: 400,
statusCode: 400,
statusMessage: 'Bad request',
})
}
Expand All @@ -21,7 +21,7 @@ export default defineEventHandler(async (event) => {

if (!user) {
throw createError({
status: 404,
statusCode: 404,
statusMessage: 'Not found',
})
}
Expand Down
2 changes: 1 addition & 1 deletion server/api/event/[id].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default defineProtectedEventHandler(async (event) => {

if (!result) {
throw createError({
status: 400,
statusCode: 400,
statusMessage: 'Bad request',
})
}
Expand Down
4 changes: 2 additions & 2 deletions server/api/event/[id].post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default defineProtectedEventHandler(async (event) => {
const result = await updateEventRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
status: 400,
statusCode: 400,
statusMessage: 'Bad request',
})
}
Expand All @@ -38,7 +38,7 @@ export default defineProtectedEventHandler(async (event) => {

if (updatedEvent.length > 1) {
throw createError({
status: 500,
statusCode: 500,
statusMessage: 'Internal server error',
})
}
Expand Down
2 changes: 1 addition & 1 deletion server/api/event/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineProtectedEventHandler(async (event) => {

if (!result) {
throw createError({
status: 400,
statusCode: 400,
statusMessage: 'Bad request',
})
}
Expand Down
4 changes: 2 additions & 2 deletions server/api/event/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineProtectedEventHandler(async (event) => {
const result = await createEventRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
status: 400,
statusCode: 400,
statusMessage: 'Bad request',
})
}
Expand All @@ -34,7 +34,7 @@ export default defineProtectedEventHandler(async (event) => {

if (createdEvent.length > 1) {
throw createError({
status: 500,
statusCode: 500,
statusMessage: 'Internal server error',
})
}
Expand Down
4 changes: 2 additions & 2 deletions server/api/user/[id].delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { eq } from 'drizzle-orm'
import { users } from '~/server/db/schema'

export default defineProtectedEventHandler(async (event) => {
if (event.context.user!.id !== event.context.params!.id) {
if (event.context.appUser!.id !== event.context.params!.id) {
throw createError({
status: 403,
statusCode: 403,
statusMessage: 'Forbidden',
})
}
Expand Down
4 changes: 2 additions & 2 deletions server/api/user/[id].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
export default defineProtectedEventHandler(async (event) => {
if (event.context.firebaseId !== event.context.params!.id) {
throw createError({
status: 403,
statusCode: 403,
statusMessage: 'Forbidden',
})
}

return event.context.user
return event.context.appUser
}, {
cache: {
maxAge: 5,
Expand Down
4 changes: 2 additions & 2 deletions server/api/user/[id].post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineProtectedEventHandler(async (event) => {
const result = await registerRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
status: 400,
statusCode: 400,
statusMessage: 'Bad request',
})
}
Expand All @@ -28,7 +28,7 @@ export default defineProtectedEventHandler(async (event) => {

if (updatedUsers.length > 1) {
throw createError({
status: 500,
statusCode: 500,
statusMessage: 'Internal server error',
})
}
Expand Down
2 changes: 1 addition & 1 deletion server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const bookings = sqliteTable('bookings', {
id: text('id').primaryKey().$defaultFn(() => createId()),
userId: text('user_id').notNull().references(() => users.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
slotId: text('slot_id').notNull().references(() => slots.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
status: text('status', {
statusCode: text('status', {
enum: [
'pending',
'cancelled',
Expand Down
10 changes: 5 additions & 5 deletions server/utils/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { User } from '~/shared/types'

declare module 'h3' {
interface H3EventContext {
user?: User
appUser?: User // Vuefire is using `user` property so we have to use appUser
firebaseId?: string
}
}
Expand All @@ -34,7 +34,7 @@ export function defineProtectedEventHandler<T extends EventHandlerRequest, D>(

if (!authorization.startsWith('Bearer ')) {
throw createError({
status: 403,
statusCode: 403,
statusMessage: 'Unauthorized',
})
}
Expand All @@ -57,21 +57,21 @@ export function defineProtectedEventHandler<T extends EventHandlerRequest, D>(

if (user === null) {
throw createError({
status: 401,
statusCode: 401,
statusMessage: 'Unauthorized',
})
}

if (options.restrictTo) {
if (!user?.memberType || !options.restrictTo.includes(user.memberType)) {
throw createError({
status: 403,
statusCode: 403,
statusMessage: 'Forbidden',
})
}
}

event.context.user = user
event.context.appUser = user
event.context.firebaseId = sub

return handler(event)
Expand Down

0 comments on commit f779efd

Please # to comment.