Add activation-codes and registration. Added tooltips. Added player-score-cache-display in mainpage.
This commit is contained in:
29
auth.go
29
auth.go
@@ -2,9 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"InfrantrySkillCalculator/models"
|
||||
"InfrantrySkillCalculator/utils"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
@@ -25,7 +28,9 @@ func getUserPassword(username string) (string, error) {
|
||||
var user models.User
|
||||
|
||||
if err := models.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
||||
log.Fatal(err)
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -58,3 +63,25 @@ func AuthRequired() gin.HandlerFunc {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func isValidCode(code string) bool {
|
||||
var activationCode models.ActivationCode
|
||||
if err := models.DB.Where("code = ?", code).First(&activationCode).Error; err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if activationCode.Code == code && !activationCode.Used {
|
||||
models.DB.Model(&activationCode).Updates(map[string]interface{}{
|
||||
"Code": code,
|
||||
"Used": true,
|
||||
})
|
||||
|
||||
newCode := utils.GenerateActivationCode()
|
||||
newCodeObj := models.ActivationCode{Code: newCode, Used: false}
|
||||
models.DB.Create(&newCodeObj)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user