27 lines
560 B
Go
27 lines
560 B
Go
package appcontext
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AppContext struct {
|
|
C *gin.Context
|
|
Db *gorm.DB
|
|
I18n *i18n.Bundle
|
|
}
|
|
|
|
func (ac AppContext) GetAuthenticatedUser() (model.User, error) {
|
|
var user model.User
|
|
username, userIsInContext := ac.C.Get("user")
|
|
if !userIsInContext {
|
|
return user, errors.New("User not found in context")
|
|
}
|
|
res := ac.Db.Where("name = ?", username).First(&user)
|
|
return user, res.Error
|
|
}
|