Removing Username from the Registration Page

Permalink
Hi,

I'm having a bit of trouble with a registration page. I'd like for people to sign up and login with just an email address and password - no usernames (and some custom attributes that I have no problems with). I can configure the login to ask for email and password in the dashboard and this works fine. But when I remove the code on my register page-type for the username field, the form breaks.

Is there a way to remove the username field from the register page?

kevinschueller
View Replies: View Best Answer
okhayat replied on at Permalink Reply
okhayat
invision replied on at Permalink Reply
invision
I know this is an old thread, but I'm trying to do this as well: remove the 'username' field from the registration page.

Semantically, it doesn't make sense to show a username field when you plan to use email for account logins.

The other thread referred to by okhayat doesn't answer the question. Too vague.

Does anyone have a detailed answer or how-to? I'm surprised this isn't a more common question.
jgaris replied on at Permalink Reply
I just had to do this same thing yesterday. It's actually really easy. The code is already there just commented out. Here's what I did...

1. In the dashboard go to Users and Groups --> Login & Registration, then check "Login using email address."

2. Copy /concrete/controllers/register.php to /controllers/register.php

3. Edit the new file. Around line 18, change:
/*if (USER_REGISTRATION_WITH_EMAIL_ADDRESS) {
         $this->set('displayUserName', false);
      } else {
         $this->set('displayUserName', true);
      }*/
      $this->set('displayUserName', true);

to
if (USER_REGISTRATION_WITH_EMAIL_ADDRESS) {
         $this->set('displayUserName', false);
      } else {
         $this->set('displayUserName', true);
      }
      //$this->set('displayUserName', true);

That should do the trick.
invision replied on at Permalink Reply
invision
I just realized I can't mark your answer, since I didn't start this thread. I was close, just forgot to comment out the second part of the code.

Thanks so much! Works like a charm.

Sherm Stevens
http://www.invision-studios.com
jgaris replied on at Permalink Reply
I actually just came across some bugs with this. It wouldn't let more than one person register because it was still checking for a valid username. So my initial idea was to save a timestamp as the username.

Here is the code:

In /controllers/register.php around line 43 change
$username = $_POST['uName'];

to
$username = time();


Although I think I am going to take it a little further and make the username the first part of the email address plus the timestamp.
jgaris replied on at Permalink Reply
I did go ahead and modify it to include the first part of the email address into the username. It was a simple change...

$emailname = strstr($_POST['uEmail'], '@', true);
      $username = $emailname.time();
invision replied on at Permalink Reply
invision
If I un-comment the block of code to hide username and just use email, the form won't submit. I'm trying to track down the source of the problem...

Sherm
jgaris replied on at Permalink Reply
Yes that is the problem I was having too, and why I posted the above 2 fixes for that. Something needs to be saved in the username field, and the above fixes that.
invision replied on at Permalink Reply
invision
Weird. I tried it before, and when I submit it would just return to the register page.

Double-checked the code, everything's kosher...

Just flushed my browser & site's cache and re-started Apache, everything's working as designed now, so ignore my previous post.

Nice work on using the email + timestamp for a unique username, BTW.

Sherm
jordanlev replied on at Permalink Reply
jordanlev
Sherm, I too am surprised this isn't a more common question.

The overall technique here is right on (using a randomly-generated username that is very unlikely to conflict).

