From c912319967edebe56934dcc3366b654d69651507 Mon Sep 17 00:00:00 2001 From: wancat Date: Mon, 19 Feb 2024 23:51:21 +0800 Subject: [PATCH] refactor: userController and add req user type --- .../{userController.js => userController.ts} | 12 ++++----- server/src/auth/oauthVerify.ts | 12 ++++----- server/src/auth/passport.ts | 4 +-- server/src/auth/passwordStrategy.ts | 6 ++++- server/src/game/commandHandler.ts | 4 --- server/src/middleware/authRequired.ts | 2 +- server/src/models/userManager.ts | 7 ++++-- server/src/types/express.d.ts | 25 +++++++++++++++++++ 8 files changed, 49 insertions(+), 23 deletions(-) rename server/src/api/controllers/{userController.js => userController.ts} (50%) create mode 100644 server/src/types/express.d.ts diff --git a/server/src/api/controllers/userController.js b/server/src/api/controllers/userController.ts similarity index 50% rename from server/src/api/controllers/userController.js rename to server/src/api/controllers/userController.ts index b819c96..220cf2c 100644 --- a/server/src/api/controllers/userController.js +++ b/server/src/api/controllers/userController.ts @@ -1,16 +1,16 @@ +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, res) => { +export const me = asyncHandler(async (req: Request, res: Response) => { + const { id, username, email } = req.user as Express.ExistingUser + const userProfile = await UserProfile.findOne({ - user: req.user._id, + user: id, }) if (!userProfile) { - throw new Error( - `UserProfile not found for user ${req.user.username} ${req.user._id}!` - ) + throw new Error(`UserProfile not found for user ${username} ${id}!`) } - const { username, email } = req.user const { progress } = userProfile res.json({ username, diff --git a/server/src/auth/oauthVerify.ts b/server/src/auth/oauthVerify.ts index 927f0b3..9daf3b8 100644 --- a/server/src/auth/oauthVerify.ts +++ b/server/src/auth/oauthVerify.ts @@ -2,12 +2,6 @@ import { User } from '@linux-odyssey/models' import type { Profile } from 'passport' import type { VerifyCallback } from 'passport-oauth2' -export type SocialLogin = { - provider: string - id: string - displayName: string -} - export default async function oauthVerify( provider: string, profile: Profile, @@ -37,7 +31,11 @@ export default async function oauthVerify( await user.save() } - return cb(null, user) + return cb(null, { + id: user.id, + username: user.username, + email: user.email, + }) } catch (err) { return cb( err instanceof Error ? err : new Error('An unknown error occurred') diff --git a/server/src/auth/passport.ts b/server/src/auth/passport.ts index 8b86d5b..1d9bf15 100644 --- a/server/src/auth/passport.ts +++ b/server/src/auth/passport.ts @@ -29,11 +29,11 @@ if (githubStrategy !== null) { passport.serializeUser((user: Express.User, done) => { process.nextTick(async () => { try { - const id = (user as any)._id - if (!id) { + if (!('id' in user)) { done(new Error('User has no _id')) return } + const { id } = user if (!(await UserProfile.exists({ user: id }))) { await UserProfile.create({ user: id }) } diff --git a/server/src/auth/passwordStrategy.ts b/server/src/auth/passwordStrategy.ts index d280cd8..3a6aab6 100644 --- a/server/src/auth/passwordStrategy.ts +++ b/server/src/auth/passwordStrategy.ts @@ -29,7 +29,11 @@ async function verifyPassword( if (!isPasswordValid) { return done(null, false, { message: 'Incorrect username or password.' }) } - return done(null, user) + return done(null, { + id: user.id, + username: user.username, + email: user.email, + }) } catch (err) { return done(err) } diff --git a/server/src/game/commandHandler.ts b/server/src/game/commandHandler.ts index e500c4a..00ba2cd 100644 --- a/server/src/game/commandHandler.ts +++ b/server/src/game/commandHandler.ts @@ -155,14 +155,10 @@ export default class CommandHandler extends SessionHandler { this.handleEvent() this.handleCommand() - console.log(stages) - const matches = await Promise.all( stages.map((stage) => stage.condition && this.isMatch(stage.condition)) ) - console.log(matches) - const stage = stages.find((_, i) => matches[i]) if (stage) { const response = this.execute(stage) diff --git a/server/src/middleware/authRequired.ts b/server/src/middleware/authRequired.ts index c7eb3d3..e66548a 100644 --- a/server/src/middleware/authRequired.ts +++ b/server/src/middleware/authRequired.ts @@ -5,7 +5,7 @@ export default function authRequired( res: Response, next: NextFunction ) { - if (req.isAuthenticated() && !('newUser' in req.session)) { + if (req.isAuthenticated() && !('newUser' in req.user)) { next() return } diff --git a/server/src/models/userManager.ts b/server/src/models/userManager.ts index 9651add..630ac76 100644 --- a/server/src/models/userManager.ts +++ b/server/src/models/userManager.ts @@ -1,11 +1,14 @@ +import { Express } from 'express' import { User, UserProfile } from '@linux-odyssey/models' -import { SocialLogin } from '../auth/oauthVerify.js' import { hashPassword } from '../utils/auth.js' export async function createUser( username: string, email: string, - { password, socialLogin }: { password?: string; socialLogin?: SocialLogin } + { + password, + socialLogin, + }: { password?: string; socialLogin?: Express.SocialLogin } ) { const user = new User({ username, diff --git a/server/src/types/express.d.ts b/server/src/types/express.d.ts new file mode 100644 index 0000000..a65988e --- /dev/null +++ b/server/src/types/express.d.ts @@ -0,0 +1,25 @@ +/* eslint-disable no-unused-vars */ +declare namespace Express { + export type SocialLogin = { + provider: string + id: string + displayName?: string + } + + export type PendingUser = { + email: string + socialLogin: SocialLogin + } + + export interface ExistingUser { + id: string + username: string + email: string + } + + export interface NewUser { + newUser?: PendingUser + } + + export type User = ExistingUser | NewUser +}