Skip to content

Commit

Permalink
fix: 简化部分逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
afzw committed Jan 26, 2024
1 parent 552082a commit 1ecf4fa
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 32 deletions.
13 changes: 1 addition & 12 deletions src/apis/auth/local-auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ import { AppError } from '@/lib/error'
import { genRandom32BitsHexString, genUniqString, genPBK } from '@/lib/encryption/crypto'
import { Types } from 'mongoose'

/** 用户登录信息 */
interface LoginInfo {
/** 用户邮箱 */
email: string
/** 用户密码 */
password: string
}

/** 用户注册信息 */
interface RegisterInfo {
/** 用户注册邮箱 */
email: string
/** 用户密码 */
password: string
}
type RegisterInfo = LoginInfo

class LocalAuthController {
public static async login(req: Request<null, UserProps, LoginInfo>, res: Response, next: NextFunction) {
Expand Down Expand Up @@ -72,11 +63,9 @@ class LocalAuthController {

const salt = genRandom32BitsHexString()
const password = genPBK(registerInfo.password, salt)
const username = `user-${genUniqString()}`

const newUserProps: Partial<UserProps> = {
email: registerInfo.email,
username,
password,
salt
}
Expand Down
11 changes: 4 additions & 7 deletions src/apis/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ import UserStore from '../../business/user/user.store'
* 获取用户主页信息
*/
export async function getProfile(req: Request, res: Response) {
const user = req.user

const [updateSessionInfoErr] = await callAsync(
SessionInfoDao.findOneAndUpdate({ sessionId: req.sessionID }, { activeAt: new Date() })
)
if (updateSessionInfoErr) return res.status(500).send(`更新会话信息失败 => ${updateSessionInfoErr}`)

const [findUserInfoErr, userInfo] = await callAsync(
UserDao.findOne({ _id: req.user._id }, { activeAt: new Date() }, { lean: true })
)
if (findUserInfoErr) return res.status(500).send(`获取用户信息失败 => ${findUserInfoErr}`)
if (updateSessionInfoErr) console.log(`更新会话信息失败 => ${updateSessionInfoErr}`)

const profile = _.pick(userInfo, UserStore.theProfileKeys())
const profile = _.pick(user, UserStore.theProfileKeys())

res.send(profile)
}
Expand Down
2 changes: 1 addition & 1 deletion src/business/auth/local/local-auth.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const LocalAuthStore = {
/** 登录信息字段名 */
theLoginInfoKeys: () => ['email', 'password'],
/** 注册信息字段名 */
theRegisterInfoKeys: () => ['email', 'password']
theRegisterInfoKeys: () => [...LocalAuthStore.theLoginInfoKeys()]
}

export { LocalAuthStore }
2 changes: 1 addition & 1 deletion src/business/user/user.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const UserStore = {
/**
* 用户主页属性键名。
*/
theProfileKeys: () => ['email', 'username', 'nickname', 'avatar', 'roles']
theProfileKeys: () => ['email', 'username', 'nickname', 'avatar', 'role']
}

export default UserStore
22 changes: 11 additions & 11 deletions src/entities/auth/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export interface UserProps extends App.User {
/**
* 用户名
*/
username: string
//username: string
/**
* 用户昵称
*/
nickname?: string
//nickname?: string
/**
* 用户头像保存路径
*/
avatar?: string
//avatar?: string
/**
* 密码
*/
Expand Down Expand Up @@ -109,14 +109,14 @@ const userSchema = new Schema<UserProps, UserModelType, UserMethods, UserQueryHe
required: true,
unique: true
},
username: {
type: String,
index: true,
required: true,
unique: true
},
nickname: String,
avatar: String,
// username: {
// type: String,
// index: true,
// required: true,
// unique: true
// },
// nickname: String,
// avatar: String,
password: {
type: String,
required: true
Expand Down

0 comments on commit 1ecf4fa

Please sign in to comment.