perf: 完善新用户引导
This commit is contained in:
@@ -94,6 +94,7 @@ describe('AuthService', () => {
|
||||
data: { openid: OPENID, nickname: TEST_NICKNAME },
|
||||
})
|
||||
expect(result.user).toEqual(mockUser)
|
||||
expect(result.isNewUser).toBe(true)
|
||||
})
|
||||
|
||||
it('creates user with unionid when present', async () => {
|
||||
@@ -123,6 +124,7 @@ describe('AuthService', () => {
|
||||
})
|
||||
expect(mockPrismaService.user.create).not.toHaveBeenCalled()
|
||||
expect(result.user).toEqual(mockUser)
|
||||
expect(result.isNewUser).toBe(false)
|
||||
})
|
||||
|
||||
it('returns a valid JWT token', async () => {
|
||||
@@ -145,6 +147,7 @@ describe('AuthService', () => {
|
||||
expect(result).toEqual({
|
||||
token: JWT_TOKEN,
|
||||
user: mockUser,
|
||||
isNewUser: false,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -24,7 +24,7 @@ export class AuthController {
|
||||
|
||||
@Post('login')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
async login(@Body() loginDto: LoginDto): Promise<{ token: string; user: User }> {
|
||||
async login(@Body() loginDto: LoginDto): Promise<{ token: string; user: User; isNewUser: boolean }> {
|
||||
return this.authService.login(
|
||||
loginDto.code,
|
||||
loginDto.nickname,
|
||||
|
||||
@@ -8,6 +8,7 @@ import { WechatService } from './wechat.service'
|
||||
export interface LoginResult {
|
||||
token: string
|
||||
user: User
|
||||
isNewUser: boolean
|
||||
}
|
||||
|
||||
export interface JwtPayload {
|
||||
@@ -69,6 +70,8 @@ export class AuthService {
|
||||
where: { openid },
|
||||
})
|
||||
|
||||
const isNewUser = existingUser === null
|
||||
|
||||
const user =
|
||||
existingUser ??
|
||||
(await this.prisma.user.create({
|
||||
@@ -89,7 +92,7 @@ export class AuthService {
|
||||
sessionKeyStore.set(updated.id, sessionKey)
|
||||
const payload: JwtPayload = { sub: updated.id, role: updated.role as UserRole }
|
||||
const token = this.jwtService.sign(payload)
|
||||
return { token, user: updated }
|
||||
return { token, user: updated, isNewUser: false }
|
||||
}
|
||||
|
||||
sessionKeyStore.set(user.id, sessionKey)
|
||||
@@ -97,7 +100,7 @@ export class AuthService {
|
||||
const payload: JwtPayload = { sub: user.id, role: user.role as UserRole }
|
||||
const token = this.jwtService.sign(payload)
|
||||
|
||||
return { token, user }
|
||||
return { token, user, isNewUser }
|
||||
}
|
||||
|
||||
async bindPhone(
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user