Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow88sky committed May 15, 2023
1 parent 2ec3d19 commit cd93378
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
67 changes: 40 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,22 @@ func main() {
address, err := utils.JwtDecode(jwt)
if err != nil {
logger.Error("failed to decode jwt:", zap.Error(err))
c.Redirect(http.StatusTemporaryRedirect, "/error")
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("解析jwt错误"))
return
}
if platformType == "github" {
// 使用OAuth配置对象中定义的Exchange方法,通过code获取access token
token, err := githubOauthConfig.Exchange(c, code)
if err != nil {
logger.Error("failed to exchange token:", zap.Error(err))
// todo 需要给个error页面
c.Redirect(http.StatusTemporaryRedirect, "/error")
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("获取token错误"))
return
}
client := githubOauthConfig.Client(c, token)
userInfo, err := getUserInfo(client)
if err != nil {
logger.Error("failed to get user info:", zap.Error(err))
c.Redirect(http.StatusTemporaryRedirect, "/error")
c.AbortWithError(http.StatusForbidden, fmt.Errorf("获取github用户信息错误"))
return
}
github := userInfo["login"].(string)
Expand All @@ -78,7 +77,7 @@ func main() {
result := db.Model(&utils.Address{}).Where("github = ?", github).First(&addr)
if addr != (utils.Address{}) {
logger.Error("github has bound:", zap.Error(result.Error))
c.Redirect(http.StatusTemporaryRedirect, "/error")
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("This github has bound"))
return
}
logger.Info("userInfo", zap.Any("user", userInfo))
Expand All @@ -87,6 +86,38 @@ func main() {
result = db.Model(&addr).Where("addr = ?", address).Updates(map[string]interface{}{"github": github, "email": email})
if result.Error != nil {
logger.Error("failed to update address:", zap.Error(result.Error))
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("Update Error"))
return
}
c.JSON(http.StatusOK, gin.H{"data": "success"})
} else if platformType == "discord" {
token, err := utils.ExchangeCodeForToken(code)
if err != nil {
logger.Error("failed to exchange discord token:", zap.Error(err))
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("获取token错误"))
return
}
user, err := utils.FetchUser(token)
if err != nil {
logger.Error("failed to get discord user info:", zap.Error(err))
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("获取discord用户信息错误"))
return
}
addr := utils.Address{}
result := db.Model(&utils.Address{}).Where("discord = ?", user).First(&addr)
if addr != (utils.Address{}) {
logger.Error("discord has bound:", zap.Error(result.Error))
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("This discord has bound"))
return
}
logger.Info("userInfo", zap.Any("user", user))
addr = utils.Address{}

result = db.Model(&addr).Where("addr = ?", address).Updates(map[string]interface{}{"discord": user})
if result.Error != nil {
logger.Error("failed to update address:", zap.Error(result.Error))
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("Update Error"))
return
}
c.JSON(http.StatusOK, gin.H{"data": "success"})
}
Expand All @@ -108,30 +139,12 @@ func main() {
r.GET("/oauth/discord", func(c *gin.Context) {
code := c.Query("code")
if code == "" {
logger.Error("No authorization code provided.")
// todo 需要给个error页面
c.Redirect(http.StatusTemporaryRedirect, "/error")
}
token, err := utils.ExchangeCodeForToken(code)
if err != nil {
logger.Error("Unable to exchange authorization code for access token", zap.Error(err))
c.Redirect(http.StatusTemporaryRedirect, "/error")
return
}
user, err := utils.FetchUser(token)
if err != nil {
logger.Error("Unable to fetch user information:", zap.Error(err))
return
}
fmt.Println(user)
// address, err := utils.JwtDecode(jwtToken)
if err != nil {
logger.Error("failed to get user info:", zap.Error(err))
c.Redirect(http.StatusTemporaryRedirect, "/error")
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("No authorization code provided."))
return
}
// fmt.Println(address)
c.Redirect(http.StatusTemporaryRedirect, "https://topscore.social")
logger.Info("discord oauth认证", zap.String("code", code))

c.Redirect(http.StatusTemporaryRedirect, "https://topscore.social?code="+code)
})

r.Run(":8001")
Expand Down
9 changes: 5 additions & 4 deletions utils/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
var db *gorm.DB

type Address struct {
Addr string `json:"addr" gorm:"column:addr;primaryKey"`
Name string `json:"name"`
Github string `json:"github"`
Email string `json:"email"`
Addr string `json:"addr" gorm:"column:addr;primaryKey"`
Name string `json:"name"`
Github string `json:"github"`
Email string `json:"email"`
Discord string `json:"discord"`
}

func (Address) TableName() string {
Expand Down

0 comments on commit cd93378

Please sign in to comment.