go-urlsh/internal/db/apikey.go

29 lines
549 B
Go
Raw Permalink Normal View History

2023-05-05 15:16:54 +02:00
package db
import (
"context"
"log"
2024-05-04 17:06:01 +02:00
"code.lila.network/adoralaura/go-urlsh/models"
2023-05-05 15:16:54 +02:00
)
// IsApiKeyValid checks the database if ApiKey val is valid.
//
// Returns true if it's valid, false if not.
func IsApiKeyValid(val string) bool {
if val == "" {
return false
}
count, err := models.DB.NewSelect().Model((*models.ApiKey)(nil)).Where("key = ?", val).Count(context.Background())
if err != nil {
log.Printf("Error checking apikey validity for key %v\n", val)
return false
}
if count < 1 {
return false
} else {
return true
}
}