Skip to content

Commit

Permalink
WIP: show how to extend the session service in the README
Browse files Browse the repository at this point in the history
Continuing from my comment here, there isn't any info about this in the docs so yet.
mainmatter#2285 (comment)
  • Loading branch information
mcfiredrill authored Jun 18, 2021
1 parent 1cca7aa commit b16d20b
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,54 @@ to customize those behaviours, these methods can be overridden when the
application defines its own session service that extends the one provided by
Ember Simple Auth.

You can create your own session service that extends SimpleAuthSessionService in order to customize the `handleAuthentication` method to
redirect to a default route or possibly load the current user.
```
import SimpleAuthSessionService from 'ember-simple-auth/services/session';
import { inject as service } from '@ember/service';
export default class SessionService extends SimpleAuthSessionService {
/**
* Inject the router service
*
* @var {Service}
*/
@service router;
/**
* Inject the current user service
*
* @var {Service}
*/
@service currentUser;
/**
* Overwrite the handle authentication method
*
* @var {Service}
*/
handleAuthentication() {
this.router.transitionTo('dashboard.boards');
this.loadCurrentUser();
}
/**
* Loads the current authenticated user
*
* @void
*/
async loadCurrentUser() {
try {
const user = await this.currentUser.load();
return user;
} catch (err) {
await this.session.invalidate();
}
}
}
```

To add authorization information to requests, you can use the session service
to check if the session is authenticated and access
authentication/authorization data, e.g. a token:
Expand Down

0 comments on commit b16d20b

Please sign in to comment.