Removed activation file
parent
63f84581fb
commit
9a78c588a1
|
@ -1,65 +0,0 @@
|
||||||
package user
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/database"
|
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/structs"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
func createUserActivation(userId string) (string, error) {
|
|
||||||
userActivationId, err := generateRandomString(16, 1)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Warnln("Failed to generate random string")
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
db := database.DB
|
|
||||||
|
|
||||||
db.Create(structs.UserActivation{Id: userActivationId, UserId: userId, Expires: getUserActivationExpiresTime()})
|
|
||||||
|
|
||||||
return userActivationId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func deleteExpiredUserActivations(db *gorm.DB) {
|
|
||||||
var res string
|
|
||||||
|
|
||||||
db.Raw("DELETE FROM user_activations WHERE expires < ?", time.Now()).Scan(&res)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ActivateUser(c *fiber.Ctx) error {
|
|
||||||
// swagger:operation POST /user/activate/:id User activation
|
|
||||||
// ---
|
|
||||||
// summary: Activate user
|
|
||||||
// responses:
|
|
||||||
// '200':
|
|
||||||
// description: User was activated
|
|
||||||
// '401':
|
|
||||||
// description: Activation Id is incorrect or expired
|
|
||||||
|
|
||||||
db := database.DB
|
|
||||||
|
|
||||||
deleteExpiredUserActivations(db)
|
|
||||||
|
|
||||||
userActivation := structs.UserActivation{Id: c.Params("id")}
|
|
||||||
|
|
||||||
db.Select("user_id").Where("id = ?", c.Params("id")).Find(&userActivation)
|
|
||||||
|
|
||||||
if userActivation.UserId == "" {
|
|
||||||
return c.SendStatus(fiber.StatusUnauthorized)
|
|
||||||
}
|
|
||||||
|
|
||||||
db.Delete(userActivation)
|
|
||||||
|
|
||||||
db.Model(structs.User{Id: userActivation.UserId}).Update("state", 0)
|
|
||||||
|
|
||||||
return c.SendStatus(fiber.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUserActivationExpiresTime() time.Time {
|
|
||||||
return time.Now().Add(time.Minute * time.Duration(cfg.Settings.Expires.UserActivation))
|
|
||||||
}
|
|
Loading…
Reference in New Issue