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,5 +1,5 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { getUserProfile } from '../utils/api'
|
||||
import { trpc } from '../utils/trpc'
|
||||
|
||||
interface IProgress {
|
||||
quest: string
|
||||
@@ -23,7 +23,7 @@ const useUserProfile = defineStore('userProfile', {
|
||||
}),
|
||||
actions: {
|
||||
async loadUserProfile() {
|
||||
const res = await getUserProfile()
|
||||
const res = await trpc.users.getMyProfile.query()
|
||||
this.$patch(res)
|
||||
},
|
||||
async resetUserProfile() {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import axios from 'axios'
|
||||
import { GetSessionsRequest } from '@linux-odyssey/constants'
|
||||
import { Session } from '../types'
|
||||
import { UserProfile } from '../store/userProfile'
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: '/api/v1',
|
||||
@@ -12,11 +11,6 @@ const api = axios.create({
|
||||
})
|
||||
export default api
|
||||
|
||||
export async function getUserProfile(): Promise<UserProfile> {
|
||||
const res = await api.get<UserProfile>('/users/me')
|
||||
return res.data
|
||||
}
|
||||
|
||||
export async function createSession(questId: string): Promise<Session> {
|
||||
const res = await api.post<Session>('/sessions', { questId })
|
||||
return res.data
|
||||
|
||||
@@ -4,7 +4,7 @@ export { User, IUser } from './user.js'
|
||||
export { Quest, IQuest } from './quest.js'
|
||||
export { Session, ISession, ITask } from './session.js'
|
||||
export { Command, ICommand } from './command.js'
|
||||
export { UserProfile, IUserProfile } from './userProfile.js'
|
||||
export { UserProfile, IUserProfile, IProgress } from './userProfile.js'
|
||||
export { IFileCondition, ICondition } from './condition.js'
|
||||
export { IStage } from './stage.js'
|
||||
export { IRequirement } from './requirement.js'
|
||||
|
||||
@@ -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