Jump to content
pwFoo

FrontendUser: login, logout and register users / members

Recommended Posts

FrontendUser uses a temp PW User object. All existing fields should be saved, but it's done via PW api, not the PW admin create user process (haven't looked into it...).

Send email after user registration? You could write a plugin hook after FrontendUser save() method...

    /**
     * Save the temp User object
     * @param User $user Temp User object to save
     * @return boolean Sucessful (true) saved or not (false)
     */
    protected function ___save($user) {
        if (empty($user->name) || empty($user->email) || $user->pass->hash == '') {
            return $this->_('Register process unexpected failed!');
        }
        if ($user->save()) {
            return true;
        }
        return $this->_('User registration failed!');
    }

Hook after save() method. If return value  === true (exactly true! not "== true") you can send a mail via PW api or load a module to send the email...

  • Like 1

Share this post


Link to post
Share on other sites

Thats actually my code. It works for register but dont work for the Email new user. 

Ive thought i could write i plugin hook but i cant :D

So please help where is the mistake ? 

$fu = $modules->get('FrontendUser');

// prepare login form (default parameters)
$fu->login();

// Default parameter
//$fu->login(array('username', 'password'));

// Additional LoginPersist and ProcessForgotPassword module integration (built-in)
$fu->login(array('username', 'password',  'forgot'));

// process login / form submit
$fu->process('../objekte/');

// output form
echo $fu->render();


$modules->get('EmailAdminNewUser'); // lade Modul um den Admin zu benachrichtigen 
$u_vorname = $modules->get('InputfieldText');
$u_vorname->label = $this->_('Vorname');
$u_vorname->attr('id+name', 'u_vorname');
$u_vorname->required = 1;
$u_vorname->fhSanitizer = 'text';

$nachname = $modules->get('InputfieldText');
$nachname->label = $this->_('Nachname');
$nachname->attr('id+name', 'nachname');
$nachname->required = 1;
$nachname->fhSanitizer = 'text';

$unternehmen = $modules->get('InputfieldText');
$unternehmen->label = $this->_('Unternehmen');
$unternehmen->attr('id+name', 'unternehmen');
$unternehmen->required = 1;
$unternehmen->fhSanitizer = 'text';

$strnr = $modules->get('InputfieldText');
$strnr->label = $this->_('Strasse / Hausnummer');
$strnr->attr('id+name', 'strnr');
$strnr->required = 1;
$strnr->fhSanitizer = 'text';

$plz = $modules->get('InputfieldText');
$plz->label = $this->_('Plz / Ort');
$plz->attr('id+name', 'plz');
$plz->required = 1;
$plz->fhSanitizer = 'text';

$tel = $modules->get('InputfieldText');
$tel->label = $this->_('Telefonnummer');
$tel->attr('id+name', 'tel');
$tel->required = 1;
$tel->fhSanitizer = 'text';

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

    if(!count($form->getErrors())) {

        wire('fu')->userObj->u_vorname = $form->fhValue('u_vorname', 'text');
        wire('fu')->userObj->u_nachname = $form->fhValue('nachname', 'text');
        wire('fu')->userObj->u_unternehmen = $form->fhValue('unternehmen', 'text');
        wire('fu')->userObj->u_hausnummer = $form->fhValue('strnr', 'text');
        wire('fu')->userObj->u_plzort = $form->fhValue('plz', 'text');
        wire('fu')->userObj->u_telefonnummer = $form->fhValue('tel', 'text');

    
    }
});


// prepare register form
$fu->register();
$fu->register(array('username',  $u_vorname, $nachname, $unternehmen , $strnr, $plz, $tel, 'email', 'password'));
$fu->process('/');
echo $fu->render();

The admin need an email with new user details and a link to activate this user. 

Because there are 3 different view gropus of users. 

Where i put the code for my custom email to the user admin ? 

I tried to understand it with the after hooks but how to hook the press on the registeration button ? 

Thanks 

PS: I have some problems with the forget user funktion. I see it twice ? 

