ember-cli-gatekeeper
ember-cli addon for @onehiltech/blueprint-gatekeeper.
Last updated 2 months ago by onehilltech .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ember-cli-gatekeeper 
SYNC missed versions from official npm registry.

ember-cli-gatekeeper

EmberJS add-on for blueprint-gatekeeper

npm version Dependencies

Compatibility

  • Ember.js v3.4 or above
  • Ember CLI v2.13 or above
  • Node.js v8 or above

Installation

npm install ember-cli-blueprint-helpers --save-dev      # temp workaround   
ember install ember-cli-gatekeeper

Getting Started

Defining the configuration

Update the ENV variable in config/environment.js with the required configuration values:

Name Description Required Default Value
gatekeeper.baseUrl Location of blueprint-gatekeeper Yes
gatekeeper.startRoute Default route name, or url, to transition to after login index
gatekeeper.signInRoute Name of the sign in route sign-in
gatekeeper.tokenOptions.client_id Client id Yes
gatekeeper.tokenOptions.client_secret Client secret

The client secret should only be used if the web application is installed in a trusted environment, such as a mobile application via ember-cordova.

Here is an example config/environment.js with the Gatekeeper configuration:

let ENV = {
  // ...
  
  EmberENV: {
    FEATURES: {
    // This must be enabled for account adapter to work.
    'ds-improved-ajax': true
    }
    
    // ...
  },
  
  gatekeeper: {
    baseUrl: 'https://api.onehilltech.com/gatekeeper',

    tokenOptions: {      
      client_id: '59ee923e1fd71c2ae68ade62',
      client_secret: '1234567890'
    }
  }
}

Protecting application routes

Protected application routes are routes that require the user to be signed in to access. Creating protected application route is very simple.

First, create the route using ember-cli.

ember g route [name]

Then, import the Authenticated mixin from ember-cli-gatekeeper and apply it to the route.

// app/routes/comments.js

import Route from '@ember/routing/route';
import Authenticated from 'ember-cli-gatekeeper/mixins/authenticated';

export default Route.extend (Authenticated, {
  model () {
    // Get the user for the current session.
    let currentUser = this.get ('currentUser');
    return this.get ('store').query ('comments', {user: user.id});
  }
});

The gatekeeper service is injected into all routes. The Authenticated class provides the currentUser property, which gives you access to the account model (less the password) for the signed in user.

When this route is accessed and the user is not signed in, the user will be transitioned to the sign-in route (see Configuration). After the user signs in, the user will be transitioned back to the original route or the startRoute defined in the configuration.

Accessing protected data

ember-data uses data models to access resources on a remote server. When using Gatekeeper, the routes for accessing these resources are protected via an authorization token. To get this authorization token into each ember-data request, the adapters in your application (either the application or model-specific adapters) must extend the RESTAdapter in Gatekeeper.

// app/adapters/application.js

import RESTAdapter from 'ember-cli-gatekeeper/-lib/user/adapters/rest';

export default RESTAdapter.extend({
  
});

You can then continue configuring the adapter as normal.

Signing in a user

To sign in a user, you need a route with a form that collects the user's username and password. The Gatekeeper add-on provides a form that can be used to sign-in a user.

{{gatekeeper-sign-in complete=(action "complete")}}

This form needs to be added to your sign-in route. When the user has signed in successfully, the complete action is invoked. At this point, you are free to transition to any route in the application.

A standard practice is to route to either the start route, or to the previous route the user tried to access when they were not signed in. If you want this behavior, then apply the Completed mixin to the controller for the sign in route.

import Controller from '@ember/controller';
import Completed  from 'ember-cli-gatekeeper/mixins/completed';

export default Controller.extend (Completed, {
  
});

Now, the user will either be routed to the start route, or the previously accessed route before being routed to the sign in route, when they successfully sign in.

Using reCAPTCHA

Gatekeeper uses different public/private key verification schemes to ensure that robots are not accessing the system. When developing a web application, it is not safe to place a secret in an EmberJS application because it will be accessible to site visitors. We therefore recommend you use a reCAPTCHA service, such as Google reCAPTCHA, to verify users are not robots.

Gatekeeper provides out-of-the-box support for Google reCAPTCHA via the ember-cli-google-recaptcha add-on. First, you have to do is add your siteKey to config/environment.js:

