Skip to content

Commit

Permalink
Merge pull request #6 from Aditya-Chowdhary/v2
Browse files Browse the repository at this point in the history
fix: redirect to frontend
  • Loading branch information
Chandram-Dutta authored Aug 19, 2024
2 parents be9b2ab + 5106a71 commit 089b56f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ OAUTH_CLIENT_ID=your_oauth_client_id_here
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
GOOGLE_REDIRECT_URI=your_google_redirect_uri_here
FRONTEND_CALLBACK_URL=fronted_callback_url_for_oauth
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID}
- GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET}
- GOOGLE_REDIRECT_URI=${GOOGLE_REDIRECT_URI}
- FRONTEND_CALLBACK_URL=${FRONTEND_CALLBACK_URL}
depends_on:
- psql
- migrate
Expand Down
21 changes: 15 additions & 6 deletions internal/controllers/v1/auth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"io"
"net/http"
"net/url"
"os"
"time"
"triton-backend/internal/database"
Expand All @@ -27,6 +28,7 @@ type AuthHandler struct {
db *pgxpool.Pool
googleOauthConfig *oauth2.Config
oauthStateString string
frontendCallback string
}

func Handler(db *pgxpool.Pool) *AuthHandler {
Expand All @@ -40,6 +42,7 @@ func Handler(db *pgxpool.Pool) *AuthHandler {
Endpoint: google.Endpoint,
},
oauthStateString: "random_state_string", // Ideally, generate dynamically
frontendCallback: os.Getenv("FRONTEND_CALLBACK_URL"),
}
}

Expand Down Expand Up @@ -147,12 +150,18 @@ func (a *AuthHandler) GoogleCallbackHandler(c *gin.Context) {
return
}

c.JSON(http.StatusOK, utils.BaseResponse{
Success: true,
Message: "OAuth user successfully authenticated",
Data: tok,
StatusCode: http.StatusOK,
})
queryVal := url.Values{
"token": {tok.Plaintext},
}
reqUrl := a.frontendCallback + "?" + queryVal.Encode()
c.Redirect(http.StatusTemporaryRedirect, reqUrl)

// c.JSON(http.StatusOK, utils.BaseResponse{
// Success: true,
// Message: "OAuth user successfully authenticated",
// Data: tok,
// StatusCode: http.StatusOK,
// })
}

func (a *AuthHandler) RegisterOAuthUser(c *gin.Context) {
Expand Down

0 comments on commit 089b56f

Please sign in to comment.