Add services and auth
This commit is contained in:
parent
9b0b5dc040
commit
5dec454afb
46 changed files with 900 additions and 31 deletions
53
src/controllers/user/user.ts
Normal file
53
src/controllers/user/user.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { type FastifyReply, type FastifyRequest } from "fastify";
|
||||
import type {
|
||||
IUserParams,
|
||||
IUserResponseError,
|
||||
IUserResponseSuccess,
|
||||
ISessionsResponseError,
|
||||
ISessionsResponseSuccess,
|
||||
} from "./types.js";
|
||||
import { getUserById, getUserSessionsById } from "../../services/user/user.js";
|
||||
|
||||
const getUser = async (request: FastifyRequest, _reply: FastifyReply) => {
|
||||
const { id } = request.params as IUserParams;
|
||||
|
||||
const user = await getUserById(id);
|
||||
if (!user) {
|
||||
return {
|
||||
id: id,
|
||||
error: "user does not exist",
|
||||
} as IUserResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
description: user.description,
|
||||
admin: user.admin,
|
||||
registerDate: user.registerDate.getTime(),
|
||||
lastLogin: user.lastLogin?.getTime() ?? 0,
|
||||
} as IUserResponseSuccess;
|
||||
};
|
||||
|
||||
const getSessions = async (request: FastifyRequest, _reply: FastifyReply) => {
|
||||
const { id } = request.params as IUserParams;
|
||||
const authHeader = request.headers["authorization"];
|
||||
|
||||
const sessions = await getUserSessionsById(id, authHeader);
|
||||
if (!sessions) {
|
||||
return {
|
||||
id: id,
|
||||
error: "user does not exist or you have no access",
|
||||
} as ISessionsResponseError;
|
||||
}
|
||||
|
||||
return {
|
||||
sessions: sessions.map((session) => ({
|
||||
id: session.id,
|
||||
userId: session.userId,
|
||||
})),
|
||||
} as ISessionsResponseSuccess;
|
||||
};
|
||||
|
||||
export { getUser, getSessions };
|
||||
Loading…
Add table
Add a link
Reference in a new issue