|
|
|
@ -2,59 +2,53 @@ package auth |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"github.com/google/uuid" |
|
|
|
|
|
|
|
"github.com/markbates/goth" |
|
|
|
|
|
|
|
"github.com/markbates/goth/gothic" |
|
|
|
"github.com/markbates/goth/gothic" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"sponsorahacker/config" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func Login(c *gin.Context) { |
|
|
|
func Login(c *gin.Context) { |
|
|
|
providerName := c.Param("provider") |
|
|
|
providerName := c.Param("provider") |
|
|
|
|
|
|
|
q := c.Request.URL.Query() |
|
|
|
// Begin the authentication process
|
|
|
|
q.Add("provider", providerName) |
|
|
|
provider, err := goth.GetProvider(providerName) |
|
|
|
c.Request.URL.RawQuery = q.Encode() |
|
|
|
if err != nil { |
|
|
|
gothic.BeginAuthHandler(c.Writer, c.Request) |
|
|
|
c.String(http.StatusInternalServerError, "Error getting provider: %s", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
state := uuid.New().String() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
session, err := provider.BeginAuth(state) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
c.String(http.StatusInternalServerError, "Error creating auth url: %s", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
url, err := session.GetAuthURL() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
c.String(http.StatusInternalServerError, "Error getting auth url: %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, url) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Callback(c *gin.Context) { |
|
|
|
func Callback(c *gin.Context) { |
|
|
|
sessionStore, err := NewSessionManager("libsql://sponsorahackersession-stephanie-gredell.turso.io") |
|
|
|
sessionStore, err := NewSessionManager(config.GetEnvVar("DATABASE_URL")) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
user, err := gothic.CompleteUserAuth(c.Writer, c.Request) |
|
|
|
user, err := gothic.CompleteUserAuth(c.Writer, c.Request) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Println("Error during user authentication:", err) |
|
|
|
log.Println("Error during user authentication:", err) |
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/") |
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login") |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
c.SetCookie("user_id", user.UserID, 3600, "/", "localhost", false, true) |
|
|
|
c.SetCookie("user_id", user.UserID, 3600, "/", "localhost", false, true) |
|
|
|
|
|
|
|
err = sessionStore.SetSession(user.Name, c) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Println("failed to set session:", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// For now, redirect to profile page after successful login
|
|
|
|
// For now, redirect to profile page after successful login
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/") |
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Logout(c *gin.Context) { |
|
|
|
func Logout(c *gin.Context) { |
|
|
|
|
|
|
|
sessionStore, err := NewSessionManager(config.GetEnvVar("DATABASE_URL")) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
panic(err) |
|
|
|
|
|
|
|
} |
|
|
|
c.SetCookie("user_id", "", -1, "/", "localhost", false, true) |
|
|
|
c.SetCookie("user_id", "", -1, "/", "localhost", false, true) |
|
|
|
|
|
|
|
err = sessionStore.DeleteSession(c) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Println("failed to delete session:", err) |
|
|
|
|
|
|
|
} |
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/") |
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/") |
|
|
|
} |
|
|
|
} |
|
|
|
|