A sdbapi/db/user.go => sdbapi/db/user.go +27 -0
@@ 0,0 1,27 @@
+/*
+ * This file is part of sdbapi
+ * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
+ *
+ * Licensed under GPL-3.0-only.
+ * See LICENSE for licensing information.
+ */
+
+package db
+
+import (
+ "api/models"
+
+ "api/apierror"
+ "errors"
+)
+
+// get a user with an email address
+func GetUserByEmail(email string) (models.User, error) {
+ var user models.User
+ user_record := DbConn.Where("email = ?", email).First(&user)
+ if user_record.Error != nil {
+ return models.User{}, errors.New(apierror.UserNotFound)
+ }
+
+ return user, nil
+}
M sdbapi/handlers/gameinfo.go => sdbapi/handlers/gameinfo.go +3 -3
@@ 39,9 39,9 @@ func GameInfo(c *gin.Context) {
if claims, ok := token.Claims.(*auth.JWTClaims); ok && token.Valid {
// Check if the email in the claims matches a user in the database
- var user models.User
- user_record := db.DbConn.Where("email = ?", claims.Email).First(&user)
- if user_record.Error != nil {
+ // NOTE: Technically we should never end up here, but just in-case.
+ _, err := db.GetUserByEmail(claims.Email)
+ if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": apierror.NotAuthorized})
c.Abort()
return