How to login and logout with REST in Drupal 8

First you will have to setup your REST resources. Make sure you enabled all modules:

Enable REST modules
Enable REST modules

I use the REST UI module and I recommend using it yourself because it's very handy. You don't have to edit the .yml files to enable your resources, instead you can do it with a UI.

I made an ionic app with a login screen and a logout button. I use Cookie Authentication.

This is my login function (Angular):

      /*
       * Login User with username and password.
       */
      login: function (email, password) {

        $ionicLoading.show();

        var defer = $q.defer();

        $http({
          method: 'POST',
          url: config.endpoint + '/user/login?_format=json',
          data: {
            name: email,
            pass: password
          },
        })
          .success(function (data, status, headers, config) {

            // Store the CSRF token.
            localStorage.set('csrf_token', data.csrf_token);

            // Store the Logout token
            localStorage.set('logout_token', data.logout_token);

            // Set the user object.
            localStorage.setObject('user', data.current_user);

            // Set a logged in flag.
            localStorage.set('logged_in', true);

            // Redirect to the overview.
            $state.go("overview");

            $ionicLoading.hide();

            defer.resolve(data);
          })
          .error(function (data, status, headers, config) {

            $ionicLoading.hide();

            defer.reject(data);
          });

        return defer.promise;
      },

This call returns the current_user, a csrf_token, the logout_token.

And this is my logout function

      /*
       * Logout User.
       */
      logout: function () {

        $ionicLoading.show();

        var defer = $q.defer();

        $http({
          method: 'GET',
          url: config.endpoint + '/user/logout?_format=json',
        })
          .success(function (data, status, headers, config) {
            localStorage.clearAll();
            defer.resolve(data);

            // Go to the login page.
            $state.go("login");
            $ionicLoading.hide();
          })
          .error(function (data, status, headers, config) {

            defer.reject(data);
            $ionicLoading.hide();
          });

        return defer.promise;
      },

You will notice I read to, write from and clear my LocalStorage. I created a factory to do this:

.factory('localStorage', ['$window', function ($window) {
    return {
      set: function (key, value) {
        $window.localStorage[key] = value;
      },
      get: function (key, defaultValue) {
        return $window.localStorage[key] || defaultValue;
      },
      setObject: function (key, value) {
        $window.localStorage[key] = JSON.stringify(value);
      },
      getObject: function (key) {
        return JSON.parse($window.localStorage[key] || '[]');
      },
      removeItem: function (key) {
        window.localStorage.removeItem(key);
      },
      clearAll: function () {
        // We clear all the things.
        $window.localStorage.clear();
      }
    }
  }])
Drupal

Meer leesvoer

I have been working on a Drupal 8 multisite and at some point I had to set up a new site in the structure. The...
Drupal
In this blog post you will learn how to add a theme field preprocess function in your theme.
Theming
It's quite easy to create a node with REST in Drupal 8, in this blog post I'll show you how to do it.
Drupal
Drupal 8.3-x-dev is out and it comes packed with improvements to the REST module. It's now possible to create new users using REST.
Drupal