halfway commit to allow collaboration
This commit is contained in:
@@ -104,7 +104,7 @@ model Message {
|
||||
User User @relation(fields: [userId], references: [id])
|
||||
userId String
|
||||
deleted Boolean
|
||||
text String
|
||||
text String @db.VarChar(2000)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
replies Reply? @relation("MessageToReply")
|
||||
@@ -114,7 +114,7 @@ model Message {
|
||||
}
|
||||
|
||||
model Reply {
|
||||
message Message @relation("MessageToReply", fields: [messageId], references: [id]) //message text
|
||||
message Message @relation("MessageToReply", fields: [messageId], references: [id]) //message text
|
||||
messageId String @unique //message id of the reply
|
||||
repliesTo Message @relation("ReplyToMessage", fields: [repliesToId], references: [id]) //message id that this message replies to
|
||||
repliesToId String @unique //replies to this message id
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
getAllUsersFrom,
|
||||
getUserInformation,
|
||||
createUser,
|
||||
getUserId,
|
||||
} from "../services/userService";
|
||||
import { CreateUserInput } from "../validators/userValidator";
|
||||
|
||||
@@ -16,3 +17,7 @@ export async function fetchAllUsers(instanceId: string) {
|
||||
export async function createNewUser(data: CreateUserInput) {
|
||||
return await createUser(data);
|
||||
}
|
||||
|
||||
export async function fetchUserId(username: string) {
|
||||
return await getUserId(username);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { zValidator } from "@hono/zod-validator";
|
||||
import { describeRoute, resolver } from "hono-openapi";
|
||||
import {
|
||||
getUserCredentials,
|
||||
getUserId,
|
||||
getUserInformation,
|
||||
} from "../services/userService";
|
||||
import shaHash from "../helper/hashing";
|
||||
@@ -45,6 +46,7 @@ authRoutes.post(
|
||||
async (c) => {
|
||||
try {
|
||||
const { username, password } = await c.req.json();
|
||||
console.log(c.req.json);
|
||||
|
||||
// Find user by username
|
||||
const user = await prisma.user.findFirst({
|
||||
@@ -55,21 +57,29 @@ authRoutes.post(
|
||||
return c.json({ error: "Invalid username or password" }, 401);
|
||||
}
|
||||
|
||||
// Get user credentials
|
||||
const userCredentials = await getUserCredentials(user.id);
|
||||
// get userId
|
||||
const userIdResult = await getUserId(username);
|
||||
if (!userIdResult) {
|
||||
return c.json({ error: "Invalid username or password" }, 401);
|
||||
}
|
||||
|
||||
const userId = userIdResult.userId;
|
||||
|
||||
// get user creds
|
||||
const userCredentials = await getUserCredentials(userId);
|
||||
if (!userCredentials) {
|
||||
return c.json({ error: "Invalid username or password" }, 401);
|
||||
}
|
||||
|
||||
// Hash
|
||||
// const hashedPassword = shaHash(password, user.id);
|
||||
// hash the provided password with user ID as salt
|
||||
const hashedPassword = shaHash(password, userId);
|
||||
|
||||
// Verify password
|
||||
if (password !== userCredentials.password) {
|
||||
// verify password
|
||||
if (hashedPassword !== userCredentials.password) {
|
||||
return c.json({ error: "Invalid username or password" }, 401);
|
||||
}
|
||||
|
||||
// Generate new token
|
||||
// generate new token
|
||||
const token = crypto.randomUUID();
|
||||
|
||||
// Update user's token in database
|
||||
@@ -78,7 +88,7 @@ authRoutes.post(
|
||||
data: { token: token },
|
||||
});
|
||||
|
||||
// Get full user information
|
||||
// get full user information
|
||||
const userInfo = await getUserInformation(user.id);
|
||||
if (!userInfo) {
|
||||
return c.json({ error: "Failed to get user information" }, 500);
|
||||
|
||||
@@ -3,16 +3,49 @@ import {
|
||||
fetchAllUsers,
|
||||
fetchUserData,
|
||||
createNewUser,
|
||||
fetchUserId,
|
||||
} from "../controller/userController";
|
||||
import {
|
||||
createUserSchema,
|
||||
queryAllUsersByInstanceId,
|
||||
queryUserByIdSchema,
|
||||
queryUserByUsernameSchema,
|
||||
} from "../validators/userValidator";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { describeRoute, resolver } from "hono-openapi";
|
||||
const userRoutes = new Hono();
|
||||
|
||||
userRoutes.get(
|
||||
"/username/:username",
|
||||
describeRoute({
|
||||
description: "Get userId by username",
|
||||
responses: {
|
||||
200: {
|
||||
description: "Success getting userId",
|
||||
content: {
|
||||
"application/json": { schema: resolver(queryUserByUsernameSchema) },
|
||||
},
|
||||
},
|
||||
404: {
|
||||
description: "userId not found",
|
||||
content: {
|
||||
"application/json": { schema: resolver(queryUserByUsernameSchema) },
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
zValidator("param", queryUserByUsernameSchema),
|
||||
async (c) => {
|
||||
const username = c.req.param("username");
|
||||
const userId = await fetchUserId(username);
|
||||
if (userId) {
|
||||
return c.json(userId);
|
||||
} else {
|
||||
return c.json({ error: "User not found" }, 404);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
userRoutes.get(
|
||||
"/:id",
|
||||
describeRoute({
|
||||
|
||||
@@ -64,6 +64,39 @@ export async function createUser(data: CreateUserInput): Promise<{
|
||||
return userData;
|
||||
}
|
||||
|
||||
export async function getUserId(
|
||||
username: string,
|
||||
): Promise<{ userId: string } | null> {
|
||||
try {
|
||||
if (!username) throw new Error("missing username");
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
username: username,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) throw new Error("could not find user");
|
||||
|
||||
return {
|
||||
userId: user.id,
|
||||
};
|
||||
} catch (err) {
|
||||
const errMessage = err as Error;
|
||||
|
||||
if (errMessage.message === "missing username") {
|
||||
console.log("services::actions::getUserId - no username given");
|
||||
return null;
|
||||
}
|
||||
if (errMessage.message === "could not find user") {
|
||||
console.log("services::actions::getUserId - unable to find user");
|
||||
return null;
|
||||
}
|
||||
console.log("services::actions::getUserId - unknown error");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUserCredentials(userId: string): Promise<{
|
||||
userId: string;
|
||||
password: string;
|
||||
|
||||
@@ -4,6 +4,10 @@ export const queryUserByIdSchema = z.object({
|
||||
id: z.uuidv7(),
|
||||
});
|
||||
|
||||
export const queryUserByUsernameSchema = z.object({
|
||||
username: z.string().min(3).max(30),
|
||||
});
|
||||
|
||||
export const queryAllUsersByInstanceId = z.object({
|
||||
instanceId: z.uuidv7(),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user