160 lines
3.2 KiB
Go
160 lines
3.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"api/database"
|
|
"api/models"
|
|
"api/utils"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/gofiber/fiber/v2"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func Version(c *fiber.Ctx) error {
|
|
return c.JSON(fiber.Map{
|
|
"version": "1.0.3",
|
|
})
|
|
}
|
|
|
|
func Login(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
|
|
if err := c.BodyParser(&data); err != nil {
|
|
return err
|
|
}
|
|
|
|
var user models.User
|
|
|
|
database.DB.Where("email = ?", data["email"]).First(&user)
|
|
|
|
if user.Id == 0 {
|
|
c.Status(fiber.StatusNotFound)
|
|
return c.JSON(fiber.Map{
|
|
"message": "Usuário não cadastrado",
|
|
})
|
|
}
|
|
|
|
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(data["password"])); err != nil {
|
|
c.Status(fiber.StatusBadRequest)
|
|
return c.JSON(fiber.Map{
|
|
"message": "Senha incorreta",
|
|
})
|
|
}
|
|
|
|
type customClaims struct {
|
|
Userid string `json:"user"`
|
|
jwt.StandardClaims
|
|
}
|
|
|
|
tok := customClaims{
|
|
Userid: strconv.Itoa(int(user.Id)),
|
|
StandardClaims: jwt.StandardClaims{
|
|
Issuer: strconv.Itoa(int(user.Id)),
|
|
ExpiresAt: time.Now().Add(time.Hour * 1).Unix(),
|
|
},
|
|
}
|
|
|
|
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, tok)
|
|
|
|
token, err := claims.SignedString([]byte(os.Getenv("API_SECRET")))
|
|
if err != nil {
|
|
c.Status(fiber.StatusInternalServerError)
|
|
return c.JSON(fiber.Map{
|
|
"message": "(A1) - Erro na geração do token",
|
|
})
|
|
}
|
|
|
|
return c.JSON(fiber.Map{
|
|
"token": token})
|
|
}
|
|
|
|
func AddUser(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
|
|
if err := c.BodyParser(&data); err != nil {
|
|
return err
|
|
}
|
|
|
|
if data["name"] == "" || data["email"] == "" || data["password"] == "" || data["channel"] == "" {
|
|
c.SendStatus(fiber.StatusBadRequest)
|
|
return c.JSON(fiber.Map{
|
|
"message": "Nome, E-Mail, Senha e Nome do Canal devem ser informados",
|
|
})
|
|
}
|
|
|
|
passwd, _ := utils.HashPassword(data["password"])
|
|
|
|
user := models.User{
|
|
Name: data["name"],
|
|
Email: data["email"],
|
|
Password: passwd,
|
|
Channel: data["channel"],
|
|
UserType: data["usertype"],
|
|
Blocked: "N",
|
|
Cancelled: "N",
|
|
CreatedBy: data["creator"],
|
|
}
|
|
|
|
database.DB.Create(&user)
|
|
|
|
if user.Id == 0 {
|
|
c.SendStatus(fiber.StatusFound)
|
|
return c.JSON(fiber.Map{
|
|
"message": "Usuário já cadastrado",
|
|
})
|
|
}
|
|
|
|
c.SendStatus(fiber.StatusCreated)
|
|
return c.JSON(user)
|
|
}
|
|
|
|
func GetOwnUser(c *fiber.Ctx) error {
|
|
|
|
var user models.User
|
|
|
|
userCode, err := utils.ProcessToken(c)
|
|
if err != nil {
|
|
c.Status(fiber.StatusBadRequest)
|
|
return c.JSON(fiber.Map{
|
|
"message": "Autorização inválida",
|
|
})
|
|
}
|
|
|
|
database.DB.Where("id = ?", userCode).First(&user)
|
|
|
|
if user.Id == 0 {
|
|
c.Status(fiber.StatusInternalServerError)
|
|
return c.JSON(fiber.Map{
|
|
"message": "(A2) - Autorização inválida",
|
|
})
|
|
}
|
|
|
|
return c.JSON(user)
|
|
}
|
|
|
|
func GetAllUsers(c *fiber.Ctx) error {
|
|
var users []models.User
|
|
|
|
_, err := utils.ProcessToken(c)
|
|
if err != nil {
|
|
c.Status(fiber.StatusUnauthorized)
|
|
return c.JSON(fiber.Map{
|
|
"message": "Sem autorização",
|
|
})
|
|
}
|
|
|
|
database.DB.Find(&users)
|
|
|
|
if len(users) == 0 {
|
|
c.Status(fiber.StatusNotFound)
|
|
return c.JSON(fiber.Map{
|
|
"message": "Nenhum usuário encontrado",
|
|
})
|
|
}
|
|
|
|
return c.JSON(users)
|
|
}
|