2022-11-10 02:11:36 -05:00
|
|
|
// user.get("/self", jwt, async (req: UserJwtRequest, res, next) => {
|
|
|
|
// const error = () =>
|
|
|
|
// res.status(401).json({
|
|
|
|
// message: "Unauthorized"
|
|
|
|
// })
|
|
|
|
|
|
|
|
import { USER_COOKIE_NAME } from "@lib/constants"
|
2022-11-12 19:06:23 -05:00
|
|
|
import { getUserById } from "@lib/server/prisma"
|
2022-11-10 02:11:36 -05:00
|
|
|
import { getCookie } from "cookies-next"
|
|
|
|
import { NextApiRequest, NextApiResponse } from "next"
|
|
|
|
|
|
|
|
// try {
|
|
|
|
// if (!req.user) {
|
|
|
|
// return error()
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const user = await User.findByPk(req.user?.id, {
|
|
|
|
// attributes: {
|
|
|
|
// exclude: ["password"]
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// if (!user) {
|
|
|
|
// return error()
|
|
|
|
// }
|
|
|
|
// res.json(user)
|
|
|
|
// } catch (error) {
|
|
|
|
// next(error)
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
|
|
|
|
export default async function handler(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse
|
|
|
|
): Promise<any> {
|
|
|
|
const error = () =>
|
|
|
|
res.status(401).json({
|
|
|
|
message: "Unauthorized"
|
|
|
|
})
|
|
|
|
|
2022-11-12 19:06:23 -05:00
|
|
|
const userId = String(
|
|
|
|
getCookie(USER_COOKIE_NAME, {
|
|
|
|
req,
|
|
|
|
res
|
|
|
|
})
|
|
|
|
)
|
2022-11-10 02:11:36 -05:00
|
|
|
|
2022-11-12 19:06:23 -05:00
|
|
|
if (!userId) {
|
|
|
|
return error()
|
|
|
|
}
|
2022-11-10 02:11:36 -05:00
|
|
|
|
|
|
|
try {
|
2022-11-12 19:06:23 -05:00
|
|
|
const user = await getUserById(userId)
|
|
|
|
|
2022-11-10 02:11:36 -05:00
|
|
|
if (!user) {
|
|
|
|
return error()
|
|
|
|
}
|
|
|
|
return res.json(user)
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(`/api/user/self:`, e)
|
|
|
|
return error()
|
|
|
|
}
|
|
|
|
}
|