feat(auth): Add login, register, session_info and api creation
This commit is contained in:
11
app/controllers/api_key.go
Normal file
11
app/controllers/api_key.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/auth-api/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func CreateApiKeyController(c *fiber.Ctx) error {
|
||||
response := services.CreateApiKey()
|
||||
return response.Send(c)
|
||||
}
|
||||
35
app/controllers/login.go
Normal file
35
app/controllers/login.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/auth-api/services"
|
||||
"git.secnex.io/secnex/auth-api/utils"
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
}
|
||||
|
||||
func LoginController(c *fiber.Ctx) error {
|
||||
var request LoginRequest
|
||||
if err := c.BodyParser(&request); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
masterlog.Debug("Processing login request", map[string]interface{}{"username": request.Username})
|
||||
|
||||
validate := validator.New()
|
||||
if err := validate.Struct(request); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
response := services.Login(request.Username, request.Password)
|
||||
return response.Send(c)
|
||||
}
|
||||
35
app/controllers/register.go
Normal file
35
app/controllers/register.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/auth-api/services"
|
||||
"git.secnex.io/secnex/auth-api/utils"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type RegisterRequest struct {
|
||||
FirstName string `json:"first_name" validate:"required"`
|
||||
LastName string `json:"last_name" validate:"required"`
|
||||
Username string `json:"username" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
Email string `json:"email" validate:"required"`
|
||||
}
|
||||
|
||||
func RegisterController(c *fiber.Ctx) error {
|
||||
var request RegisterRequest
|
||||
if err := c.BodyParser(&request); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
if err := validate.Struct(request); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
response := services.Register(request.FirstName, request.LastName, request.Username, request.Password, request.Email)
|
||||
return response.Send(c)
|
||||
}
|
||||
40
app/controllers/session_info.go
Normal file
40
app/controllers/session_info.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/auth-api/services"
|
||||
"git.secnex.io/secnex/auth-api/utils"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type SessionInfoRequest struct {
|
||||
SessionToken string `json:"token" validate:"required"`
|
||||
}
|
||||
|
||||
func SessionInfoController(c *fiber.Ctx) error {
|
||||
var request SessionInfoRequest
|
||||
if err := c.BodyParser(&request); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
if err := validate.Struct(request); err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusBadRequest, &fiber.Map{
|
||||
"message": "Invalid request body",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
sessionDetails, err := services.SessionInfo(request.SessionToken)
|
||||
if err != nil {
|
||||
return utils.NewErrorResponse(fiber.StatusUnauthorized, &fiber.Map{
|
||||
"message": "Invalid token",
|
||||
}).Send(c)
|
||||
}
|
||||
|
||||
return utils.NewHTTPResponse(fiber.StatusOK, &fiber.Map{
|
||||
"message": "OK",
|
||||
"session": sessionDetails,
|
||||
}, "", nil, nil).Send(c)
|
||||
}
|
||||
Reference in New Issue
Block a user