let ENV = {
  // ...
  
  'ember-cli-google': {
    recaptcha: {
      siteKey: 'This is where my siteKey goes'
    }
  }
};

Next, you replace the standard sign in component with the reCAPTCHA sign in component.

{{gatekeeper-sign-in-with-recaptcha recaptcha=v2 complete=(action "complete")}}

Set recaptcha="invisible" to use invisible reCAPTCHA.

Signing out a user

A signed in user can be signed out from any where in the application as long as you have access to the session service.

The session service is injected into all routes and controllers.

// app/controllers/index.js

import Controller from '@ember/controller';

export default Controller.extend({
  actions: {
    signOut () {
      this.get ('session').signOut ().then (() => {
        this.replaceRoute ('sign-in');
      });
    }
  }
});

Allowing users to create accounts

The Gatekeeper add-on also provides a default form for creating an new account. You use it in a similar manner as signing in a user. First, add the sign up form to the route for signing up a user, and configure the form to your needs.

{{gatekeeper-sign-up complete=(action "complete")}}

The Gatekeeper add-on also has sign up components that supports reCAPTCHA.

Then, apply the Completed mixin to the controller for the sign up route.

import Controller from '@ember/controller';
import Completed  from 'ember-cli-gatekeeper/mixins/completed';

export default Controller.extend (Completed, {

});

The client registered with the server must have the gatekeeper.account.create scope. Otherwise, the client will not be authorized to create the account.

Manually creating an account

We use the account model to create user accounts. We assume that you have created a template to gather the username, password, and email from the user and have a controller action to that creates the account:

import Controller from '@ember/controller';

export default Controller.extend({
  actions: {
    createAccount () {
      let {email, username, password} = this.getProperties (['email', 'username', 'password']);
      let account = this.get ('store').createRecord ('account', {username, password, email});
      let adapterOptions = {signIn: true};
      
      account.save ({adapterOptions}).then (account => {
        // You can transition to a protected application route
      }).catch (reason => {
        // Display error message to user
      });
    }
  }
});

The save() method takes an optional adapterOptions property that allows you to sign in the user when the account is created. The advantage of doing this it that it allows you to transition to a protected application route after account creation, or access protected data as part of the creation process. Otherwise, the user will have to sign in after creating the account to access a protected application route.

Happy Coding!

Current Tags

  • 0.60.4                                ...           latest (2 months ago)

