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

Hi,

need some help. How can i theme the login button through css? How can i give the input field another class?

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

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Macrura
      PrevNextTabs Module
      Github: https://github.com/outflux3/PrevNextTabs
      Processwire helper modules for adding page navigation within the editor.
      Overview
      This is a very simple module that adds Previous and Next links inline with the tabs on the page editor. Hovering over the tab shows the title of the previous or next page (using the admin's built in jqueryUI tooltips.)
      Usage
      This module is typically used during development where you or your editors need to traverse through pages for the purpose of proofing, flagging and/or commenting. Rather than returning to the page tree or lister, they can navigate with these links.
      Warnings
      If you are using PW version 2.6.1 or later, the system will prevent you from leaving the page if you have unsaved edits.
      For earlier versions, to avoid accidentally losing changes made to a page that might occur if a user accidentally clicks on one of these, make sure to have the Form Save Reminder module installed.
      http://modules.processwire.com/modules/prev-next-tabs/
    • By Gadgetto
      SnipWire - Snipcart integration for ProcessWire
      Snipcart is a powerful 3rd party, developer-first HTML/JavaScript shopping cart platform. SnipWire is the missing link between Snipcart and the content management framework ProcessWire.
      With SnipWire, you can quickly turn any ProcessWire site into a Snipcart online shop. The SnipWire plugin helps you to get your store up and running in no time. Detailed knowledge of the Snipcart system is not required.
      SnipWire is free and open source licensed under Mozilla Public License 2.0! A lot of work and effort has gone into development. It would be nice if you could donate an amount to support further development:

      Status update links (inside this thread) for SnipWire development
      2020-03-21 -- SnipWire 0.8.5 (beta) released! Improves SnipWires webhooks interface and provides some other fixes and additions 2020-03-03 -- SnipWire 0.8.4 (beta) released! Improves compatibility for Windows based Systems. 2020-03-01 -- SnipWire 0.8.3 (beta) released! The installation and uninstallation process has been heavily revised. 2020-02-08 -- SnipWire 0.8.2 (beta) released! Added a feature to change the cart and catalogue currency by GET, POST or SESSION param 2020-02-03 -- SnipWire 0.8.1 (beta) released! All custom classes moved into their own namespaces. 2020-02-01 -- SnipWire is now available via ProcessWire's module directory! 2020-01-30 -- SnipWire 0.8.0 (beta) first public release! (module just submitted to the PW modules directory) 2020-01-28 -- added Custom Order Fields feature (first SnipWire release version is near!) 2020-01-21 -- Snipcart v3 - when will the new cart system be implemented? 2020-01-19 -- integrated taxes provider finished (+ very flexible shipping taxes handling) 2020-01-14 -- new date range picker, discount editor, order notifiactions, order statuses, and more ... 2019-11-15 -- orders filter, order details, download + resend invoices, refunds 2019-10-18 -- list filters, REST API improvements, new docs platform, and more ... 2019-08-08 -- dashboard interface, currency selector, managing Orders, Customers and Products, Added a WireTabs, refinded caching behavior 2019-06-15 -- taxes provider, shop templates update, multiCURL implementation, and more ... 2019-06-02 -- FieldtypeSnipWireTaxSelector 2019-05-25 -- SnipWire will be free and open source Plugin Key Features
      Fast and simple store setup Full integration of the Snipcart dashboard into the ProcessWire backend (no need to leave the ProcessWire admin area) Browse and manage orders, customers, discounts, abandoned carts, and more Multi currency support Custom order and cart fields Process refunds and send customer notifications from within the ProcessWire backend Process Abandoned Carts + sending messages to customers from within the ProcessWire backend Complete Snipcart webhooks integration (all events are hookable via ProcessWire hooks) Integrated taxes provider (which is more flexible then Snipcart own provider) Useful Links
      SnipWire in PW modules directory SnipWire Docs (please note that the documentation is a work in progress) SnipWire @GitHub (feature requests and suggestions for improvement are welcome - I also accept pull requests) Snipcart Website  
      ---- INITIAL POST FROM 2019-05-25 ----
       
    • By horst
      Croppable Image 3
      for PW 3.0.20+
      Module Version 1.2.0
      Sponsored by http://dreikon.de/, many thanks Timo & Niko!
      You can get it in the modules directory!
      Please refer to the readme on github for instructions.
       
      - + - + - + - + - + - + - + - + - + - NEWS - 2020/03/19 - + - + - + - + - + - + - + - + - + - 
      There is a new Version in the pipe, that supports WebP too: 
       
      - + - + - + - + - + - + - + - + - + - NEWS - 2020/03/19 - + - + - + - + - + - + - + - + - + - 
       
       
      -------------------------------------------------------------------------
       
      Updating from prior versions:
       
      Updating from Croppable Image 3 with versions prior to 1.1.7, please do this as a one time step:
      In the PW Admin, go to side -> modules -> new, use "install via ClassName" and use CroppableImage3 for the Module Class Name. This will update your existing CroppableImage3 module sub directory, even if it is called a new install. After that, the module will be recogniced by the PW updater module, what makes it a lot easier on further updates.
      -------------------------------------------------------------------------
       
      For updating from the legacy Thumbnail / CropImage to CroppableImage3 read on here.
       
      -------------------------------------------------------------------------
       
    • By Robin S
      Inspired by a recent question.
      Image Crop Ratios
      Allows preset aspect ratios to be defined per image field for the ProcessWire image crop tool.
      The module adds a select dropdown to the crop tool. Choose an aspect ratio and the crop area will be fixed to that ratio.
      Screencast

      Installation
      Install the Image Crop Ratios module.
      Configuration
      Default aspect ratios for all image fields can be defined in the module config. Aspect ratios for specific image fields can be defined on the Input tab of the field settings. You can override the ratio settings in template context if needed. Insert a hyphen as the first item in the ratio settings unless you want to force a ratio to be applied to the crop tool. The hyphen represents a blank option that allows a free crop area to be drawn. Usage
      Click the "Crop" link on the details view of an image thumbnail. Click the "Crop" icon at the top of the editor window. Choose an option from the "Ratio" select dropdown.  
      https://github.com/Toutouwai/ImageCropRatios
      https://modules.processwire.com/modules/image-crop-ratios/
×
×
  • Create New...