Select user role from frontend

Is it possible to let a user sign up for an account from the front end but specify one of two user roles to choose from.

The worst case scenario would be to allow a user to sign up and then to change user roles once logged in?


Well, you can set the user role just like any other user field, either with User::create() or $user->update(). Could you please post the lines of your code that are responsible for user creation? Then we can help you with this directly.

Hi Lukas :slight_smile:

I’m currently using this in my controller -


return function($site, $pages, $page) {

    if(r::is('post') and get('create')) {
        try {

            $data = array(
                'title'         => get('title'),
                'text'          => get('text'),
                'author'        => get('author'),
                'date'          => date('d-m-Y') 

            $curr = page('jobs')->children();
            $jobcount = $curr->count() + 1;
            $newPage = $curr->create(get('title'), 'job', $data);
            $success = true;

        } catch(Exception $e) {

            $error = true;


I haven’t even got to add the user role bit yet, was more of a question for when i get around to it if i’m honest.

So would it be possible to add role into the array and update with the new user role from there?

Currently you are just creating a page, but yes, if you want to create a user, you should be able to set the role just like the other values.

Ahh apologies, I copied the wrong file haha!

I’m actually using this :wink:


return function($site, $pages, $page) {

    if(r::is('post') and get('create-user')) {

        try {

            $user = $site->users()->create(array(
                'username'      => get('username'),
                'firstname'         => get('firstname'),
                'lastname'         => get('lastname'),
                'sex'           => get('sex'),
                'email'         => get('email'),
                'password'      => get('password'),
                'language'      => 'en'
            $login = $user->login(get('password'));

        } catch(Exception $e) {
            $error = true;

    } else {    
        $error = false;

return array('error' => $error, 'success' => $success);

1 Like

Ah, alright. Have you tried just putting a role key in there?

Important: Please make sure to validate the param. If you don’t, users can become admins this way.

Will give it a shot now and let you know, yeah everything will be validated too. I’m just quickly throwing this together now to see if it works as a proof of concept. It’s not going to be used in its current form :slight_smile:

Hey Chris, did you get this to work ? I have to try this :slight_smile:

Hi Thiousi,

Yeah, I got it working fine. It’s a work in progress at the moment. I’ll share the link when its further down the line or drop me a message if you would like to see it in its current form :slight_smile:

1 Like