Jump to content

FrontendUser: login, logout and register users / members


pwFoo

Recommended Posts

Take a look here how to manipulate forms:

https://bitbucket.org/pwFoo/frontenduser/wiki/Login extensions and plugins

https://bitbucket.org/pwFoo/frontenduser/wiki/Register extensions and plugins

https://bitbucket.org/pwFoo/frontenduser/wiki/Code snippets / Examples

You could try to hook FrontendUser::save

$fu->addHookBefore('save', function($event) {
    $userObj = wire('fu')->userObj;
    $form = wire('fu')->form;
    
    // Do...
});

 

Or username or email field

// hook after field processed (form need to be submitted)
$myField->addHookAfter('processInput', function($event) {
  $form = wire('fu')->form;
  $currentField = $event->object;

  // Do ...
}

 

 

  • Like 1
Link to comment
Share on other sites

@jmartsch

i've a own registerform created with FormHelper from @pwFoo
but not depending on FrontendUser and i use this code for usernam = email

 

//check for email duplication
if (email_valid($email) == true) {
	//email exists
	$out .= message("email exists...","danger");
} elseif ($password == ''){
	//no password is there
	$out .= message("no password...","danger");
} else {

//email is valid create so we first create a new user
$u = new User();
$u->of(false);
$u->name = $sanitizer->fieldName($email);
$u->email = $sanitizer->email($email);
.....save other fields go on...

you can use $sanatizer->fieldName in a hook on FrontendUser module, too i guess...
regards mr-fan

Link to comment
Share on other sites

Set a field to not required is done with FrontendUserRegisterEmailValidation "plugin"

            $form->get('password')->required = false;
            $form->get('EmailPreValidation')->required = false;

You can remove fields with required false 

            // prepare / modify registration form
            $form->remove($form->get('password'));

 

Link to comment
Share on other sites

I was scratching my head for hours because I couldn't get the add default role plugin to work.
It's probably only me who is so newbie at hooks. But anyway, just wanted to add here for those who have similar problems: 
(@pwFoo, you might want to add this extra line to the documentation ;) )

Hooks must be added before the processing! :-) 

I got my code working this way:

// Add default user role
$fu->addHookBefore('save', function($event) {
    $user = wire('fu')->userObj;
    $user->addRole('dress');
});

// Login after successful registration
$fu->addHookAfter('save', function($event) {
  $user = $event->object->form->fhValue('username','text');
  $pass = $event->object->form->fhValue('password','text');
  $this->session->login($user, $pass);
});

// process register / form submit
$fu->process($redirectDestination);

Thanks for the great module though, it saved me loads of time and it just works.
The email validations is just the icing on the cake. :)

  • Like 1
Link to comment
Share on other sites

I don't understand hooks or/and how the forms API works. I am trying to fix this bit:

Spoiler

$fullname = $modules->get('InputfieldText');
$fullname->label = 'Full name';
$fullname->attr('id+name','fullname');
$fullname->required = 1;
$fullname->fhSanitizer = 'text';

// Call hook after field is processed by PW form api
$fullname->addHookAfter('processInput', function($event) {
	$field = $event->object;
	$mySanitizedCustomInput = wire('fu')->form->fhValue($field->name);
	wire('fu')->userObj->fullname = $mySanitizedCustomInput;
});

$fu->addHookBefore('save', function($event) {
    $user = wire('fu')->userObj;
    $user->addRole('member');
});

 

The fullname apparently does not get saved along with the username when the validation email is sent; the pre-filled form only has the username.

I tried changing $fullname->addHookAfter to addHookBefore, but that made no difference.

I assume the $fullname->addHookAfter function adds the fullname field value to a userObj object, so that it gets saved along with the username and email address in $fu->addHookBefore ... , right?

Or is 'send email validation code' another hookable event than 'save'? Is there a list of available hookable events in the registration process?

Or are custom fields like fullname correctly stored with the userObj by this bit of code, only not automatically added to the pre-filled form by the module?

Should I hook into the validation email event, somehow, and add the fullname field?

 

Or are my assumptions about what goes on in this bit of code all wrong?

 

Link to comment
Share on other sites

You need to read the FrontendUser documentation examples I think ;)

Take a look here: https://bitbucket.org/pwFoo/frontenduser/wiki/Code snippets / Examples

<?php
$myField = $modules->get('InputfieldText');
$myField->label = $this->_('MyLabel');
$myField->attr('id+name', 'MyField');
//$myField->required = 1;
$myField->fhSanitizer = 'text';

