pushbits/internal/authentication/context.go
2023-04-01 17:24:48 +02:00

32 lines
900 B
Go

package authentication
import (
"errors"
"net/http"
"github.com/pushbits/server/internal/model"
"github.com/gin-gonic/gin"
)
// GetApplication returns the application which was previously registered by the authentication middleware.
func GetApplication(ctx *gin.Context) *model.Application {
app, ok := ctx.MustGet("app").(*model.Application)
if app == nil || !ok {
ctx.AbortWithError(http.StatusInternalServerError, errors.New("an error occurred while retrieving application from context"))
return nil
}
return app
}
// GetUser returns the user which was previously registered by the authentication middleware.
func GetUser(ctx *gin.Context) *model.User {
user, ok := ctx.MustGet("user").(*model.User)
if user == nil || !ok {
ctx.AbortWithError(http.StatusInternalServerError, errors.New("an error occurred while retrieving user from context"))
return nil
}
return user
}