added jwt token

This commit is contained in:
2024-04-19 18:18:07 +07:00
parent ada1af805a
commit 5eda99b443
3 changed files with 37 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ import { z } from "zod";
import { SQL, eq } from "drizzle-orm";
import { Config } from "./config";
import { TRPCError } from "@trpc/server";
import * as jwt from "jsonwebtoken";
const userInsertSchema = createInsertSchema(user);
const opinionInsertSchema = createInsertSchema(userOpinion)
@@ -157,7 +158,6 @@ async function requestOtp(phone: string) {
async function verifyOtp(token: string, pin: string) {
try {
console.log(token, pin);
let pt = await db.query.phoneToken.findFirst({
where: (pt, { eq }) => eq(pt.token, token),
orderBy: (pt, { desc }) => desc(pt.createdOn),
@@ -189,8 +189,10 @@ async function verifyOtp(token: string, pin: string) {
});
} else {
await db.delete(phoneToken).where(eq(phoneToken.phone, pt.phone));
console.log(rs, pt.phone);
return rs;
const token = jwt.sign({ phone: pt.phone }, Config.jwt_secret, {
expiresIn: "3d",
});
return token;
}
} catch (e) {
console.error(e);