feat: 登录时同步微信头像和昵称
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -57,7 +57,11 @@ export class AuthService {
|
||||
@Inject(RANDOM_FN_TOKEN) private readonly randomFn: () => number = Math.random,
|
||||
) {}
|
||||
|
||||
async login(code: string): Promise<LoginResult> {
|
||||
async login(
|
||||
code: string,
|
||||
nickname?: string,
|
||||
avatarUrl?: string,
|
||||
): Promise<LoginResult> {
|
||||
const { openid, unionid, sessionKey } =
|
||||
await this.wechatService.code2Session(code)
|
||||
|
||||
@@ -71,10 +75,23 @@ export class AuthService {
|
||||
data: {
|
||||
openid,
|
||||
...(unionid !== undefined && { unionid }),
|
||||
nickname: generateDefaultNickname(this.randomFn),
|
||||
nickname: nickname || generateDefaultNickname(this.randomFn),
|
||||
...(avatarUrl && { avatarUrl }),
|
||||
},
|
||||
}))
|
||||
|
||||
// Update avatar for existing users if new avatar is provided
|
||||
if (existingUser && avatarUrl) {
|
||||
const updated = await this.prisma.user.update({
|
||||
where: { id: existingUser.id },
|
||||
data: { avatarUrl, ...(nickname && { nickname }) },
|
||||
})
|
||||
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 }
|
||||
}
|
||||
|
||||
sessionKeyStore.set(user.id, sessionKey)
|
||||
|
||||
const payload: JwtPayload = { sub: user.id, role: user.role as UserRole }
|
||||
|
||||
Reference in New Issue
Block a user