fusenapi/server/home-user-auth/internal/logic/useroauth2loginlogic.go
2023-07-17 19:43:43 +08:00

65 lines
1.7 KiB
Go

package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"log"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/474420502/requests"
"github.com/zeromicro/go-zero/core/logx"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
type UserOAuth2LoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserOAuth2LoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOAuth2LoginLogic {
return &UserOAuth2LoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserOAuth2LoginLogic) UserOAuth2Login(req *types.RequestOAuth, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
switch req.Platform {
case "google":
var googleOauthConfig = &oauth2.Config{
RedirectURL: "https://fusenh5.kayue.cn/api/user/oauth2/login/google",
ClientID: l.svcCtx.Config.OAuth[0].Appid,
ClientSecret: l.svcCtx.Config.OAuth[0].Secret,
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
Endpoint: google.Endpoint,
}
token, err := googleOauthConfig.Exchange(context.Background(), req.Code)
if err != nil {
resp.SetStatus(basic.CodeApiErr)
}
resp, err := requests.Get("https://www.googleapis.com/oauth2/v2/userinfo" + token.AccessToken).Execute()
if err != nil {
panic(err)
}
log.Println(resp.Json())
case "facebook":
default:
}
return resp.SetStatus(basic.CodeOK)
}