Rename context functions

This commit is contained in:
perp 2024-06-04 16:00:53 +01:00
parent 510d71ef33
commit 29f0900068
4 changed files with 28 additions and 26 deletions

View File

@ -48,7 +48,7 @@ func New(cfg *config.Config, db *database.Database) *Context {
}
// Return a Gin Context
func (ctx *Context) API(handler func(ctx *Context)) func(*gin.Context) {
func (ctx *Context) Route(handler func(ctx *Context)) func(*gin.Context) {
return func(gctx *gin.Context) {
context := Context{
Context: gctx,
@ -60,15 +60,17 @@ func (ctx *Context) API(handler func(ctx *Context)) func(*gin.Context) {
}
// Return an Error
func (ctx *Context) Error(err string) models.Error {
return models.Error{
func (ctx *Context) Error(code int, err string) {
error := &models.Error{
Type: err,
}
ctx.JSON(code, error)
}
// Return a Token
func (ctx *Context) Token(token string) models.Token {
return models.Token{
func (ctx *Context) Token(token string) {
token_ := &models.Token{
Token: token,
}
ctx.JSON(200, token_)
}

View File

@ -29,7 +29,7 @@ import (
// @param register body v1.Register true "alice" "supersecretpassword"
// @success 200 {object} models.Token
// @failure 400 {object} models.Error "MissingBody | InvalidUsername | InvalidPassword"
// @failure 500 {object} models.Error "InternalServerError"
// @failure 500 {object} models.Error "DatabaseError | InternalServerError"
// @router /v1/account/login [post]
func Login(ctx *context.Context) {
// Store body
@ -38,36 +38,36 @@ func Login(ctx *context.Context) {
// Bind JSON
err := ctx.BindJSON(&body)
if err != nil {
ctx.JSON(400, ctx.Error("MissingBody"))
ctx.Error(400, "MissingBody")
return
}
// Select account by username
account, err := ctx.Db.Account.SelectByUsername(body.Username)
if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError"))
ctx.Error(500, "DatabaseError")
return
}
// Compare username
if account.Username != body.Username {
ctx.JSON(400, ctx.Error("InvalidUsername"))
ctx.Error(400, "InvalidUsername")
return
}
// Compare password
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(body.Password))
if err != nil {
ctx.JSON(400, ctx.Error("InvalidPassword"))
ctx.Error(400, "InvalidPassword")
return
}
// Generate token
token, err := jwt.Encode(account.ID)
if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError"))
ctx.Error(500, "InternalServerError")
return
}
ctx.JSON(200, ctx.Token(token))
ctx.Token(token)
}

View File

@ -30,12 +30,12 @@ import (
// @success 200 {object} models.Token
// @failure 400 {object} models.Error "MissingBody | UsernameTaken"
// @failure 403 {object} models.Error "RegistrationDisabled"
// @failure 500 {object} models.Error "InternalServerError"
// @failure 500 {object} models.Error "DatabaseError | InternalServerError"
// @router /v1/account/register [post]
func Register(ctx *context.Context) {
// Check if registration is disabled
if ctx.Config.Auth.Disabled {
ctx.JSON(403, ctx.Error("RegistrationDisabled"))
ctx.Error(403, "RegistrationDisabled")
return
}
@ -45,50 +45,50 @@ func Register(ctx *context.Context) {
// Bind JSON
err := ctx.BindJSON(&body)
if err != nil {
ctx.JSON(400, ctx.Error("MissingBody"))
ctx.Error(400, "MissingBody")
return
}
// Select account by username
account, err := ctx.Db.Account.SelectByUsername(body.Username)
if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError"))
ctx.Error(500, "DatabaseError")
return
}
// Compare username
if account.Username == body.Username {
ctx.JSON(400, ctx.Error("UsernameTaken"))
ctx.Error(400, "UsernameTaken")
return
}
// Hash password
password, err := bcrypt.GenerateFromPassword([]byte(body.Password), ctx.Config.Auth.Cost)
if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError"))
ctx.Error(500, "InternalServerError")
return
}
// Insert account
err = ctx.Db.Account.Insert(body.Username, string(password))
if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError"))
ctx.Error(500, "DatabaseError")
return
}
// Select account by username
account, err = ctx.Db.Account.SelectByUsername(body.Username)
if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError"))
ctx.Error(500, "DatabaseError")
return
}
// Generate token
token, err := jwt.Encode(account.ID)
if err != nil {
ctx.JSON(500, ctx.Error("InternalServerError"))
ctx.Error(500, "InternalServerError")
return
}
ctx.JSON(200, ctx.Token(token))
ctx.Token(token)
}

View File

@ -34,13 +34,13 @@ func Register(ctx *context.Context) {
v1 := ctx.Group("v1")
{
v1.GET("scalar", ctx.API(Spec))
v1.GET("docs", ctx.API(Spec))
v1.GET("scalar", ctx.Route(Spec))
v1.GET("docs", ctx.Route(Spec))
}
{
a := v1.Group("account")
a.POST("login", ctx.API(account.Login))
a.POST("register", ctx.API(account.Register))
a.POST("login", ctx.Route(account.Login))
a.POST("register", ctx.Route(account.Register))
}
}