if im logged in i see blank place. can i set a message like you are logged in currently ? 

Share this post


Link to post
Share on other sites

Hi,

$modules->get('EmailAdminNewUser'); // lade Modul um den Admin zu benachrichtigen 

I haven't used or seen EmailAdminNewUser before. So I don't know how it should work / triggered...

    public function init() {
        if($this->data['generatePassword']){
            $this->addHookBefore('InputfieldPassword::render', $this, 'notRequiredNote');
            $this->addHookBefore('InputfieldPassword::processInput', $this, 'removeRequired');
        }
        if(wire('page')->process == "ProcessUser") $this->addHookAfter('ProcessPageEdit::buildFormContent', $this, 'addEmailFields');
        $this->addHookAfter('Password::setPass', $this, 'getPassword');
        $this->pages->addHookBefore('saveReady', $this, 'sendNewUserEmail');
    }

FrontendUser "save()" method won't trigger it I think...

EmailAdminNewUser will only work if it hooks into $user->save().

Share this post


Link to post
Share on other sites

hmm but it doesent relly work for me still ? 

why ? 

The testmail works but the Module not really.

 

Share this post


Link to post
Share on other sites

hmm but it doesent relly work for me still ? 

why ? 

The testmail works but the Module not really.

Try putting $modules->get('EmailAdminNewUser');

After

$fu = $modules->get('FrontendUser');

At least that is how I have it.

Do you have this in site/config.php:

/**
* adminEmail: address to send optional fatal error notifications to.
*
*/
$config->adminEmail = 'name@exampledomain.com';

Because my module is getting the email with

wire('config')->adminEmail

I did notice that I didn't even have that config set, but it still always worked for me..

Share this post


Link to post
Share on other sites

I haven't take a look into it, but add hooks have to be done before $fu->process(). After this method the form is processed by PW.

Why not send the email by wireMail() with custom content and recipient?

$mail = wireMail();
$mail->to($adminEmail);
$mail->subject($subject);

// html body
//$mail->bodyHTML($emailContentHtml);

// plain body
$mail->body($emailContentPlain);

$mail->send();

Could be done hooked after FrontendUser::save.

  • Like 1

Share this post


Link to post
Share on other sites

I haven't take a look into it, but add hooks have to be done before $fu->process(). After this method the form is processed by PW.

Why not send the email by wireMail() with custom content and recipient?

$mail = wireMail();
$mail->to($adminEmail);
$mail->subject($subject);

// html body
//$mail->bodyHTML($emailContentHtml);

// plain body
$mail->body($emailContentPlain);

$mail->send();

Could be done hooked after FrontendUser::save.

Thanks heaps! Now I can throw away my silly little module made in a hurry.

Here is a solution that will email to all your superusers, thanks to LostKobrakai for being there on IRC to give advice:

  $form->addHookAfter('FrontendUser::save', function() use($fu, $input) {
    $superusers = wire('users')->find("roles=superuser");
    $emails = $superusers->explode('email');
    $subject = "A new user registered at the site: " . $fu->userObj->fullname;
    $emailContentPlain = "Go and activate them: http://www.mysite.fi/myadmin/";
    $mail = wireMail();
    $mail->to($emails);
    $mail->from('admin@mysite.fi');
    $mail->subject($subject);

    // plain body
    $mail->body($emailContentPlain);

    $mail->send();
  });
 
  $fu->process($redirectDestination);

