4

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."}
  • 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 '15 at 19:13
  • What are entity form modes used for? – tyler.frankenstein Jan 10 '15 at 22:23
  • Entity form modes are used for the Manage form display page, for example: admin/structure/types/manage/article/form-display – tyler.frankenstein Jan 6 '16 at 4:06
6

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!

  • It seems like an e-mail address would be required as well... no? – tyler.frankenstein Jan 7 '17 at 16:58
  • Updated my answer! – Fons Vandamme Jan 9 '17 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? – Christopher Borchert Mar 8 '17 at 16:36
  • Does this automatically log the user in? Or do you run in /user/login?_format=json afterwards? – Shawn Matthews Aug 8 '18 at 20:39
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."

  • This is now possible, see my answer to learn how you can do this now. – Fons Vandamme Jan 7 '17 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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