Jump to content

Plugin LoginRegister use email for login, which username should be generated?


pout
 Share

Recommended Posts

At the moment I extend Ryans Code to make a new email validation, if a user changes his email in his profile. This works for now.
But I ran into following Problem:

If the login with email is allowed, the module generates a username out of the email address:
email = mail@example.com
username = mail-example.com

If the user changes the email address in his profile, I have to change the username accordantly to the email. If the username is not changed, the old email address is blocked for new registration or the user can not change the email address back to his first email.
But if I change the username, this could cause problems with selectors (e.g. pages are named to the usernames).

So I think, it would be the best to generate random usernames, if the login/registration with email is active.
What do you think? Which would be the best way to make random usernames?

 

Link to comment
Share on other sites

Thanks for your input. I like the idea using the id. But I think there are some out there making a user-page with the username right after saving the new user (at least I do) in ready.php like this:

wire()->addHookAfter('Pages::saved', function($event) {
	$page = $event->arguments(0);
	if($page->template == "user"){
          $savedUser = $page;
          $userpage = wire('pages')->find("parent=/parentpage, name=".$savedUser->name); 
          if ( count($userpage) == 0 ) {
              $page = new Page();
              $page->template = 'Lektionen';
              $page->parent = wire('pages')->get('/parentpage');
              $page->title = $savedUser->name;
              $page->save();
          }
	}
});

To get the id of the new user I have to save the new user firts and then retrieve the user-id, set the name and save again. This will cause that the script above will make 2 pages.

But your input is interesting in the way, that I think I would make the username unique this way:

protected function emailToName($email) { 
		$name = $this->wire('pages')->names()->uniquePageName($this->wire('sanitizer')->pageName($email, Sanitizer::translate, $maxLength=100));
		return $name;
	}

So I am sure to have a unique username. I would prefer a ID based name, but I think there is no way to get the new users ID before saving.

The only think I am not happy with then:
If someone populates a userpage made like in the first script to the world and the user changes the email-address, the page will still have his old email-address-alike name in name and title.

Another idea is to make the user names just counted (user-1, user-2, user-3,...), maybe this would best way to make a neutral username. I could add a field to the settings to input to use an own prefix instead of "user-".

//protected function emailToName($email) { 
protected function makeUniqueUserName() { 
		$name = $this->wire('pages')->names()->uniquePageName($this->wire('sanitizer')->pageName('user-'));
		return $name;
	}

 

Link to comment
Share on other sites

protected function makeUniqueUserName() { 
		$name = $this->wire('pages')->names()->uniquePageName($this->wire('sanitizer')->pageName('user'), $this->wire('pages')->get(29));
		if ($name === 'user') $name = "user-1";  // to start with no 1
		return $name;
	}

This worked for the first time, but then the username returned is allways "user-1".
(The sanitizer will get neccessary as soon as ther is a field for own prefix in settings.)

Link to comment
Share on other sites

@pout You can hook to Pages::added. It will fire only once when the user is created. 

wire()->addHookAfter('Pages::added', function($event) {
	$page = $event->arguments(0);
	if($page->template == "user"){
	 $user_id = $event->arguments(0)->id;
	}
});

From this hook, you can create user's page 

wire()->addHookAfter('Pages::saved', function($event) {
	$page = $event->arguments(0);
	if($page->template == "user"){
	 $user_id = $event->arguments(0)->id;
          $savedUser = $page;
          $userpage = wire('pages')->find("parent=/parentpage, name=page-for-user-".$user_id); 

          if ( count($userpage) == 0 ) {
				$parent = wire('pages')->get('/parentpage');
              $page = new Page();
              $page->template = 'Lektionen';
			$page->name = $pages->names()->uniquePageName('page-for-user-' . $user_id, $parent);
              $page->parent = $parent;
              $page->title = $savedUser->name;
              $page->save();
          }
	}
});

 

  • Like 1
Link to comment
Share on other sites

Moderator note: this is not a module support thread, so I'm moving it to the General Support area of the forum instead. Please keep in mind that the Modules/Plugins area is only intended for module-specific support threads. Thanks!

 
  • Like 1
Link to comment
Share on other sites

@cstevensjr I think, as this is related to the Module LoginRegister, it was in the right area. Or should it better move to "Module/Plugin Development"? But any area is fine for me.

@Zeka Yes, thanks, this is good for creating userpages. I will adopt my code using your ideas.

But the main thing for the LoginRegister module is:

.) Should the username change after changing the users email. I think: no. Then I can not make the username depending on his email address.
.) Should the username be created at the first saving/adding of the user. I think: yes. And this makes it, as far as I can see, impossible to use the id in the username. So my conclusion is using a prefix for the user (Standard: user, changeable in settings) and a counting number (with the possibility to overwrite the start number in settings).

What do you (other) think? Maybe some using this module can give some input.

Link to comment
Share on other sites

I did a pull request on github for my changes to LoginRegister. This changes will send a confirmation email if a user changes the email address in its profile. The new address is only set if it gets confirmed. Then also the username will change according his new email address.

For the userpage I used @Zeka's idea in ready.php:

wire()->addHookAfter('Pages::saved', function($event) {
	$page = $event->arguments(0);
	if($page->template == "user"){
          $savedUser = $page;
          $parent = wire('pages')->get('/parentpage');
          $userpage = wire('pages')->find("parent=".$parent.", name=user-".$savedUser->id); 
          if ( count($userpage) == 0 ) {
                $p = new Page();
                $p->template = 'Templatename';
                $p->parent = $parent;
                $p->title = $savedUser->name;
                $p->name = wire('pages')->names()->uniquePageName('user-'.$savedUser->id, $parent);
                $p->save(); 

          } else {
              $up = wire('pages')->get("parent=".$parent.", name=user-".$savedUser->id);
              $up->of(false);
              $up->title = $savedUser->name;
              $up->save(); 
          } 
	}
});

 

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...