Skip to content

Commit 5cc99f5

Browse files
authored
Merge pull request #232 from import-ai/fix/auth
fix(auth): fix social media account binding errors
2 parents a7fd16f + 8bc0c94 commit 5cc99f5

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

src/auth/google/google.service.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,28 @@ export class GoogleService {
173173
}
174174

175175
if (userId) {
176-
const wechatUser = await this.userService.findByLoginId(userData.sub);
177-
if (wechatUser && wechatUser.id !== userId) {
178-
const providerName = this.i18n.t('auth.providers.google');
179-
const message = this.i18n.t('auth.errors.invalidProviderData', {
180-
args: { provider: providerName },
181-
});
182-
throw new AppException(
183-
message,
184-
'ACCOUNT_ALREADY_BOUND',
185-
HttpStatus.BAD_REQUEST,
186-
);
176+
const googleUser = await this.userService.findByLoginId(userData.sub);
177+
if (googleUser) {
178+
if (googleUser.id !== userId) {
179+
const providerName = this.i18n.t('auth.providers.google');
180+
const message = this.i18n.t('auth.errors.invalidProviderData', {
181+
args: { provider: providerName },
182+
});
183+
throw new AppException(
184+
message,
185+
'ACCOUNT_ALREADY_BOUND',
186+
HttpStatus.BAD_REQUEST,
187+
);
188+
}
189+
const returnValue = {
190+
id: googleUser.id,
191+
access_token: this.jwtService.sign({
192+
sub: googleUser.id,
193+
}),
194+
};
195+
stateInfo.userInfo = returnValue;
196+
await this.socialService.updateState(state, stateInfo);
197+
return returnValue;
187198
}
188199
const existingUser = await this.userService.bindingExistUser({
189200
userId,

src/auth/wechat/wechat.service.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,27 @@ export class WechatService {
174174

175175
if (userId) {
176176
const wechatUser = await this.userService.findByLoginId(userData.unionid);
177-
if (wechatUser && wechatUser.id !== userId) {
178-
const providerName = this.i18n.t('auth.providers.wechat');
179-
const message = this.i18n.t('auth.errors.invalidProviderData', {
180-
args: { provider: providerName },
181-
});
182-
throw new AppException(
183-
message,
184-
'ACCOUNT_ALREADY_BOUND',
185-
HttpStatus.BAD_REQUEST,
186-
);
177+
if (wechatUser) {
178+
if (wechatUser.id !== userId) {
179+
const providerName = this.i18n.t('auth.providers.wechat');
180+
const message = this.i18n.t('auth.errors.invalidProviderData', {
181+
args: { provider: providerName },
182+
});
183+
throw new AppException(
184+
message,
185+
'ACCOUNT_ALREADY_BOUND',
186+
HttpStatus.BAD_REQUEST,
187+
);
188+
}
189+
const returnValue = {
190+
id: wechatUser.id,
191+
access_token: this.jwtService.sign({
192+
sub: wechatUser.id,
193+
}),
194+
};
195+
stateInfo.userInfo = returnValue;
196+
await this.socialService.updateState(state, stateInfo);
197+
return returnValue;
187198
}
188199
const existingUser = await this.userService.bindingExistUser({
189200
userId,

0 commit comments

Comments
 (0)