/* ScheduleTogether Backend Copyright (C) 2024, Marco Vitchi Thulin DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License version 3 for more details. This program incorporates external libraries for certain functionalities. These libraries are covered by their respective licenses, and their usage agreements are as outlined in their respective documentation or source code. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package accounts import ( "github.com/kataras/iris/v12" db "git.zervo.org/scheduletogether/backend/internal/database" "git.zervo.org/scheduletogether/backend/pkg/helpers/cryptography" "git.zervo.org/scheduletogether/backend/pkg/types" ) // AccountRegister_Service registers a new user func AccountRegister_Service(ctx iris.Context, req types.AccountRegisterRequest) { // Validate user properties err := validateUserProperties(ctx, req) if err != nil { return } // Hash password hashedPassword, err := cryptography.HashString(req.Password) if err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "An error occured while registering your account, please try again later", }) logboi.Warn("Failed to hash password: " + err.Error()) return } // Generate uuid userid, err := generateUUID() if err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "An error occured while registering your account, please try again later", }) logboi.Warn("Failed to generate UUID: " + err.Error()) return } // Create new user record in database newUser := db.User{ FirstName: req.FirstName, LastName: req.LastName, Email: req.Email, Username: req.Username, Role: "user", // TODO: implement proper roles? Uuid: userid, Hash: string(hashedPassword), Verified: false, } // Start a transaction tx := db.Db.Begin() err = tx.Create(&newUser).Error if err != nil { tx.Rollback() ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "An error occured while registering your account, please try again later", }) logboi.Warn("An error occured while creating account in database: " + err.Error()) return } // Generate token token, err := cryptography.GenerateToken(7) if err != nil { tx.Rollback() ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "An error occured while registering your account, please try again later", }) logboi.Warn("Failed to generate token: " + err.Error()) return } // Create verification req record in database newVerify := db.RegisterVerification{ Uuid: userid, Token: token, } err = tx.Create(&newVerify).Error if err != nil { tx.Rollback() ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "An error occured while registering your account, please try again later", }) logboi.Warn("An error occured while adding account verification req to database: " + err.Error()) return } // TODO: Send verification email thingy // Commit the transaction if everything is successful err = tx.Commit().Error if err != nil { tx.Rollback() ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "Something went wrong, please try again later", }) return } logboi.Info("Registered new user! Username: " + req.Username + ", Verification token: " + token) ctx.JSON(types.AccountRegisterResponse{ Message: "User registered successfully", Uuid: newUser.Uuid, MustVerify: true, }) }