mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 19:40:11 +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 { UserProfile } from '@linux-odyssey/models'
|
||||||
import { asyncHandler } from '../../middleware/error.js'
|
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({
|
const userProfile = await UserProfile.findOne({
|
||||||
user: req.user._id,
|
user: id,
|
||||||
})
|
})
|
||||||
if (!userProfile) {
|
if (!userProfile) {
|
||||||
throw new Error(
|
throw new Error(`UserProfile not found for user ${username} ${id}!`)
|
||||||
`UserProfile not found for user ${req.user.username} ${req.user._id}!`
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
const { username, email } = req.user
|
|
||||||
const { progress } = userProfile
|
const { progress } = userProfile
|
||||||
res.json({
|
res.json({
|
||||||
username,
|
username,
|
||||||
@@ -2,12 +2,6 @@ import { User } from '@linux-odyssey/models'
|
|||||||
import type { Profile } from 'passport'
|
import type { Profile } from 'passport'
|
||||||
import type { VerifyCallback } from 'passport-oauth2'
|
import type { VerifyCallback } from 'passport-oauth2'
|
||||||
|
|
||||||
export type SocialLogin = {
|
|
||||||
provider: string
|
|
||||||
id: string
|
|
||||||
displayName: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function oauthVerify(
|
export default async function oauthVerify(
|
||||||
provider: string,
|
provider: string,
|
||||||
profile: Profile,
|
profile: Profile,
|
||||||
@@ -37,7 +31,11 @@ export default async function oauthVerify(
|
|||||||
await user.save()
|
await user.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
return cb(null, user)
|
return cb(null, {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
email: user.email,
|
||||||
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return cb(
|
return cb(
|
||||||
err instanceof Error ? err : new Error('An unknown error occurred')
|
err instanceof Error ? err : new Error('An unknown error occurred')
|
||||||
|
|||||||
@@ -29,11 +29,11 @@ if (githubStrategy !== null) {
|
|||||||
passport.serializeUser((user: Express.User, done) => {
|
passport.serializeUser((user: Express.User, done) => {
|
||||||
process.nextTick(async () => {
|
process.nextTick(async () => {
|
||||||
try {
|
try {
|
||||||
const id = (user as any)._id
|
if (!('id' in user)) {
|
||||||
if (!id) {
|
|
||||||
done(new Error('User has no _id'))
|
done(new Error('User has no _id'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const { id } = user
|
||||||
if (!(await UserProfile.exists({ user: id }))) {
|
if (!(await UserProfile.exists({ user: id }))) {
|
||||||
await UserProfile.create({ user: id })
|
await UserProfile.create({ user: id })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,11 @@ async function verifyPassword(
|
|||||||
if (!isPasswordValid) {
|
if (!isPasswordValid) {
|
||||||
return done(null, false, { message: 'Incorrect username or password.' })
|
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) {
|
} catch (err) {
|
||||||
return done(err)
|
return done(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,14 +155,10 @@ export default class CommandHandler extends SessionHandler {
|
|||||||
this.handleEvent()
|
this.handleEvent()
|
||||||
this.handleCommand()
|
this.handleCommand()
|
||||||
|
|
||||||
console.log(stages)
|
|
||||||
|
|
||||||
const matches = await Promise.all(
|
const matches = await Promise.all(
|
||||||
stages.map((stage) => stage.condition && this.isMatch(stage.condition))
|
stages.map((stage) => stage.condition && this.isMatch(stage.condition))
|
||||||
)
|
)
|
||||||
|
|
||||||
console.log(matches)
|
|
||||||
|
|
||||||
const stage = stages.find((_, i) => matches[i])
|
const stage = stages.find((_, i) => matches[i])
|
||||||
if (stage) {
|
if (stage) {
|
||||||
const response = this.execute(stage)
|
const response = this.execute(stage)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ export default function authRequired(
|
|||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction
|
next: NextFunction
|
||||||
) {
|
) {
|
||||||
if (req.isAuthenticated() && !('newUser' in req.session)) {
|
if (req.isAuthenticated() && !('newUser' in req.user)) {
|
||||||
next()
|
next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
|
import { Express } from 'express'
|
||||||
import { User, UserProfile } from '@linux-odyssey/models'
|
import { User, UserProfile } from '@linux-odyssey/models'
|
||||||
import { SocialLogin } from '../auth/oauthVerify.js'
|
|
||||||
import { hashPassword } from '../utils/auth.js'
|
import { hashPassword } from '../utils/auth.js'
|
||||||
|
|
||||||
export async function createUser(
|
export async function createUser(
|
||||||
username: string,
|
username: string,
|
||||||
email: string,
|
email: string,
|
||||||
{ password, socialLogin }: { password?: string; socialLogin?: SocialLogin }
|
{
|
||||||
|
password,
|
||||||
|
socialLogin,
|
||||||
|
}: { password?: string; socialLogin?: Express.SocialLogin }
|
||||||
) {
|
) {
|
||||||
const user = new User({
|
const user = new User({
|
||||||
username,
|
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