Răsfoiți Sursa

logic for attaching oauth integration to user account

Ivan Galakhov 4 ani în urmă
părinte
comite
f3c8781a74
2 a modificat fișierele cu 36 adăugiri și 4 ștergeri
  1. 3 0
      internal/models/user.go
  2. 33 4
      server/api/oauth_github_handler.go

+ 3 - 0
internal/models/user.go

@@ -12,6 +12,9 @@ type User struct {
 	Password      string `json:"password"`
 	EmailVerified bool   `json:"email_verified"`
 
+	// ID of oauth integration for github connection (optional)
+	GithubAppIntegrationID uint
+
 	// The github user id used for login (optional)
 	GithubUserID int64
 	GoogleUserID string

+ 33 - 4
server/api/oauth_github_handler.go

@@ -340,11 +340,40 @@ func (app *App) HandleGithubAppOAuthCallback(w http.ResponseWriter, r *http.Requ
 		return
 	}
 
-	//
+	userID, err := app.getUserIDFromRequest(r)
 
-	//app.Repo.User.ReadUser(session.Values["user_id"].(uint))
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
+
+	user, err := app.Repo.User.ReadUser(userID)
+
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
 
-	//session.Values["user_id"]
+	oauthInt := &integrations.OAuthIntegration{
+		Client:       integrations.OAuthGithub,
+		UserID:       userID,
+		AccessToken:  []byte(token.AccessToken),
+		RefreshToken: []byte(token.RefreshToken),
+	}
 
-	fmt.Println(token)
+	oauthInt, err = app.Repo.OAuthIntegration.CreateOAuthIntegration(oauthInt)
+
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
+
+	user.GithubAppIntegrationID = oauthInt.ID
+
+	user, err = app.Repo.User.UpdateUser(user)
+
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
 }