mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: userController and add req user type
This commit is contained in:
1 parent
4c421b4cb3
commit
c912319967
8 files changed
+49
-23
No files matched your search
+6
-6
@@ -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,
|
||||
@@ -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')
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Vendored
+25
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user