5

How do you register a user account via REST in Drupal 8?

Here's how far I've made it (not sure if this is the right direction though):

I've enabled the Form mode resource under admin/config/services/rest for POST, cookie authentication, and hal_json and json formats. I then make a call to the site:

POST: http://localhost/d8/entity/entity_form_mode
Content-type: application/hal+json
Accept: application/json

The JSON to POST will start something like this:

{"_links":{}}

It will of course be filled in with various properties and values. What are these properties and values though? I've tried many combinations, and the furthest I've got is the server responding with this:

{"error":"Entity type parameter must be included in context."}
3
  • 1
    I'm not sure why you think that entity form modes have anything to do with registration, they do not. Support this is being worked on in drupal.org/node/2291055
    – Berdir
    Jan 10, 2015 at 19:13
  • What are entity form modes used for? Jan 10, 2015 at 22:23
  • Entity form modes are used for the Manage form display page, for example: admin/structure/types/manage/article/form-display Jan 6, 2016 at 4:06

2 Answers 2

8

This now works in Drupal 8.3-x-dev. This is an example to do it from Angular:

  /*
   * Register new User.
   */
  register: function (email, password) {

    var defer = $q.defer();

    $http({
      method: 'POST',
      url: config.endpoint + '/user/register',
      dataType: 'json',
      data: {
        'name': {
          'value': email,
        },
        'mail': {
          'value': email,
        },
        'pass': {
          'value': password,
        }
      },
    })
      .success(function (data, status, headers, config) {
        defer.resolve(data);
      })
      .error(function (data, status, headers, config) {
        defer.reject(data);
      });

    return defer.promise;
  }

Make sure you enable the resource to enable user registration (I use the REST UI module).

enter image description here

The last step is enabling the permissions on this resource:

enter image description here

Now you are all set!

5
  • It seems like an e-mail address would be required as well... no? Jan 7, 2017 at 16:58
  • Updated my answer! Jan 9, 2017 at 9:48
  • Thanks for the response! Question: If you are testing this from within drupal (as opposed to a headless set up), with a logged in user, this does not seem to work. I think the endpoint '/user/register' does not point to the same place for logged in users. Is there a way around this? I get a 403 error whose body is a Drupal html Access Denied page ("You are not authorized to access this page."). Any ideas? Should I be setting up my own services? Mar 8, 2017 at 16:36
  • Does this automatically log the user in? Or do you run in /user/login?_format=json afterwards? Aug 8, 2018 at 20:39
  • Note that there is currently a nasty bug in Drupal 9 that requires a patch for this to work: drupal.org/project/drupal/issues/3055807 Jun 16, 2022 at 5:10
0

Not yet. =(

There is an open "Major" feature request for this:

It's currently in Drupal 8.2.x-dev, which is scheduled to be released as a beta in Aug 2016, and stable in Oct 2016.

From the issue summary: "A workaround would be to login as an admin and create a user the regular way."

1
  • This is now possible, see my answer to learn how you can do this now. Jan 7, 2017 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.