Exemple #1
0
func (user *User) Validate(password string) bool {
	key, err := common.Encrypt(password, user.Salt)
	if err != nil {
		return false
	}
	if len(key) != len(user.Password) {
		return false
	}
	for i := 0; i < len(key); i++ {
		if key[i] != user.Password[i] {
			return false
		}
	}
	return true
}
Exemple #2
0
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
}