Skip to content

Commit

Permalink
update guides
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoow committed May 9, 2020
1 parent 5876d30 commit b6704d8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 34 deletions.
54 changes: 34 additions & 20 deletions guides/auth-torii-with-github.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,41 +250,54 @@ When you spin up the server again with `ember serve`, you should see, at

### Adding Authentication

First, we'll add the [ApplicationRouteMixin](http://ember-simple-auth.com/api/classes/ApplicationRouteMixin.html) to
our application route. This is optional, but adds methods supporting the authentication lifecycle that we would
otherwise have to implement explicitly.
First, we'll call the session service's
[setup](http://ember-simple-auth.com/api/classes/SessionService.html#method_setup)
method in our application route's constructor. This ensures the session is
fully initialized when the application boots and installs default handlers for
the session events.

```js
// app/routes/application.js

import Route from '@ember/routing';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
import { inject as service } from '@ember/service';

export default class ApplicationRoute extends Route {
@service session;

export default class ApplicationRoute extends Route.extend(ApplicationRouteMixin) {
constructor() {
super(...arguments);

this.session.setup();
}
}
```

Next, we'll designate the `index` route as an authenticated route using the
[AuthenticatedRouteMixin](http://ember-simple-auth.com/api/classes/AuthenticatedRouteMixin.html). This will make the
route inaccessible until we finish the authentication. It will automatically redirect you to the specified login route,
by default `login`, if you are not authenticated.
session service's
[requireAuthentication](http://ember-simple-auth.com/api/classes/SessionService.html#method_requireAuthentication)
method. This will make the route inaccessible until we finish the
authentication. It will automatically redirect you to the specified login
route, if you are not authenticated.

```js
// app/routes/index.js

import Route from '@ember/routing';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
import { inject as service } from '@ember/service';

export default IndexRoute extends Route {
@service session;

export default IndexRoute extends Route.extend(AuthenticatedRouteMixin) {
beforeModel(transition) {
this.get('session').requireAuthentication(transition, 'login');
},
}
```

If you're still running the app when you save this, you will see it redirect to the `login` route.

We'll also designate the `login` route as available for unauthenticated access only by applying the
[UnauthenticatedRouteMixin](http://ember-simple-auth.com/api/classes/UnauthenticatedRouteMixin.html) to it. This will
redirect you to the `routeIfAlreadyAuthenticated` which defaults to `index`. As you can see, `ember-simple-auth` has
sensible and convenient defaults.
We'll also designate the `login` route as available for unauthenticated access only by calling the session service's
[prohibitAuthentication](http://ember-simple-auth.com/api/classes/SessionService.html#method_prohibitAuthentication).
This will redirect you to the `index` route.

Next, we'll create and set up our torii authenticator to start.

Expand Down Expand Up @@ -680,9 +693,11 @@ ember g adapter github-user

import { computed } from '@ember/object';
import GitHubUserAdapter from 'ember-data-github/adapters/github-user';
import DataAdapterMixin from "ember-simple-auth/mixins/data-adapter-mixin";
import { inject as service } from '@ember/service';

export default class GithubUserAdapter extends GitHubUserAdapter {
@service session;

export default class GithubUserAdapter extends GitHubUserAdapter.extend(DataAdapterMixin) {
@computed("session.data.authenticated.access_token")
get headers() {
const headers = {};
Expand All @@ -697,8 +712,7 @@ export default class GithubUserAdapter extends GitHubUserAdapter.extend(DataAdap
}
```
This adapter injects an authorization header into the GitHub request now. The `DataAdapterMixin` is a mixin provided by
`ember-simple-auth` that injects the `session` service we use to generate the authentication header.
This adapter injects an authorization header into the GitHub request now.
### Wrapping Up
Expand Down
42 changes: 28 additions & 14 deletions guides/managing-current-user.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,22 @@ The Ember Simple Auth session can either be authenticated already when the
application starts up or become authenticated later when either the user logs
in via that instance of the application or the session state is synced from
another tab or window. In the first case, the session will already be
authenticated when the application route's `beforeModel` method is called and
in the latter case Ember Simple Auth will call the application route's
`sessionAuthenticated` method. The `currentUser` service's `load` method must
be called in both cases so that it's `user` property is always populated when
the session is authenticated:
authenticated when the application route's `beforeModel` method is called:

```js
// app/routes/application.js

import Route from '@ember/routing/route';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

import { inject as service } from '@ember/service';

export default class ApplicationRoute extends Route.extend(ApplicationRouteMixin) {
export default class ApplicationRoute extends Route {
@service session;
@service currentUser;

beforeModel() {
return this._loadCurrentUser();
},

async sessionAuthenticated() {
let _super = this._super;
await this._loadCurrentUser();
_super.call(this, ...arguments);
},

async _loadCurrentUser() {
try {
await this.currentUser.load();
Expand All @@ -162,3 +151,28 @@ export default class ApplicationRoute extends Route.extend(ApplicationRouteMixin
}
});
```

In the latter case Ember Simple Auth will call the session service's
`handleAuthentication` method. The `currentUser` service's `load` method must
be called in that cases as well. We can do that by overriding the session
sevice's method in a custom extension of Ember Simple Auth's standard session
service:

```js
import { inject as service } from '@ember/service';
import SessionService from 'ember-simple-auth/services/session';

export default SessionService.extend({
@service currentUser;

async handleAuthentication() {
this._super(...arguments);

try {
await this.currentUser.load();
} catch(err) {
await this.invalidate();
}
}
});
```

0 comments on commit b6704d8

Please sign in to comment.