// Call hook after field is processed by PW form api
$myField->addHookAfter('processInput', function($event) {
    $field = $event->object;

    // Value will be sanitized as text with "$sanitizer->text()"
    $mySanitizedCustomInput = wire('fu')->form->fhValue($field->name);

    // Do ...

    // Example: Add value to user during registration
    wire('fu')->userObj->myField = $mySanitizedCustomInput;

    // Need to set an field error?
    // $field->error('Custom field has an error...');
});

// Define the field before you call FU and add it as additional field...
$fu->register(array('username', 'email', 'password', $myField));

 

To extend the FrontendUser module it's needed to know how to use form api and hooks. FrontendUser is just based on PW basic features. 

Link to comment
Share on other sites

I'm no PHP or PW guru neither but this is how I see it (food for thought):

You created a new input field but didn't make it part of the wire('fu') object – hence pwFoo's comment. So when you get to the 

wire('fu')->process();

your hook will be able to process your input field. This is what happens here:

// Define the field before you call FU and add it as additional field...
$fu->register(array('username', 'email', 'password', $myField));

I'm sure you will get there soon based on the example above.

Link to comment
Share on other sites

@Jozsef, I have that line in my full code:

Spoiler
<?php
$fu = $modules->get('FrontendUser');
 
$redirectDestination = htmlentities($_SERVER['REQUEST_URI']);
 
$fullname = $modules->get('InputfieldText');
$fullname->label = 'Full name';
$fullname->attr('id+name','fullname');
$fullname->required = 1;
$fullname->fhSanitizer = 'text';
 
// Call hook after field is processed by PW form api
$fullname->addHookAfter('processInput', function($event) {
 $field = $event->object;
 $mySanitizedCustomInput = wire('fu')->form->fhValue($field->name);
 wire('fu')->userObj->fullname = $mySanitizedCustomInput;
});
 
// include fullname in the wire('fu') object?
$fu->register(array($fullname,'username', 'email', 'emailValidation', 'password'));
 
$fu->addHookBefore('save', function($event) {
    $user = wire('fu')->userObj;
    $user->addRole('member');
});
 
$fu->addHookAfter('FrontendUser::save', function($event) use($fu, $input) {
 if($event->return === true) {
  if(!empty($input->post->password) && !count($fu->form->getErrors())) {
   $subject = "Welcome - your new account at mydomain.com";
   $emailContentPlain = "Your connection details:\nUsername: {$fu->userObj->name}\nEmail: {$fu->userObj->email}\nPassword: {$input->post->password}";
   $mail = wireMail();
   $mail->to($fu->userObj->email);
   $mail->subject($subject);
   $mail->body($emailContentPlain);
   $mail->send();
  }
 }
});
 
// Login after successful registration
$fu->addHookAfter('save', function($event) {
  $user = $event->object->form->fhValue('username','text');
  $pass = $event->object->form->fhValue('password','text');
  $this->session->login($user, $pass);
});
 
// Login with the email address instead of the username
$fu->addHookBefore('FrontendUser::auth', function($event) {
        $email = wire('fu')->form->fhValue('username', 'email');
        $loginUser = wire('users')->get("email={$email}");
        if ($loginUser instanceof User && !$loginUser->isGuest()) {
            $userObj = $event->arguments[0];
            $userObj->name = $loginUser->name;
        }
});
 
if ($user->isGuest()) {
$fu->process($redirectDestination);
echo $fu->render();
echo 'or log in here if you already have an account';
echo $fu->login(null, $redirectDestination);
}
 
if($user->isLoggedin()) {
 if (isset($_GET['logout'])) $fu->logout($redirectDestination);
 echo "<a href='?logout'>log out</a>";
 echo '<h3>Hello '. $user->fullname .'</h3>';
}
?>

So I guess it's in the wrong place? I'll try it with a few things turned around and report back...

Edit: Moved the line to current position. Fullname is still blank after the validation email. Will dig into the documentation when I have time and report back...

What does '// Call hook after field is processed by PW form api' do? I thought that bit added fullname to userObj.

Should I replace mySanitizedCustomInput with 'fullname' or 'field' or something else?

Which hookable events are there; processInput, save, ...? Is 'send email validation code' another hookable event?

To be clear, the fullname does get saved fine, but only after manually entering it again after the email validation. Fullname is not saved along with the username before the validation process or is not added to the custom field in the pre-filled form. Or is that the normal process?

 

Link to comment
Share on other sites

  • 1 month later...

Regarding the email validation, it works great... except when my client tried in on his fully updated iPhone 5, and somehow the session gets lost when he clicks on the link in the activation email he receives: he ends up with the initial default form (username + email), and he's stuck in a loop. I've been trying to reproduce it, but to no avail. He's using Safari, and not in Private Mode (which by the way does break the activation).

