Add create from email

This commit is contained in:
Achim Rohn
2025-07-27 19:15:16 +02:00
parent 258853857f
commit 868a167660
4 changed files with 15 additions and 9 deletions
+1 -1
View File
@@ -164,7 +164,7 @@ func (a *Auth) AddRoutes(e *echo.Echo) []ersteller_lib.Route {
userId, err := a.userRepo.GetUserId(data.Email)
if err != nil {
ersteller_lib.LogError("Failed to get user id: %v", err)
userId, err = a.userRepo.Create(data.Email)
userId, err = a.userRepo.CreateFromEmail(data.Email)
if err != nil {
ersteller_lib.LogError("Failed to create user: %v", err)
return err
+1 -1
View File
@@ -114,7 +114,7 @@ func RunKeycloakAuth(e *echo.Echo, environment KeycloakEnv, cookieStore *session
}
func createUser(gothUser goth.User, repo UserRepository) (int, error) {
return repo.Create(gothUser.Email)
return repo.CreateFromEmail(gothUser.Email)
}
func saveEmailToSessionStore(c echo.Context, sessionStore *sessions.CookieStore, email string, userId int, environment KeycloakEnv) error {
+1 -1
View File
@@ -2,5 +2,5 @@ package authentication
type UserRepository interface {
GetUserId(email string) (int, error)
Create(email string) (int, error)
CreateFromEmail(email string) (int, error)
}
+12 -6
View File
@@ -333,14 +333,14 @@ func (r *UserRepository) jsonToString(jsonData any) string {
return string(bytes)
}
func (u *UserRepository) DoesUserEmailExist(email string) (bool, error) {
sql, args, _ := u.dialect.From("user").
func (r *UserRepository) DoesUserEmailExist(email string) (bool, error) {
sql, args, _ := r.dialect.From("user").
Prepared(true).
Select(goqu.COUNT("email")).
Where(goqu.Ex{"email": email}).
ToSQL()
rows, err := u.connPool.Query(context.Background(), sql, args...)
rows, err := r.connPool.Query(context.Background(), sql, args...)
if err != nil {
ersteller_lib.LogError("failed to run sql query: %v", err)
return false, err
@@ -357,14 +357,14 @@ func (u *UserRepository) DoesUserEmailExist(email string) (bool, error) {
return false, nil
}
func (u *UserRepository) GetUserId(email string) (int, error) {
sql, args, _ := u.dialect.From("user").
func (r *UserRepository) GetUserId(email string) (int, error) {
sql, args, _ := r.dialect.From("user").
Prepared(true).
Select("id").
Where(goqu.Ex{"email": email}).
ToSQL()
rows, err := u.connPool.Query(context.Background(), sql, args...)
rows, err := r.connPool.Query(context.Background(), sql, args...)
if err != nil {
ersteller_lib.LogError("failed to run sql query: %v", err)
return -1, err
@@ -381,6 +381,12 @@ func (u *UserRepository) GetUserId(email string) (int, error) {
return -1, errors.New("did not find user with email " + email)
}
func (r *UserRepository) CreateFromEmail(email string) (int, error) {
return r.Create(User{
Email: email,
})
}
func (r *UserRepository) VerifyPassword(email string, password string) (bool, int, error) {
userId, err := r.GetUserId(email)
if err != nil {