Skip to content

Commit

Permalink
feat: register endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Sep 19, 2023
1 parent 3c6f425 commit 6a44e9c
Show file tree
Hide file tree
Showing 15 changed files with 1,318 additions and 9 deletions.
4 changes: 4 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default defineNuxtConfig({
'nuxt-vuefire',
],

css: [
'framework7/framework7-bundle.min.css',
],

vuefire: {
emulators: false,

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"drizzle-orm": "^0.28.6",
"eslint": "^8.49.0",
"firebase": "^10.4.0",
"framework7": "^8.3.0",
"framework7-vue": "^8.3.0",
"nuxt": "^3.7.3",
"nuxt-vuefire": "^0.3.0",
"vuefire": "^3.1.17",
"web-auth-library": "^1.0.3"
"web-auth-library": "^1.0.3",
"zod": "^3.22.2"
}
}
51 changes: 51 additions & 0 deletions pnpm-lock.yaml

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

32 changes: 32 additions & 0 deletions server/api/auth/register.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { eq } from 'drizzle-orm'
import { z } from 'zod'
import { users } from '~/server/db/schema'

const registerRequestBody = z.object({
firebaseId: z.string().nonempty(),
email: z.string().email().optional(),
id: z.string().cuid2().optional(),
}).refine(val => !val.email && !val.id, 'Either `email` or `id` must be provided.')

export default defineProtectedEventHandler(async (event) => {
const result = await registerRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
status: 400,
statusMessage: 'Bad request',
})
}

const { data } = result

const query = data.id ? eq(users.id, data.id) : eq(users.email, data.email!)

const user = await event.context.database.update(users)
.set({
firebaseId: data.firebaseId,
})
.where(query)
.returning()

return user
})
4 changes: 1 addition & 3 deletions server/api/auth/verify.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const verifyRequestBody = z.object({
email: z.string().email(),
})

export default defineCachedEventHandler(async (event) => {
export default defineProtectedEventHandler(async (event) => {
const result = await verifyRequestBody.safeParseAsync(await readBody(event))
if (!result.success) {
throw createError({
Expand All @@ -27,6 +27,4 @@ export default defineCachedEventHandler(async (event) => {
}

return { ok: true }
}, {
maxAge: 60 * 60 * 24,
})
2 changes: 1 addition & 1 deletion server/api/event/[id].get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from 'dayjs'

export default defineEventHandler(event => ({
export default defineProtectedEventHandler(event => ({
id: event.context.params!.id,
name: 'SST Homecoming 2024',
description: 'SST Homecoming 2024',
Expand Down
2 changes: 1 addition & 1 deletion server/api/user/[id].get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default defineEventHandler(event => ({
export default defineProtectedEventHandler(event => ({
id: event.context.params!.id,
name: 'Qin Guan',
memberId: 'ABC-1',
Expand Down
7 changes: 4 additions & 3 deletions server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { createId } from '@paralleldrive/cuid2'
// Tables

export const users = sqliteTable('users', {
id: text('id').primaryKey(), // Use Firebase Auth provided ID as SSOT
name: text('name').notNull(), // Ignore Firebase Auth provided values and force user to provide their own
email: text('email').notNull(),
id: text('id').primaryKey().$defaultFn(() => createId()),
memberId: text('member_id').notNull().unique(), // Member ID for tracking by SSTAA Admin
firebaseId: text('firebase_id').notNull().unique(), // Use Firebase Auth provided ID as SSOT
name: text('name').notNull(), // Ignore Firebase Auth provided values and force user to provide their own
email: text('email').notNull().unique(),
graduationYear: integer('graduation_year').notNull(),
memberType: text('member_type', {
enum: [
Expand Down
1 change: 1 addition & 0 deletions server/drizzle/0002_peaceful_phil_sheldon.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX `users_id_unique` ON `users` (`id`);
3 changes: 3 additions & 0 deletions server/drizzle/0003_young_pyro.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP INDEX IF EXISTS `users_id_unique`;--> statement-breakpoint
ALTER TABLE users ADD `firebase_id` text NOT NULL;--> statement-breakpoint
CREATE UNIQUE INDEX `users_firebase_id_unique` ON `users` (`firebase_id`);
1 change: 1 addition & 0 deletions server/drizzle/0004_special_tenebrous.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);
Loading

0 comments on commit 6a44e9c

Please # to comment.