48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// GetGoogleAuthUrlHandlerV2 fetches a Google authentication URl and returns it.
|
|
// This function is atomic and cannot fail.
|
|
func (s *Server) GetGoogleAuthUrlHandlerV2(ctx *gin.Context) {
|
|
url := s.deps.AuthService.GetGoogleAuthUrl()
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"status": http.StatusOK,
|
|
"message": "[OK] Successfully retrieved Google auth URL.",
|
|
"url": url,
|
|
})
|
|
}
|
|
|
|
// GoogleCallbackHandlerV2 reads the data from the Google redirection and uses it
|
|
// to generate a JWT which is sent back to the UI via a URL query parameter. If an
|
|
// error occurs the user will be directed to the login page with an error query param.
|
|
func (s *Server) GoogleCallbackHandlerV2(ctx *gin.Context) {
|
|
var (
|
|
state string = ctx.Query("state")
|
|
code string = ctx.Query("code")
|
|
)
|
|
|
|
if jwt, err := s.deps.AuthService.GoogleAuthSuccess(state, code); err != nil {
|
|
url := fmt.Sprintf("http://localhost:5173/v2/web/login?error=%s", url.QueryEscape(err.Error()))
|
|
ctx.Redirect(http.StatusSeeOther, url)
|
|
} else {
|
|
url := "http://localhost:5173/v2/web/home"
|
|
s.SetCookie(ctx, "jwt_token", jwt, time.Hour*24*7)
|
|
ctx.Redirect(http.StatusSeeOther, url)
|
|
}
|
|
}
|
|
|
|
// BUG: This is not working, not yet sure why
|
|
func (s *Server) LogoutHandlerV2(ctx *gin.Context) {
|
|
s.SetCookie(ctx, "jwt_token", "", -1)
|
|
// s.SetCookie(ctx, "search-filters", "", -1) // TODO: This was copied, might function differently now
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|