Exemplo n.º 1
0
func Login(db *mgo.Database, login, password, ip string, sessionDuration time.Duration) (*models.User, error) {
	var user *models.User

	db.C("users").Find(bson.M{"login": login}).One(&user)
	if user == nil {
		return nil, errors.New("User with given login doesn't exist")
	}

	if user.PasswordHash != utils.Encrypt(password) {
		return nil, errors.New("Invalid password")
	}

	user.LastSID = models.GenerateSID(login, ip, time.Now().UTC())
	user.LastSessionStart = time.Now().UTC()
	user.LastSessionEnd = time.Now().UTC().Add(sessionDuration)

	db.C("users").UpdateId(user.Id, bson.M{
		"$set": bson.M{
			"lastSID":          user.LastSID,
			"lastSessionStart": user.LastSessionStart,
			"lastSessionEnd":   user.LastSessionEnd,
		},
	})

	return user, nil
}
Exemplo n.º 2
0
func SignUp(db *mgo.Database, login, email, password string) (*models.User, error) {
	if !models.CheckSignUpDataCorrectness(login, email, password) {
		return nil, errors.New("Invalid login, email or password")
	}

	var user *models.User

	db.C("users").Find(bson.M{"login": login}).One(&user)
	if user != nil {
		return nil, errors.New("Login already taken")
	}

	db.C("users").Find(bson.M{"email": email}).One(&user)
	if user != nil {
		return nil, errors.New("Email already taken")
	}

	user = &models.User{bson.NewObjectId(), login, utils.Encrypt(password), email, "", time.Unix(0, 0), time.Unix(0, 0)}

	db.C("users").Insert(user)

	return user, nil
}