szoe-pontok/database/auth/middleware.go
2024-10-10 22:27:01 +02:00

46 lines
761 B
Go

package auth
import (
"errors"
"git.tek.govt.hu/dowerx/szoe-pontok/database"
"github.com/redis/go-redis/v9"
)
func LoggedIn(token string) (string, error) {
rdb, ctx := database.GetRDB()
result, err := rdb.Get(ctx, token).Result()
if err == redis.Nil {
return "", err
}
return result, nil
}
func IsAdmin(neptun string) error {
db := database.GetDB()
rows, err := db.NamedQuery(`select count(*) from "admin" inner join "user" on "user"."id" = "admin"."user" where "user"."neptun" = :neptun`,
map[string]interface{}{
"neptun": neptun,
})
if err != nil {
return err
}
var count int
if !rows.Next() {
return errors.New("not an admin")
}
rows.Scan(&count)
if count != 1 {
return errors.New("not an admin")
}
return nil
}