From 6b80ddd5e6666050889b24f7981427538772fe45 Mon Sep 17 00:00:00 2001 From: Thanu Poptiphueng Date: Fri, 19 Apr 2024 18:46:25 +0700 Subject: [PATCH] add more detail to user --- src/config.ts | 1 + src/schema.ts | 50 +++++++++++++++++++++++++++++++----------------- src/userRoute.ts | 6 ++++-- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/config.ts b/src/config.ts index 3b3e0cd..db2a370 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,4 +5,5 @@ export const Config = { sms_api_verify_endpoint: "https://otp.thaibulksms.com/v2/otp/verify", jwt_secret: "T4kE6/tIqCVEZYg9lwsqeJjYfOoXTXSXDEMyParsJjj57CjSdkrfPOLWP74/9lJpcBA=", + token_duration: "365d", }; diff --git a/src/schema.ts b/src/schema.ts index be3f6ad..638cd91 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -4,28 +4,42 @@ import { integer, primaryKey, unique, + index, } from "drizzle-orm/sqlite-core"; import { relations, sql } from "drizzle-orm"; //----------------User -export const user = sqliteTable("users", { - id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), - firstName: text("firstName").notNull(), - lastName: text("lastName").notNull(), - title: text("title").notNull(), - phone: text("phone").unique().notNull(), - email: text("email"), - job: text("job").notNull(), - education: text("education").notNull(), - vision: text("vision"), - reason: text("reason"), - group: integer("group_id") - .references(() => group.id) - .notNull(), - zone: integer("zone_id") - .notNull() - .references(() => zone.id), -}); +export const user = sqliteTable( + "users", + { + id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }), + firstName: text("firstName").notNull(), + lastName: text("lastName").notNull(), + title: text("title").notNull(), + cid: text("cid", { length: 13 }).notNull().unique(), + age: integer("age").notNull(), + phone: text("phone").unique().notNull(), + public_phone: text("public_phone"), + facebook: text("facebook"), + twitter: text("twitter"), + tiktok: text("tiktok"), + otherSocial: text("other_social"), + email: text("email"), + job: text("job").notNull(), + education: text("education").notNull(), + vision: text("vision"), + reason: text("reason"), + group: integer("group_id") + .references(() => group.id) + .notNull(), + zone: integer("zone_id") + .notNull() + .references(() => zone.id), + }, + (t) => ({ + phone_idx: index("phone_idx").on(t.phone), + }) +); export const userRelation = relations(user, ({ many, one }) => ({ opinions: many(userOpinion), diff --git a/src/userRoute.ts b/src/userRoute.ts index 8df05d2..13fb0fa 100644 --- a/src/userRoute.ts +++ b/src/userRoute.ts @@ -13,7 +13,9 @@ import { Config } from "./config"; import { TRPCError } from "@trpc/server"; import * as jwt from "jsonwebtoken"; -const userInsertSchema = createInsertSchema(user); +const userInsertSchema = createInsertSchema(user, { + cid: (schema) => schema.cid.length(13), +}); const opinionInsertSchema = createInsertSchema(userOpinion) .omit({ userId: true, @@ -190,7 +192,7 @@ async function verifyOtp(token: string, pin: string) { } else { await db.delete(phoneToken).where(eq(phoneToken.phone, pt.phone)); const token = jwt.sign({ phone: pt.phone }, Config.jwt_secret, { - expiresIn: "3d", + expiresIn: "365d", }); return token; }