예제 #1
0
파일: users.go 프로젝트: aodin/volta
// createUser checks for a duplicate email before inserting the user.
// Email must already be normalized.
func (m *UserManager) createUser(user *User) error {
	email := sol.Select(
		Users.C("email"),
	).Where(Users.C("email").Equals(user.Email)).Limit(1)

	var duplicate string
	m.conn.Query(email, &duplicate)
	if duplicate != "" {
		return fmt.Errorf(
			"auth: user with email %s already exists", duplicate,
		)
	}

	// Insert the new user
	m.conn.Query(postgres.Insert(Users).Values(user).Returning(), user)
	return nil
}
예제 #2
0
파일: tokens.go 프로젝트: aodin/volta
// Create creates a new token for the user. It will panic on error. The user
// ID must exist.
func (m *TokenManager) ForeverToken(user User) (token Token) {
	token.UserID = user.ID
	token.manager = m

	// Generate a new token
	for {
		token.Key = m.keyFunc()

		// No duplicates - generate a new key if this key already exists
		stmt := sol.Select(
			Tokens.C("key"),
		).Where(Tokens.C("key").Equals(token.Key)).Limit(1)
		var duplicate string
		m.conn.Query(stmt, &duplicate)
		if duplicate == "" {
			break
		}
	}

	// Insert the token into the database
	m.conn.Query(postgres.Insert(Tokens).Values(token).Returning(), &token)
	return
}