Have you any of you guys run into this issue? Or have any clue what could be happening? Thanks in advance.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Thanks pwfoo for your very useful module. I'm still very new and I got an issue about the password validation as the attached image. The text "Not yet validInvalidToo shortToo commonWeakOkGoodExcellent" shown when I get in my Register page. It not work like in the back-end. How to fix it ?

Screenshot - 02092017 - 09:10:47 PM.png

Link to comment
Share on other sites

1 hour ago, pwFoo said:

The forms are unstyled. Backend styles are needed. 

FormHelper Extra can load the styles, but I think that wasn't added to FrontendUser as feature / option.

https://bitbucket.org/pwFoo/formhelper/wiki/Documentation#markdown-header-formhelperextra

Thank you for you message. I tried to find the information in your link but it's not for a beginner like me, and also in this forum page by page but no one got stuck like me. :'(

Could you please suggest me how to make it works ?

Link to comment
Share on other sites

FormHelper is loaded here:

protected function buildForm($fields) {
        $this->form = $this->modules->get('FormHelper')->create();

https://bitbucket.org/pwFoo/frontenduser/src/0070dc3106945198ac438bab3ab742b1784080e9/FrontendUser/FrontendUser.module?at=master&fileviewer=file-view-default#FrontendUser.module-176

FormHelper create()

https://bitbucket.org/pwFoo/formhelper/src/d85395b2dd7352a0af7aadeeec5125ba3d9d0bb0/FormHelper/FormHelper.module?at=master&fileviewer=file-view-default#FormHelper.module-61

 

The create() method need additional parameters which need to be passed from template -> FrontendUser -> FormHelper, but it isn't implemented in the FrontendUser module...

 

Or just load / create the needed styles. Here is the function is used by FormHelper

    /**
     * Use admin / custom form styles 
     * @param boolean|string $style true or an css file to load
     */
    protected function pwAdminStyles($style) {
        $this->config->styles->add($this->config->urls->adminTemplates . 'styles/font-awesome/css/font-awesome.min.css');
        
        if ($style === true) {
            $style = $this->config->urls->AdminThemeDefault . 'styles/main-classic.css';
        }
        $this->config->styles->add($style);
    }

https://bitbucket.org/pwFoo/formhelper/src/d85395b2dd7352a0af7aadeeec5125ba3d9d0bb0/FormHelper/FormHelper.module?at=master&fileviewer=file-view-default#FormHelper.module-192

Link to comment
Share on other sites

  • 4 weeks later...
On 2/9/2017 at 10:19 AM, Thachakrit said:

Thanks pwfoo for your very useful module. I'm still very new and I got an issue about the password validation as the attached image. The text "Not yet validInvalidToo shortToo commonWeakOkGoodExcellent" shown when I get in my Register page. It not work like in the back-end. How to fix it ?

Screenshot - 02092017 - 09:10:47 PM.png

 


I'm having the same problem. It's unclear from the previous post on how to fix this issue.
Shouldn't this be included in module package?

Link to comment
Share on other sites

Hi @jasonS,

it looks like the module isn't compatible to the used / latest PW version. I think password inputfield changed?
Can't remember that problem with PW 2.7 and PW Dev 3.x..? 

Anyone tested it with different / older PW versions?

Sorry, I haven't a test / dev environment at the moment and can't spend much time to change / debug it. 

Link to comment
Share on other sites

On 9.2.2017 at 3:19 PM, Thachakrit said:

Thanks pwfoo for your very useful module. I'm still very new and I got an issue about the password validation as the attached image. The text "Not yet validInvalidToo shortToo commonWeakOkGoodExcellent" shown when I get in my Register page. It not work like in the back-end. How to fix it ?

Screenshot - 02092017 - 09:10:47 PM.png

@Thachakrit @JasonS @pwFoo To get the password validation working you need to add styles and scripts from ProcessWire Admin.

The following is untested with this module but should work if you add it to the page where you do the output of the module:

 <script type="text/javascript" src="/wire/modules/Inputfield/InputfieldPassword/complexify/jquery.complexify.min.js"></script>
    <script type="text/javascript" src="/wire/modules/Inputfield/InputfieldPassword/complexify/jquery.complexify.banlist.js"></script>
    <script type="text/javascript" src="/wire/modules/Jquery/JqueryCore/xregexp.js?v=1466417387"></script>
    <script src="/wire/modules/Inputfield/InputfieldPassword/InputfieldPassword.min.js?v=101-1466417387"></script>

EDIT: Forgot to add the stylesheet. It resides under

/wire/modules/Inputfield/InputfieldPassword/InputfieldPassword.css

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...