From dd43254ec33190e89e82b247157edcfa58bc6b53 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 19 Jan 2023 10:54:26 +0200 Subject: [PATCH] feat(sdbapi): helper for getting user by email --- sdbapi/db/user.go | 27 +++++++++++++++++++++++++++ sdbapi/handlers/gameinfo.go | 6 +++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 sdbapi/db/user.go diff --git a/sdbapi/db/user.go b/sdbapi/db/user.go new file mode 100644 index 0000000..2766937 --- /dev/null +++ b/sdbapi/db/user.go @@ -0,0 +1,27 @@ +/* + * This file is part of sdbapi + * Copyright (C) 2022 Jonni Liljamo + * + * 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 +} diff --git a/sdbapi/handlers/gameinfo.go b/sdbapi/handlers/gameinfo.go index 67fe39c..2c8bb8e 100644 --- a/sdbapi/handlers/gameinfo.go +++ b/sdbapi/handlers/gameinfo.go @@ -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 -- 2.44.1