mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: migreate get user profile to trpc
This commit is contained in:
1 parent
ac92950177
commit
423548804a
11 files changed
+54
-62
No files matched your search
@@ -1,20 +0,0 @@
|
||||
import type { Express, Request, Response } from 'express'
|
||||
import { UserProfile } from '@linux-odyssey/models'
|
||||
import { asyncHandler } from '../../middleware/error.js'
|
||||
|
||||
export const me = asyncHandler(async (req: Request, res: Response) => {
|
||||
const { id, username, email } = req.user as Express.ExistingUser
|
||||
|
||||
const userProfile = await UserProfile.findOne({
|
||||
user: id,
|
||||
})
|
||||
if (!userProfile) {
|
||||
throw new Error(`UserProfile not found for user ${username} ${id}!`)
|
||||
}
|
||||
const { progress } = userProfile
|
||||
res.json({
|
||||
username,
|
||||
email,
|
||||
progress,
|
||||
})
|
||||
})
|
||||
@@ -4,7 +4,6 @@ import sessions from './sessionRouter.js'
|
||||
import commands from './commandRouter.js'
|
||||
import authRouter from './authRouter.js'
|
||||
import authRequired from '../../middleware/authRequired.js'
|
||||
import userRouter from './userRouter.js'
|
||||
import config from '../../config.js'
|
||||
import leaderboardController from '../controllers/leaderboardController.js'
|
||||
|
||||
@@ -28,24 +27,6 @@ router.use(
|
||||
router.use('/auth', authRouter)
|
||||
router.use('/sessions', authRequired, sessions)
|
||||
|
||||
router.use('/users', userRouter)
|
||||
|
||||
router.get('/survey', (req, res) => {
|
||||
if (!config.surveyUrl) {
|
||||
res.status(404).send('Survey not configured')
|
||||
return
|
||||
}
|
||||
res.redirect(config.surveyUrl)
|
||||
})
|
||||
|
||||
router.get('/bug-report', (req, res) => {
|
||||
if (!config.bugReportUrl) {
|
||||
res.status(404).send('Bug report not configured')
|
||||
return
|
||||
}
|
||||
res.redirect(config.bugReportUrl)
|
||||
})
|
||||
|
||||
router.get('/leaderboard', leaderboardController)
|
||||
|
||||
export default router
|
||||
@@ -1,9 +0,0 @@
|
||||
import express from 'express'
|
||||
import { me } from '../controllers/userController.js'
|
||||
import authRequired from '../../middleware/authRequired.js'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/me', authRequired, me)
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,7 @@
|
||||
import { check } from 'express-validator'
|
||||
import { noError } from '../../middleware/validator.js'
|
||||
|
||||
export const checkQuestId = () =>
|
||||
check('questId').notEmpty().isString().isLowercase().isLength({ max: 64 })
|
||||
|
||||
export const questIdValidator = [checkQuestId(), noError]
|
||||
@@ -1,4 +1,5 @@
|
||||
import { check } from 'express-validator'
|
||||
import { checkQuestId } from './questValidator.js'
|
||||
import { noError } from '../../middleware/validator.js'
|
||||
|
||||
const checkSessionStatus = () =>
|
||||
@@ -7,7 +8,7 @@ const checkSessionStatus = () =>
|
||||
export const checkSessionId = () =>
|
||||
check('sessionId').notEmpty().isString().isMongoId()
|
||||
|
||||
export const sessionListValidator = [checkSessionStatus()]
|
||||
export const sessionListValidator = [checkQuestId(), checkSessionStatus()]
|
||||
export const sessionDetailValidator = [checkSessionId()]
|
||||
export const createSessionValidator = [noError]
|
||||
export const activeSessionValidator = [noError]
|
||||
export const createSessionValidator = [checkQuestId(), noError]
|
||||
export const activeSessionValidator = [checkQuestId(), noError]
|
||||
@@ -1,6 +1,7 @@
|
||||
import { router, publicProcedure } from '../trpc.js'
|
||||
import { linkRouter } from './linkRouter.js'
|
||||
import { questRouter } from './questRouter.js'
|
||||
import { userRouter } from './userRouter.js'
|
||||
|
||||
export const appRouter = router({
|
||||
userList: publicProcedure.query(async (opts) => {
|
||||
@@ -9,6 +10,7 @@ export const appRouter = router({
|
||||
}),
|
||||
links: linkRouter,
|
||||
quests: questRouter,
|
||||
users: userRouter,
|
||||
})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
@@ -0,0 +1,25 @@
|
||||
import { UserProfile } from '@linux-odyssey/models'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { protectedProcedure, router } from '../trpc.js'
|
||||
|
||||
export const userRouter = router({
|
||||
getMyProfile: protectedProcedure.query(async (opts) => {
|
||||
const { id, username, email } = opts.ctx.user
|
||||
|
||||
const userProfile = await UserProfile.findOne({
|
||||
user: id,
|
||||
})
|
||||
if (!userProfile) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: `UserProfile not found for user ${username} ${id}!`,
|
||||
})
|
||||
}
|
||||
const { progress } = userProfile
|
||||
return {
|
||||
username,
|
||||
email,
|
||||
progress,
|
||||
}
|
||||
}),
|
||||
})
|
||||
+13
-2
@@ -1,12 +1,13 @@
|
||||
import { initTRPC } from '@trpc/server'
|
||||
import { initTRPC, TRPCError } from '@trpc/server'
|
||||
import { CreateNextContextOptions } from '@trpc/server/adapters/next'
|
||||
|
||||
export const createContext = async (opts: CreateNextContextOptions) => {
|
||||
const { session, user, ip } = opts.req
|
||||
const { session, user, ip, isAuthenticated } = opts.req
|
||||
return {
|
||||
session,
|
||||
user,
|
||||
ip,
|
||||
isAuthenticated,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,3 +19,13 @@ const t = initTRPC.context<AppContext>().create()
|
||||
* that can be used throughout the router
|
||||
*/
|
||||
export const { router, procedure: publicProcedure } = t
|
||||
|
||||
export const protectedProcedure = publicProcedure.use(async (opts) => {
|
||||
if (opts.ctx.isAuthenticated() && !('newUser' in opts.ctx.user)) {
|
||||
return opts.next()
|
||||
}
|
||||
throw new TRPCError({
|
||||
code: 'UNAUTHORIZED',
|
||||
message: 'Login required',
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user