Note: wire('pages')->find("template=user.. did not work. It had to be wire('users') !!

$fu->userObj->fullname; is getting the value of my extra field, fullname.

  • Like 1

Share this post


Link to post
Share on other sites

Hey there pwFoo this is great! Great module you wrote here. I am working on implementing it into my site and was wondering if I am misunderstanding how to set up registration with email validation. The flow that I am used to on the net is :

register with information - > submit

see splash screen that reminds the user to check their email to confirm

follow email link -> enable account (sometimes this is the step where the user sets a permanent password if none is entered on the sign up form)

redirect to profile page for more information on the user 

How would you recommend that I go about implementing this module in order to accomplish this? 

Share this post


Link to post
Share on other sites

Take a look at the FrontendUserRegisterEmailValidation submodule. It adds an email validation step before the new user will be registered.

Just load the plugin as additional field "emailValidation".

$fu->register(array('username', 'email', 'emailValidation', 'password'));
  • Like 1

Share this post


Link to post
Share on other sites

Apologies for not being more specific. I set the option for email validation and have noticed that the email contains a validation token. Is there any way to implement a flow that allows for a user to follow the link using the token instead of first having to enter the code that was emailed. I would like to just have the user follow the link to the password reset page where they would then enter their own password. Then on reset the user would be automatically logged in. 

Share this post


Link to post
Share on other sites

The Email should contain the token and a link to the second step of user registration form (filled with username, email address and token).

Password reset isn't handled by FrontendUser module, it just uses the ProcessForgotPassword core module.

Your use case is possible and the needed snippets and examples should be here in the forum, but you need to extend FrontendUser to work as you want it. I implemented ProcessForgotPassword because it should be secure and do the password reset job.

  • Like 1

Share this post


Link to post
Share on other sites

Sorry, I wasn't intending to sound critical  :). I just didn't want to start modifying anything before I fully understood the intended behaviour of the module. I now have it implemented and the css is now in place. I have one issue though. When registering you first input your username and your email. When you click to send the code the form posts back as it should, but then the error gets tripped before the whole form has been submitted "Email validation code NOT match!". I think I could locate a fix in the code but I was wondering if this is something that you were planning on dealing with in your own code before I go hacking at the module.

Share this post


Link to post
Share on other sites

It was important to check token mismatch and changed / duplicate username or email address before a new user is saved.

I think you shouldn't use my plugin. Write a submodule which isn't based on FrontendUserRegisterEmailValidation.

Just register an unpublished user. Send an activation mail via hook after save(). User activation could be done as independent (sub-)module.

Share this post


Link to post
Share on other sites

FrontendUser is based on the PW form api and PW inputfields.

Haven't links here, but just search the forum.

https://processwire.com/talk/topic/9467-add-a-css-class-to-an-inputfield-on-render/

https://processwire.com/talk/topic/2089-create-simple-forms-using-api/

I hope that will help you to style your form.

If someone build a base / example style I'll add it to the module / documentation.

  • Like 2

Share this post


Link to post
Share on other sites

Get it to work only with css. Thank you for your links.

How can i change the placeholders inside the textfields? It says username, password and login.

How could i translate that?

Share this post


Link to post
Share on other sites

Get it to work only with css. Thank you for your links.

How can i change the placeholders inside the textfields? It says username, password and login.

How could i translate that?

In PW settings - languages - your lang - select file for translation and give the module file.

  • Like 1

Share this post


Link to post
Share on other sites

In PW settings - languages - your lang - select file for translation and give the module file.

This was new to me. Thank you very much! :frantics:

Share this post


Link to post
Share on other sites

I have some Redirect Issues on Logout in Firefox. When I press Logout in Chrome everything seems working. In FF ive got endless redirects. The way I lougout is:

$fu = $modules->get('FrontendUser');
$fu->logout();

Runs on an own page, means not on the same where the Login/Register is located (There is only a Link to the Logout Page to print a feedback message). Whats the fault? I dont use redirect because I dont need any. 

By the way: I find no elegant way to send an email, when some new user is registrated. Is there any way to send an email out of the box from this module? Every extern way means to send emails in a very general way without events fired directly from my registration form.

Share this post


Link to post
Share on other sites

Easiest way to deal with logging out is to just make a two line template at /logout/

<?php
     if($user->isLoggedin()) $session->logout();
     $session->redirect($pages->get('/')->url);
?>

Share this post


Link to post
Share on other sites

Easiest way to deal with logging out is to just make a two line template at /logout/

<?php
     if($user->isLoggedin()) $session->logout();
     $session->redirect($pages->get('/')->url);
?>

We take a look at the modules logout() method ;)

    public function ___logout ($redirect) {
        $this->session->logout();
        $this->session->redirect($redirect, false);
    }

