Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show how to extend the session service in the README #2310

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write super.handleAuthentication('dashboard.boards') instead. That way, the default logic defined here is kept as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bertdeblock How would I pass owner if I did that ?

export function handleSessionAuthenticated(owner, routeAfterAuthentication) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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