func GenerateDeviceToken() (string, error) { //retry 5 times before it fail for i := 0; i < 5; i++ { token, err := common.Token(16) if err != nil { return "", err } has, err := mysqlEngine.Get(&AggregationDevice{Token: token}) if err != nil { return "", err } if !has { return token, nil } } return "", errors.New("Generate token failed") }
func CreateUser(email string, password string, level string) (*User, error) { salt, err := common.Salt() if err != nil { return nil, err } key, err := common.Encrypt(password, salt) if err != nil { return nil, err } token, err := GenerateUserToken() if err != nil { return nil, err } expireAt := time.Now().Add(userTokenExpireDuration) user := User{ Email: email, Password: key, Salt: salt, Token: token, Level: level, IsVerified: false, TokenExpireAt: expireAt, } _, err = mysqlEngine.Insert(&user) if err != nil { return nil, err } verificationToken, err := common.Token(16) verificationExpireAt := time.Now().Add(userVerificationTokenExpireDuration) if err == nil { mysqlEngine.Insert(&UserVerification{UserId: user.Id, Token: verificationToken, TokenExpireAt: verificationExpireAt}) } return &user, nil }