But I personally would not use the beginning part of the email address in the username, because that could contain non-alphanumeric characters (for a very thorough discussion about why you shouldn't assume email addresses are formatted a certain way, seehttp://stackoverflow.com/questions/201323... ). I just used the timestamp. If for some crazy reason two people are signing up at the same second, one would get the "username already taken" error, get really confused, and hopefully just try re-submitting (but this is so unlikely to happen on the kinds of sites people build with concrete5).

Furthermore, I would simplify all of this by not even bothering to override any system files. Instead you can just generate the timestamp and put it into a hidden form field, like this:
<input type="hidden" value="<?php echo time(); ?>" name="uName" />


In my case, I was actually creating an ajax registration form, using the jquery.form.js plugin to submit the forms to the registration controller (you can pass in a hidden field with name="format" and value="JSON" to have the reg controller return JSON data about success/failure) -- so I created the timestamp in javascript. But same basic idea.

Anyway, thanks for the pointers about what kind of approach to take -- much appreciated!

Cheers,
Jordan
datagri replied on at Permalink Reply
datagri
I know it's an old topic but, if you find it, you can use the PHP function uniqid() [http://www.php.net/manual/en/function.uniqid.php] to generate an username.
Metaglyphics replied on at Permalink Reply
I'm wrestling with this issue now. I've successfully changed the registration process to not use a user name (members will log in with their email address.)

I've added some custom user attributes called first_name and last_name.

Even though I'd disabled the username option, I needed to add something to that fields dynamically, or else I'd get an error saying that the username needs to be at least 3 characters long.

I tried changing
$username = $_POST['uName'];

to
$username = time();


which works. It adds a unique numerical value to the username field.

But I'd really like the username to be [first_name][last_name] so if someone registers as John Doe, their username becomes JohnDoe.

I think it's something like this:
$userName = $ui->getAttribute('first_name')$ui->getAttribute('last_name');


but that's not quite right. I'm missing a helper or some other lines of code that would make this work. Can anyone help me figure this out?

Thanks
jordanlev replied on at Permalink Reply
jordanlev
The attributes probably aren't available yet, because the account hasn't been officially created yet. Kind of a paradox here... because you can't create the account until you provide a username, but you can't make a username unless you get the first_name and last_name attribute.

You could work around this by pulling the first and last name directly from the $_POST data... but you'll need to do some inspecting of the reg form to see what the actual field names are (C5 uses really weird names for attribute fields, they aren't what you'd expect).

HOWEVER, note that you will run into lots of problems even if you can get the first and last name... you might run into duplicates (if someone else has the same name), or you might run into weird characters (spaces, apostrophes, etc.), or the name might be too long, etc.
What you might want to do is combine the time() or uniqid() with the first+last name, but run the first+last name through some kind of sanitization function (there are a few in the C5 text helper) so it only contains valid characters. Then truncate it at the max # of characters allowed by C5's username field. This way you will always guarantee a unique value (because you're starting each one off with time() or uniqid()), but also can visibly see the first+last name for easier identification when looking at usernames.

Good luck!
Metaglyphics replied on at Permalink Reply
Thanks. What I ended up doing was to essentially factor out the username. Using the TIME field for username, I get a unique value for each user. Then I just use email addresses for the login, so the username never gets used on the front end. And I refer to any user by the First_Name attribute.

That seems to be working so far...
jordanlev replied on at Permalink Reply
jordanlev
Sorry, I misunderstood your situation then. Yes, what you did is absolutely the best option -- that's what I've done several times on sites where they don't want usernames. If you're worried about two people registering at the same time, use the uniqid() function instead of time() (as pointed out by @datagri above).
Metaglyphics replied on at Permalink Reply
Thanks. For this site, the odds of two registrations at the same moment are pretty low. But that's a good idea, just to be safe.
zoinks replied on at Permalink Reply
Could you share the code you used to do this? I have the same problem and no idea what you mean by TIME field or how to go about setting that to the user.
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Override the "register.php" single_page (by copying SITEROOT/concrete/single_pages/register.php to SITEROOT/single_pages/register.php), and find the code that outputs the $uName field:
<?php echo $form->text('uName'); ?>

...and change that to a hidden field that uses the uniqid() function as its value, like so:
<?php echo $form->hidden('uName', uniqid()); ?>


-Jordan
zoinks replied on at Permalink Reply
That sounds like a good solution to generate a simple key. Thank you.

I actually set the username to the first part of the email address (before @) + TIME and then when I display a welcome message, I created a substring of the username by removing the 10 character TIME stamp at the end...

First, I set it up to register by email in Dashboard > System & Settings > Login & Registration > Public Registration > check “Use emails for login”

Then, in register.php, approximately around line 20, I made this change:
if (USER_REGISTRATION_WITH_EMAIL_ADDRESS) {
         $this->set('displayUserName', false);
      } else {
         $this->set('displayUserName', true);
      } 
      /* ^ This is the alternative with usernames instead of email registration:
      $this->set('displayUserName', true);  */


And this change approximately around line 46:
/* REMOVE USERNAME:
         $username = $_POST['uName']; */
      /* ADD timestamp automated username instead */
      $emailname = strstr($_POST['uEmail'], '@', true);
            $username = $emailname.time();
      $password = $_POST['uPassword'];
      $passwordConfirm = $_POST['uPasswordConfirm'];
      // clean the username
      $username = trim($username);
      $username = preg_replace("/ +/", " ", $username);


And then on the Login Destination page, I used this code to display a welcome message using the auto-generated username from the email + TIME function. (Here is where I removed the 10 TIME stamp characters at the end of the username):

<?php
   $u = new User();
   if ($u->isRegistered()) { 
      $zoinksName = $u->getUserName();
      $zoinksName = substr($zoinksName,0,-10);
      if (strlen($zoinksName) < 1) {
         $zoinksName = "admin";
         echo "<h3>You are logged in as user: <span style='color:black;'>" . $zoinksName;
         echo "</span></h3>";
         echo "<p><b>You may now take the test.</b></p><hr><br/><br/>";
      } else {
         echo "<h3>You are logged in as user: <span style='color:black;'>" . $zoinksName;
         echo "</span></h3>";
         echo "<p><b>You may now take the test.</b></p><hr><br/><br/>";
      }
Ta2Ta2 replied on at Permalink Reply
This worked for my 5.7.4! thanks alot...

any idea how to re-arrange the fields order? specially I added custom Attributes such as full name, company name...etc.

Thanks in advance,
Ahmed
Janks replied on at Permalink Reply
Janks
What is the reason why this is not a standard built-in mechanism?