So just do what you want in the template.

With the redirect above and the modules logout method it looks like that.

// logout the current user
$fu->logout($pages->get('/')->url);

If you use a combined login / logout page or a redirect to the current page you need if / else in the template like that.

if ($user->isGuest()) {
    // do login or just write a "You're logged out..." message 
}
else {
    // do logout
}

I could add a check to prevent a loop, but with a redirect to the current page you have to handle it with if / else to serve the correct page for guests / loggedin users...

Edited by pwFoo
  • Like 2

Share this post


Link to post
Share on other sites

Thank you a lot. I think it would be enough to mention this in your documentation. I was really confused about the endless loop redirects.

And is there any ability to send mails? Because I have the problem that any redirect is looses the data from posting the form. It would be helpful to send the admin an email that a new user is registered.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      A community member raised a question and I thought a new sanitizer method for the purpose would be useful, hence...
      Sanitizer Transliterate
      Adds a transliterate method to $sanitizer that performs character replacements as defined in the module config. The default character replacements are based on the defaults from InputfieldPageName, but with uppercase characters included too.
      Usage
      Install the Sanitizer Transliterate module.
      Customise the character replacements in the module config as needed.
      Use the sanitizer on strings like so:
      $transliterated_string = $sanitizer->transliterate($string);
       
      https://github.com/Toutouwai/SanitizerTransliterate
      https://modules.processwire.com/modules/sanitizer-transliterate/
       
    • By dimitrios
      Hello,
      this module can publish content of a Processwire page on a Facebook page, triggered by saving the Processwire page.
      To set it up, configure the module with a Facebook app ID, secret and a Page ID. Following is additional configuration on Facebook for developers:
      Minimum Required Facebook App configuration:
      on Settings -> Basics, provide the App Domains, provide the Site URL, on Settings -> Advanced, set the API version to 2.10, add Product: Facebook Login, on Facebook Login -> Settings, set Client OAuth Login: Yes, set Web OAuth Login: Yes, set Enforce HTTPS: Yes, add "http://www.example.com/processwire/page/" to field Valid OAuth Redirect URIs. This module is configurable as follows:
      Templates: posts can take place only for pages with the defined templates. On/Off switch: specify a checkbox field that will not allow the post if checked. Specify a message and/or an image for the post.
      Usage
      edit the desired PW page and save; it will post right after the initial Facebook log in and permission granting. After that, an access token is kept.
       
      Download
      PW module directory: http://modules.processwire.com/modules/auto-fb-post/ Github: https://github.com/kastrind/AutoFbPost   Note: Facebook SDK for PHP is utilized.


    • By thomasaull
      I created a little helper module to trigger a CI pipeline when your website has been changed. It's quite simple and works like this: As soon as you save a page the module sets a Boolean via a pages save after hook. Once a day via LazyCron the module checks if the Boolean is set and sends a POST Request to a configurable Webhook URL.
      Some ideas to extend this:
      make request type configurable (GET, POST) make the module trigger at a specified time (probably only possible with a server cronjob) trigger manually Anything else? If there's interest, I might put in some more functionality. Let me know what you're interested in. Until then, maybe it is useful for a couple of people 🙂
      Github Repo: https://github.com/thomasaull/CiTrigger
    • By Robin S
      I created this module a while ago and never got around to publicising it, but it has been outed in the latest PW Weekly so here goes the support thread...
      Unique Image Variations
      Ensures that all ImageSizer options and focus settings affect image variation filenames.

      Background
      When using methods that produce image variations such as Pageimage::size(), ProcessWire includes some of the ImageSizer settings (height, width, cropping location, etc) in the variation filename. This is useful so that if you change these settings in your size() call a new variation is generated and you see this variation on the front-end.
      However, ProcessWire does not include several of the other ImageSizer settings in the variation filename:
      upscaling cropping, when set to false or a blank string interlace sharpening quality hidpi quality focus (whether any saved focus area for an image should affect cropping) focus data (the top/left/zoom data for the focus area) This means that if you change any of these settings, either in $config->imageSizerOptions or in an $options array passed to a method like size(), and you already have variations at the requested size/crop, then ProcessWire will not create new variations and will continue to serve the old variations. In other words you won't see the effect of your changed ImageSizer options on the front-end until you delete the old variations.
      Features
      The Unique Image Variations module ensures that any changes to ImageSizer options and any changes to the focus area made in Page Edit are reflected in the variation filename, so new variations will always be generated and displayed on the front-end.
      Installation
      Install the Unique Image Variations module.
      In the module config, set the ImageSizer options that you want to include in image variation filenames.
      Warnings
      Installing the module (and keeping one or more of the options selected in the module config) will cause all existing image variations to be regenerated the next time they are requested. If you have an existing website with a large number of images you may not want the performance impact of that. The module is perhaps best suited to new sites where image variations have not yet been generated.
      Similarly, if you change the module config settings on an existing site then all image variations will be regenerated the next time they are requested.
      If you think you might want to change an ImageSizer option in the future (I'm thinking here primarily of options such as interlace that are typically set in $config->imageSizerOptions) and would not want that change to cause existing image variations to be regenerated then best to not include that option in the module config after you first install the module.
       
      https://github.com/Toutouwai/UniqueImageVariations
      https://modules.processwire.com/modules/unique-image-variations/
    • By Sebi
      I've created a small module which lets you define a timestamp after which a page should be accessible. In addition you can define a timestamp when the release should end and the page should not be accessable any more.
      ProcessWire-Module: http://modules.processwire.com/modules/page-access-releasetime/
      Github: https://github.com/Sebiworld/PageAccessReleasetime
      Usage
      PageAccessReleasetime can be installed like every other module in ProcessWire. Check the following guide for detailed information: How-To Install or Uninstall Modules
      After that, you will find checkboxes for activating the releasetime-fields at the settings-tab of each page. You don't need to add the fields to your templates manually.
      Check e.g. the checkbox "Activate Releasetime from?" and fill in a date in the future. The page will not be accessable for your users until the given date is reached.
      If you have $config->pagefileSecure = true, the module will protect files of unreleased pages as well.
      How it works
      This module hooks into Page::viewable to prevent users to access unreleased pages:
      public function hookPageViewable($event) { $page = $event->object; $viewable = $event->return; if($viewable){ // If the page would be viewable, additionally check Releasetime and User-Permission $viewable = $this->canUserSee($page); } $event->return = $viewable; } To prevent access to the files of unreleased pages, we hook into Page::isPublic and ProcessPageView::sendFile.
      public function hookPageIsPublic($e) { $page = $e->object; if($e->return && $this->isReleaseTimeSet($page)) { $e->return = false; } } The site/assets/files/ directory of pages, which isPublic() returns false, will get a '-' as prefix. This indicates ProcessWire (with activated $config->pagefileSecure) to check the file's permissions via PHP before delivering it to the client.
      The check wether a not-public file should be accessable happens in ProcessPageView::sendFile. We throw an 404 Exception if the current user must not see the file.
      public function hookProcessPageViewSendFile($e) { $page = $e->arguments[0]; if(!$this->canUserSee($page)) { throw new Wire404Exception('File not found'); } } Additionally we hook into ProcessPageEdit::buildForm to add the PageAccessReleasetime fields to each page and move them to the settings tab.
      Limitations
      In the current version, releasetime-protected pages will appear in wire('pages')->find() queries. If you want to display a list of pages, where pages could be releasetime-protected, you should double-check with $page->viewable() wether the page can be accessed. $page->viewable() returns false, if the page is not released yet.
      If you have an idea how unreleased pages can be filtered out of ProcessWire selector queries, feel free to write an issue, comment or make a pull request!
×
×
  • Create New...