This works very well, just need to determine what routes will be protected and such. For now, a simple system is setup, with more to come. For now, this is a WIP and needs some light work. But auth is almost complete.
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
domain "github.com/haydenhargreaves/Potion/internal/domain/server"
|
|
)
|
|
|
|
func GoogleLogin(ctx *gin.Context) {
|
|
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
|
|
url := deps.AuthService.GetGoogleAuthUrl()
|
|
|
|
ctx.Redirect(http.StatusSeeOther, url)
|
|
}
|
|
|
|
func GoogleCallback(ctx *gin.Context) {
|
|
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
|
|
|
|
var (
|
|
state string = ctx.Query("state")
|
|
code string = ctx.Query("code")
|
|
)
|
|
|
|
// TODO: Do something real, not just return data
|
|
if jwt, dbUser, googleUserInfo, err := deps.AuthService.GoogleAuthSuccess(state, code); err != nil {
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
} else {
|
|
// TODO: Update these values when using a real domain. Maybe an ENV?
|
|
ctx.SetCookie(
|
|
"jwt_token",
|
|
jwt,
|
|
int(time.Now().Add(7*24*time.Hour).Sub(time.Now()).Seconds()),
|
|
"/",
|
|
"localhost",
|
|
false, // TODO: True in prod
|
|
true,
|
|
)
|
|
ctx.JSON(http.StatusOK, gin.H{"jwt": jwt, "googleUserInfo": googleUserInfo, "dbUser": dbUser})
|
|
}
|
|
}
|