init(api): add user authentication handler
also add error casting function to db.go
This commit is contained in:
parent
68d5063dd9
commit
18064db60a
54
backend/api/handlers.go
Normal file
54
backend/api/handlers.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/H1K0/SkazaNull/db"
|
||||||
|
"github.com/gin-contrib/sessions"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
//#region User
|
||||||
|
|
||||||
|
func userAuth(c *gin.Context) {
|
||||||
|
var credentials struct {
|
||||||
|
Login string `form:"login" binding:"required"`
|
||||||
|
Password string `form:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
err := c.ShouldBind(&credentials)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "И че за шнягу ты мне кинул?"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user, err := db.UserAuth(context.Background(), credentials.Login, credentials.Password)
|
||||||
|
if err != nil {
|
||||||
|
pqErr := db.CastToPgError(err)
|
||||||
|
if pqErr == nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
statusStr := pqErr.Message[:3]
|
||||||
|
msg := pqErr.Message[4:]
|
||||||
|
status, _err := strconv.ParseInt(statusStr, 10, 0)
|
||||||
|
if _err == nil {
|
||||||
|
c.JSON(int(status), gin.H{"error": msg})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
session := sessions.Default(c)
|
||||||
|
session.Set("user_id", user.ID)
|
||||||
|
session.Set("started", time.Now().Unix())
|
||||||
|
session.Save()
|
||||||
|
c.JSON(http.StatusOK, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
//#endregion User
|
||||||
|
|
||||||
|
//#region Quotes
|
||||||
|
|
||||||
|
//#endregion Quotes
|
||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/H1K0/SkazaNull/models"
|
"github.com/H1K0/SkazaNull/models"
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,6 +31,14 @@ func InitDB(connString string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CastToPgError(err error) *pgconn.PgError {
|
||||||
|
pqErr, ok := err.(*pgconn.PgError)
|
||||||
|
if ok {
|
||||||
|
return pqErr
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//#region User
|
//#region User
|
||||||
|
|
||||||
func UserAuth(ctx context.Context, login string, password string) (user models.User, err error) {
|
func UserAuth(ctx context.Context, login string, password string) (user models.User, err error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user