2

I'm trying to create a user with the Zizaco/confide library, the user data comes from Facebook and not from a form.

I try this :

$user_profile = Facebook::api('/me','GET');

$new_user = new User;

$new_user->username = $user_profile['name'];
$new_user->password = Hash::make('secret');
$new_user->email = $user_profile['email'];
$new_user->confirmation_code = '456456';
$new_user->confirmed = true;

$new_user->save();

but it doesn't save the user. Any help ?

3
  • does it silently go over it or or throw any error? any logs in app/storage/logs/laravel.log? Commented Mar 28, 2014 at 14:04
  • Did you find a solution to this? I'm experiencing the same problem. Commented Apr 13, 2015 at 14:42
  • I posted the answer, the problem were the validation rules Commented Apr 13, 2015 at 21:00

2 Answers 2

1

I found the problem, the confide library sets some default rules to create the user, you need to pass this rules to save a user:

public static $rules = array(
    'username' => 'required|alpha_dash|unique:users',
    'email' => 'required|email|unique:users',
    'password' => 'required|between:4,11|confirmed',
    'password_confirmation' => 'between:4,11',
);

with this example it works:

$user = new User();

$user->username = 'asdasd';
$user->email = '[email protected]';
$user->password = '12312312';
$user->password_confirmation = '12312312';

$user->save();

Mabe the method should give you some information when you don't pass the rules.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe it's a pretty late answer, but I'm posting this for future reference because I was myself looking for an answer to the same question, How to create a user programatically in zizaco/confide? or more generally, how to bypass zizaco/confide's requirement for setting a password when saving a user for the first time?

well, the answer is pretty simple, thanks to the new architecture which Zizaco implemented in the 4.* branch, it's now possible to register a new user validator class see more at the package's readme in short all you need in this very case though, is just to extend the current User validator and override validatePassword() to make it accept empty passwords for new users.

Below is an example implementation:

In routes.php

 // we need to register our validator so that it gets used instead of the default one
 // Register custom Validator for ConfideUsers

 App::bind('confide.user_validator', 'MyUserValidator');

In app/models/MyUserValidator.php (that's basically a copy of the function in the class, simply just added a check whether this is a old user or not (if the user has an ID then this is an update operation) if this is a new user, the method always returns true!

/**
 * Class MyUserValidator
 * Custom Validation for Confide User
 */
class MyUserValidator extends \Zizaco\Confide\UserValidator //implements \Zizaco\Confide\UserValidatorInterface
{

    /**
     * Validates the password and password_confirmation of the given
     * user
     * @param  ConfideUserInterface $user
     * @return boolean  True if password is valid
     */
    public function validatePassword(\Zizaco\Confide\ConfideUserInterface $user)
    {
        $hash = App::make('hash');

        if($user->getOriginal('password') != $user->password && $user->getOriginal('id')) {
            if ($user->password == $user->password_confirmation) {

                // Hashes password and unset password_confirmation field
                $user->password = $hash->make($user->password);

                unset($user->password_confirmation);
                return true;
            } else {
                $this->attachErrorMsg(
                    $user,
                    'validation.confirmed::confide.alerts.wrong_confirmation',
                    'password_confirmation'
                );
                return false;
            }
        }
        unset($user->password_confirmation);
        return true;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.