refactor!: change file for auth

This commit is contained in:
rick 2022-10-16 03:16:51 +02:00
parent 15fc1cc06a
commit 6006e37536
Signed by: Rick
GPG key ID: 4A6223D66294EB20
2 changed files with 106 additions and 114 deletions

102
controllers/auth.go Normal file
View file

@ -0,0 +1,102 @@
package controllers
import (
"cds/dao"
"cds/models"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/shareed2k/goth_fiber"
"go.mongodb.org/mongo-driver/mongo"
)
func CheckAuth(c *fiber.Ctx) error {
token := c.Cookies("token", "")
if token == "" {
return c.SendStatus(fiber.StatusNotFound)
} else {
return c.SendStatus(fiber.StatusOK)
}
}
func Auth(c *fiber.Ctx) error {
provider, err := goth_fiber.GetProviderName(c)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err,
})
}
var token string = ""
if provider == "discord" {
token, err = checkDiscord(c)
} else if provider == "steam" {
token, err = checkSteam(c)
}
if err == nil {
cookie := new(fiber.Cookie)
cookie.Name = "token"
cookie.Value = token
cookie.HTTPOnly = true
c.Cookie(cookie)
} else {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"ierror": fmt.Sprint(err),
})
}
//err = c.RedirectToRoute("index", fiber.Map{"auth": "ok"})
err = c.Redirect("/?test")
return err
//return c.SendStatus(fiber.StatusOK)
//return err
}
func checkDiscord(c *fiber.Ctx) (token string, err error) {
token = ""
gothuser, err := goth_fiber.CompleteUserAuth(c)
if err != nil {
return token, err
}
/*
fmt.Println(gothuser.UserID)
fmt.Println(gothuser.Name)
fmt.Println(gothuser.RawData["discriminator"])
fmt.Println(gothuser.AccessToken)
*/
id := gothuser.UserID
name := fmt.Sprint(gothuser.Name, "#", gothuser.RawData["discriminator"])
// on vérifie s'il existe déjà dans la db sinon on le créé
user, err := dao.GetByDiscord(id)
if err == mongo.ErrNoDocuments {
user = models.NewUserDiscord(id, name)
token, err = dao.CreateUser(user)
} else if err == nil {
token = user.Id.Hex()
}
return token, err
}
func checkSteam(c *fiber.Ctx) (token string, err error) {
token = ""
gothuser, err := goth_fiber.CompleteUserAuth(c)
if err != nil {
return token, err
}
id := gothuser.UserID
user, err := dao.GetBySteam(id)
if err == mongo.ErrNoDocuments {
user = models.NewUserSteam(id)
token, err = dao.CreateUser(user)
} else if err == nil {
token = user.Id.Hex()
}
return token, err
}

118
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"cds/controllers"
"cds/dao"
"cds/models"
"fmt"
@ -30,124 +31,13 @@ func setupRoutes(app *fiber.App) {
//app.Get("/swagger/*", swagger.HandlerDefault)
app.Get("/login/:provider", goth_fiber.BeginAuthHandler)
app.Get("/auth/:provider", auth)
app.Get("/groups/:id", groups)
app.Get("/auth/:provider", controllers.Auth)
app.Get("/check", controllers.CheckAuth)
//app.Get("/groups/:id", groups)
}
func checkDiscord(c *fiber.Ctx) error {
gothuser, err := goth_fiber.CompleteUserAuth(c)
if err != nil {
print("== ERROR ==")
panic(err)
}
/*
fmt.Println(gothuser.UserID)
fmt.Println(gothuser.Name)
fmt.Println(gothuser.RawData["discriminator"])
fmt.Println(gothuser.AccessToken)
*/
token := c.Cookies("token", "")
fmt.Println("Discord cookie:" + token)
id := gothuser.UserID
name := fmt.Sprint(gothuser.Name, "#", gothuser.RawData["discriminator"])
// on vérifie s'il existe déjà dans la db sinon on le créé
user, err := dao.GetByDiscord(id)
if user != nil && token == "" {
cookie := new(fiber.Cookie)
cookie.Name = "token"
cookie.Value = user.Id.Hex()
cookie.Expires = time.Now().Add(24 * time.Hour)
c.Cookie(cookie)
}
if err == mongo.ErrNoDocuments {
user = models.NewUserDiscord(id, name)
id, err = dao.CreateUser(user)
if err != nil {
fmt.Println(err)
}
if token == "" {
cookie := new(fiber.Cookie)
cookie.Name = "token"
cookie.Value = id
cookie.Expires = time.Now().Add(24 * time.Hour)
c.Cookie(cookie)
}
} else if err != nil {
fmt.Println(err)
}
c.Redirect("/")
return nil
}
func auth(c *fiber.Ctx) error {
provider, err := goth_fiber.GetProviderName(c)
if err != nil {
print("== ERROR ==")
panic(err)
}
if provider == "discord" {
return checkDiscord(c)
} else if provider == "steam" {
return checkSteam(c)
}
return nil
}
func checkSteam(c *fiber.Ctx) error {
token := c.Cookies("token", "")
gothuser, err := goth_fiber.CompleteUserAuth(c)
if err != nil {
print("== ERROR ==")
panic(err)
}
id := gothuser.UserID
user, err := dao.GetById(fmt.Sprint(token))
if user == nil {
user, err = dao.GetBySteam(id)
if user != nil && token == "" {
cookie := new(fiber.Cookie)
cookie.Name = "token"
cookie.Value = user.Id.Hex()
cookie.Expires = time.Now().Add(24 * time.Hour)
c.Cookie(cookie)
}
if err == mongo.ErrNoDocuments {
user = models.NewUserSteam(id)
id, err = dao.CreateUser(user)
if err != nil {
fmt.Println(err)
}
if token == "" {
cookie := new(fiber.Cookie)
cookie.Name = "token"
cookie.Value = id
cookie.Expires = time.Now().Add(24 * time.Hour)
c.Cookie(cookie)
}
} else if err != nil {
fmt.Println(err)
}
} else if user.SteamId == "" {
if user.DiscordId != "" {
dao.AddSteam(user.DiscordId, id)
}
}
c.Redirect("/")
return nil
}
func index(c *fiber.Ctx) error {