@@ -367,18 +367,14 @@ func (u *UserUsecase) DeleteAdmin(ctx context.Context, id string) error {
367367 return u .repo .DeleteAdmin (ctx , id )
368368}
369369
370- func (u * UserUsecase ) OAuthSignUpOrIn (ctx context.Context , req * domain.OAuthSignUpOrInReq ) (* domain.OAuthURLResp , error ) {
371- setting , err := u .repo .GetSetting (ctx )
372- if err != nil {
373- return nil , err
374- }
370+ func (u * UserUsecase ) getOAuthConfig (setting * db.Setting , platform consts.UserPlatform ) (* domain.OAuthConfig , error ) {
375371 cfg := domain.OAuthConfig {
376372 Debug : u .cfg .Debug ,
377- Platform : req . Platform ,
373+ Platform : platform ,
378374 RedirectURI : fmt .Sprintf ("%s/api/v1/user/oauth/callback" , u .cfg .BaseUrl ),
379375 }
380376
381- switch req . Platform {
377+ switch platform {
382378 case consts .UserPlatformDingTalk :
383379 if setting .DingtalkOauth == nil || ! setting .DingtalkOauth .Enable {
384380 return nil , errcode .ErrDingtalkNotEnabled
@@ -398,11 +394,25 @@ func (u *UserUsecase) OAuthSignUpOrIn(ctx context.Context, req *domain.OAuthSign
398394 cfg .IDField = setting .CustomOauth .IDField
399395 cfg .NameField = setting .CustomOauth .NameField
400396 cfg .AvatarField = setting .CustomOauth .AvatarField
397+ cfg .EmailField = setting .CustomOauth .EmailField
401398 default :
402399 return nil , errcode .ErrUnsupportedPlatform
403400 }
404401
405- oauth , err := oauth .NewOAuther (cfg )
402+ return & cfg , nil
403+ }
404+
405+ func (u * UserUsecase ) OAuthSignUpOrIn (ctx context.Context , req * domain.OAuthSignUpOrInReq ) (* domain.OAuthURLResp , error ) {
406+ setting , err := u .repo .GetSetting (ctx )
407+ if err != nil {
408+ return nil , err
409+ }
410+ cfg , err := u .getOAuthConfig (setting , req .Platform )
411+ if err != nil {
412+ return nil , err
413+ }
414+
415+ oauth , err := oauth .NewOAuther (* cfg )
406416 if err != nil {
407417 return nil , err
408418 }
@@ -465,37 +475,13 @@ func (u *UserUsecase) FetchUserInfo(ctx context.Context, req *domain.OAuthCallba
465475 if err != nil {
466476 return nil , err
467477 }
468- cfg := domain.OAuthConfig {
469- Debug : u .cfg .Debug ,
470- Platform : session .Platform ,
471- RedirectURI : fmt .Sprintf ("%s/api/v1/user/oauth/callback" , u .cfg .BaseUrl ),
472- }
473478
474- switch session .Platform {
475- case consts .UserPlatformDingTalk :
476- if setting .DingtalkOauth == nil || ! setting .DingtalkOauth .Enable {
477- return nil , errcode .ErrDingtalkNotEnabled
478- }
479- cfg .ClientID = setting .DingtalkOauth .ClientID
480- cfg .ClientSecret = setting .DingtalkOauth .ClientSecret
481- case consts .UserPlatformCustom :
482- if setting .CustomOauth == nil || ! setting .CustomOauth .Enable {
483- return nil , errcode .ErrCustomNotEnabled
484- }
485- cfg .ClientID = setting .CustomOauth .ClientID
486- cfg .ClientSecret = setting .CustomOauth .ClientSecret
487- cfg .AuthorizeURL = setting .CustomOauth .AuthorizeURL
488- cfg .Scopes = setting .CustomOauth .Scopes
489- cfg .TokenURL = setting .CustomOauth .AccessTokenURL
490- cfg .UserInfoURL = setting .CustomOauth .UserInfoURL
491- cfg .IDField = setting .CustomOauth .IDField
492- cfg .NameField = setting .CustomOauth .NameField
493- cfg .AvatarField = setting .CustomOauth .AvatarField
494- default :
495- return nil , errcode .ErrUnsupportedPlatform
479+ cfg , err := u .getOAuthConfig (setting , session .Platform )
480+ if err != nil {
481+ return nil , err
496482 }
497483
498- oauth , err := oauth .NewOAuther (cfg )
484+ oauth , err := oauth .NewOAuther (* cfg )
499485 if err != nil {
500486 return nil , err
501487 }
0 commit comments