311 Versions

  • 0.60.4                                ...           2 months ago
  • 0.60.3                                ...           2 months ago
  • 0.60.2                                ...           2 months ago
  • 0.60.1                                ...           2 months ago
  • 0.60.0                                ...           2 months ago
  • 0.59.0                                ...           2 months ago
  • 0.58.1                                ...           3 months ago
  • 0.58.0                                ...           3 months ago
  • 0.57.4                                ...           3 months ago
  • 0.57.3                                ...           3 months ago
  • 0.57.2                                ...           5 months ago
  • 0.57.1                                ...           5 months ago
  • 0.57.0                                ...           5 months ago
  • 0.56.3                                ...           5 months ago
  • 0.56.2                                ...           5 months ago
  • 0.56.1                                ...           5 months ago
  • 0.56.0                                ...           6 months ago
  • 0.55.10                                ...           7 months ago
  • 0.55.9                                ...           7 months ago
  • 0.55.8                                ...           7 months ago
  • 0.55.7                                ...           7 months ago
  • 0.55.6                                ...           7 months ago
  • 0.55.5                                ...           7 months ago
  • 0.55.4                                ...           7 months ago
  • 0.55.3                                ...           7 months ago
  • 0.55.2                                ...           8 months ago
  • 0.55.1                                ...           8 months ago
  • 0.55.0                                ...           8 months ago
  • 0.54.7                                ...           8 months ago
  • 0.54.6                                ...           8 months ago
  • 0.54.5                                ...           8 months ago
  • 0.54.4                                ...           8 months ago
  • 0.51.1                                ...           8 months ago
  • 0.54.3                                ...           8 months ago
  • 0.54.2                                ...           8 months ago
  • 0.54.1                                ...           8 months ago
  • 0.54.0                                ...           8 months ago
  • 0.53.1                                ...           8 months ago
  • 0.53.0                                ...           8 months ago
  • 0.52.0                                ...           8 months ago
  • 0.51.0                                ...           8 months ago
  • 0.50.4                                ...           8 months ago
  • 0.50.3                                ...           a year ago
  • 0.50.1                                ...           a year ago
  • 0.50.0                                ...           a year ago
  • 0.49.0                                ...           a year ago
  • 0.48.0                                ...           a year ago
  • 0.47.2                                ...           a year ago
  • 0.47.1                                ...           a year ago
  • 0.47.0                                ...           2 years ago
  • 0.46.1                                ...           2 years ago
  • 0.46.0                                ...           2 years ago
  • 0.45.0                                ...           2 years ago
  • 0.44.2                                ...           2 years ago
  • 0.44.1                                ...           2 years ago
  • 0.44.0                                ...           2 years ago
  • 0.43.7                                ...           2 years ago
  • 0.43.6                                ...           2 years ago
  • 0.43.5                                ...           2 years ago
  • 0.43.4                                ...           2 years ago
  • 0.43.3                                ...           2 years ago
  • 0.43.1                                ...           2 years ago
  • 0.43.0                                ...           2 years ago
  • 0.42.4                                ...           2 years ago
  • 0.42.3                                ...           2 years ago
  • 0.42.2                                ...           2 years ago
  • 0.42.1                                ...           2 years ago
  • 0.42.0-0                                ...           2 years ago
  • 0.41.1                                ...           2 years ago
  • 0.41.0                                ...           2 years ago
  • 0.40.0                                ...           2 years ago
  • 0.39.2                                ...           2 years ago
  • 0.39.1                                ...           2 years ago
  • 0.39.0                                ...           2 years ago
  • 0.38.1                                ...           2 years ago
  • 0.38.0                                ...           2 years ago
  • 0.36.5                                ...           2 years ago
  • 0.36.4                                ...           2 years ago
  • 0.36.3                                ...           2 years ago
  • 0.36.2                                ...           2 years ago
  • 0.36.1                                ...           2 years ago
  • 0.36.0                                ...           2 years ago
  • 0.35.1                                ...           2 years ago
  • 0.35.0                                ...           2 years ago
  • 0.34.4                                ...           2 years ago
  • 0.34.3                                ...           2 years ago
  • 0.34.2                                ...           2 years ago
  • 0.34.1                                ...           2 years ago
  • 0.34.0                                ...           2 years ago
  • 0.33.5                                ...           2 years ago
  • 0.33.4                                ...           2 years ago
  • 0.33.3                                ...           2 years ago
  • 0.33.2                                ...           2 years ago
  • 0.33.1                                ...           2 years ago
  • 0.33.0                                ...           2 years ago
  • 0.32.1                                ...           2 years ago
  • 0.32.0                                ...           2 years ago
  • 0.31.3                                ...           2 years ago
  • 0.31.2                                ...           2 years ago
  • 0.31.1                                ...           2 years ago
  • 0.31.0                                ...           2 years ago
  • 0.30.2                                ...           2 years ago
  • 0.30.1                                ...           2 years ago
  • 0.30.0                                ...           2 years ago
  • 0.29.2                                ...           2 years ago
  • 0.29.1                                ...           2 years ago
  • 0.29.0                                ...           2 years ago
  • 0.28.9                                ...           2 years ago
  • 0.28.8                                ...           2 years ago
  • 0.28.7                                ...           2 years ago
  • 0.28.6                                ...           2 years ago
  • 0.28.5                                ...           2 years ago
  • 0.28.4                                ...           2 years ago
  • 0.28.3                                ...           2 years ago
  • 0.28.2                                ...           2 years ago
  • 0.28.1                                ...           2 years ago
  • 0.28.0                                ...           2 years ago
  • 0.27.13                                ...           2 years ago
  • 0.27.12                                ...           2 years ago
  • 0.27.11                                ...           2 years ago
  • 0.27.10                                ...           2 years ago
  • 0.27.9                                ...           2 years ago
  • 0.27.8                                ...           2 years ago
  • 0.27.7                                ...           2 years ago
  • 0.27.5                                ...           2 years ago
  • 0.27.4                                ...           2 years ago
  • 0.27.3                                ...           2 years ago
  • 0.27.2                                ...           2 years ago
  • 0.27.1                                ...           2 years ago
  • 0.27.0                                ...           2 years ago
  • 0.26.25                                ...           2 years ago
  • 0.26.24                                ...           2 years ago
  • 0.26.23                                ...           3 years ago
  • 0.26.22                                ...           3 years ago
  • 0.26.21                                ...           3 years ago
  • 0.26.20                                ...           3 years ago
  • 0.26.19                                ...           3 years ago
  • 0.26.18                                ...           3 years ago
  • 0.26.17                                ...           3 years ago
  • 0.26.16                                ...           3 years ago
  • 0.26.15                                ...           3 years ago
  • 0.26.14                                ...           3 years ago
  • 0.26.13                                ...           3 years ago
  • 0.26.12                                ...           3 years ago
  • 0.26.11                                ...           3 years ago
  • 0.26.10                                ...           3 years ago
  • 0.26.9                                ...           3 years ago
  • 0.26.8                                ...           3 years ago
  • 0.26.7                                ...           3 years ago
  • 0.26.6                                ...           3 years ago
  • 0.26.5                                ...           3 years ago
  • 0.26.4                                ...           3 years ago
  • 0.26.3                                ...           3 years ago
  • 0.26.2                                ...           3 years ago
  • 0.26.1                                ...           3 years ago
  • 0.26.0                                ...           3 years ago
  • 0.25.0                                ...           3 years ago
  • 0.24.2                                ...           3 years ago
  • 0.24.1                                ...           3 years ago
  • 0.24.0                                ...           3 years ago
  • 0.23.12                                ...           3 years ago
  • 0.23.11                                ...           3 years ago
  • 0.23.10                                ...           3 years ago
  • 0.23.9                                ...           3 years ago
  • 0.23.8                                ...           3 years ago
  • 0.23.7                                ...           3 years ago
  • 0.23.6                                ...           3 years ago
  • 0.23.5                                ...           3 years ago
  • 0.23.4                                ...           3 years ago
  • 0.23.3                                ...           3 years ago
  • 0.23.2                                ...           3 years ago
  • 0.23.1                                ...           3 years ago
  • 0.23.0                                ...           3 years ago
  • 0.22.0                                ...           3 years ago
  • 0.21.0                                ...           3 years ago
  • 0.20.14                                ...           3 years ago
  • 0.20.13                                ...           3 years ago
  • 0.20.12                                ...           3 years ago
  • 0.20.11                                ...           3 years ago
  • 0.20.10                                ...           3 years ago
  • 0.20.9                                ...           3 years ago
  • 0.20.8                                ...           3 years ago
  • 0.20.7                                ...           3 years ago
  • 0.20.6                                ...           3 years ago
  • 0.20.5                                ...           3 years ago
  • 0.20.4                                ...           3 years ago
  • 0.20.3                                ...           3 years ago
  • 0.20.2                                ...           3 years ago
  • 0.20.1                                ...           3 years ago
  • 0.20.0                                ...           3 years ago
  • 0.19.3                                ...           3 years ago
  • 0.19.2                                ...           3 years ago
  • 0.19.1                                ...           3 years ago
  • 0.19.0                                ...           3 years ago
  • 0.18.12                                ...           3 years ago
  • 0.18.11                                ...           3 years ago
  • 0.18.10                                ...           3 years ago
  • 0.18.9                                ...           3 years ago
  • 0.18.8                                ...           3 years ago
  • 0.18.7                                ...           3 years ago
  • 0.18.6                                ...           3 years ago
  • 0.18.5                                ...           3 years ago
  • 0.18.4                                ...           3 years ago
  • 0.18.3                                ...           3 years ago
  • 0.18.2                                ...           3 years ago
  • 0.18.1                                ...           3 years ago
  • 0.18.0                                ...           3 years ago
  • 0.17.0                                ...           3 years ago
  • 0.16.3                                ...           3 years ago
  • 0.16.2                                ...           3 years ago
  • 0.16.1                                ...           3 years ago
  • 0.16.0                                ...           3 years ago
  • 0.15.2                                ...           3 years ago
  • 0.15.1                                ...           3 years ago
  • 0.15.0                                ...           3 years ago
  • 0.14.0                                ...           3 years ago
  • 0.13.10                                ...           3 years ago
  • 0.13.9                                ...           3 years ago
  • 0.13.8                                ...           3 years ago
  • 0.13.7                                ...           3 years ago
  • 0.13.6                                ...           3 years ago
  • 0.13.5                                ...           3 years ago
  • 0.13.4                                ...           3 years ago
  • 0.13.3                                ...           3 years ago
  • 0.13.2                                ...           3 years ago
  • 0.13.1                                ...           3 years ago
  • 0.13.0                                ...           3 years ago
  • 0.12.0                                ...           3 years ago
  • 0.11.1                                ...           3 years ago
  • 0.11.0                                ...           3 years ago
  • 0.10.6                                ...           3 years ago
  • 0.10.5                                ...           3 years ago
  • 0.10.4                                ...           3 years ago
  • 0.10.3                                ...           3 years ago
  • 0.10.2                                ...           3 years ago
  • 0.10.1                                ...           3 years ago
  • 0.10.0                                ...           3 years ago
  • 0.9.12                                ...           3 years ago
  • 0.9.11                                ...           3 years ago
  • 0.9.10                                ...           3 years ago
  • 0.9.9                                ...           3 years ago
  • 0.9.8                                ...           3 years ago
  • 0.9.7                                ...           3 years ago
  • 0.9.6                                ...           3 years ago
  • 0.9.5                                ...           3 years ago
  • 0.9.4                                ...           3 years ago
  • 0.9.3                                ...           3 years ago
  • 0.9.2                                ...           3 years ago
  • 0.9.0                                ...           3 years ago
  • 0.8.4                                ...           3 years ago
  • 0.8.3                                ...           3 years ago
  • 0.8.2                                ...           3 years ago
  • 0.8.1                                ...           3 years ago
  • 0.8.0                                ...           3 years ago
  • 0.7.8                                ...           3 years ago
  • 0.7.7                                ...           3 years ago
  • 0.7.6                                ...           3 years ago
  • 0.7.5                                ...           3 years ago
  • 0.7.4                                ...           3 years ago
  • 0.7.3                                ...           3 years ago
  • 0.7.2                                ...           3 years ago
  • 0.7.1                                ...           3 years ago
  • 0.7.0                                ...           3 years ago
  • 0.6.11                                ...           3 years ago
  • 0.6.10                                ...           3 years ago
  • 0.6.9                                ...           3 years ago
  • 0.6.8                                ...           3 years ago
  • 0.6.7                                ...           3 years ago
  • 0.6.6                                ...           3 years ago
  • 0.6.5                                ...           3 years ago
  • 0.6.4                                ...           3 years ago
  • 0.6.3                                ...           3 years ago
  • 0.6.2                                ...           3 years ago
  • 0.6.1                                ...           3 years ago
  • 0.6.0                                ...           3 years ago
  • 0.5.2                                ...           3 years ago
  • 0.5.1                                ...           3 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.9                                ...           3 years ago
  • 0.4.8                                ...           4 years ago
  • 0.4.7                                ...           4 years ago
  • 0.4.6                                ...           4 years ago
  • 0.4.5                                ...           4 years ago
  • 0.4.4                                ...           4 years ago
  • 0.4.2                                ...           4 years ago
  • 0.4.1                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.3.23                                ...           4 years ago
  • 0.3.22                                ...           4 years ago
  • 0.3.21                                ...           4 years ago
  • 0.3.20                                ...           4 years ago
  • 0.3.19                                ...           4 years ago
  • 0.3.18                                ...           4 years ago
  • 0.3.17                                ...           4 years ago
  • 0.3.16                                ...           4 years ago
  • 0.3.15                                ...           4 years ago
  • 0.3.14                                ...           4 years ago
  • 0.3.13                                ...           4 years ago
  • 0.3.12                                ...           4 years ago
  • 0.3.11                                ...           4 years ago
  • 0.3.10                                ...           4 years ago
  • 0.3.9                                ...           4 years ago
  • 0.3.8                                ...           4 years ago
  • 0.3.7                                ...           4 years ago
  • 0.3.6                                ...           4 years ago
  • 0.3.5                                ...           4 years ago
  • 0.3.4                                ...           4 years ago
  • 0.3.3                                ...           4 years ago
  • 0.3.1                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dev Dependencies (62)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |