Skip to content

Commit

Permalink
set isAuthenticated flag for session
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyavf committed Feb 2, 2017
1 parent 72e8910 commit 03b64ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion _docs/session/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ const AppViewModel = DefineMap.extend({

That's it! The `session` property in the above example will automatically populate when the user authenticates.

### Detecting when the socket is authenticated

You don't want to make requests that require auth before the Socket.io connection has been authenticated. If you do, you'll see
`NotAuthenticated` errors in the console. To prevent these errors you can watch for the `Session.current.isAuthenticated`
attribute. If you have a component that immediately fetches data on render, you can conditionally render it:

```hbs
{{#if Session.current.isAuthenticated}}
<component-that-loads-data />
{{/if}}
```

### Handling OAuth Logins

The `feathers-session` behavior is preconfigured to listen to `login` messages coming in over the [feathers-authentication-popups](https://github.com/feathersjs/feathers-authentication-popups) `authAgent`. When any message is received through the `authAgent`, its validity is checked. If it's a valid JWT token, a Session instance will be created automatically. This will both populate `Session.current` and dispatch a `created` event on the connected Session Map.
The `feathers-session` behavior is preconfigured to listen to `login` messages coming in over the [feathers-authentication-popups](https://github.com/feathersjs/feathers-authentication-popups) `authAgent`. When any message is received through the `authAgent`, its validity is checked. If it's a valid JWT token, a Session instance will be created automatically. This will both populate `Session.current` and dispatch a `created` event on the connected Session Map.
6 changes: 5 additions & 1 deletion session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.exports = connect.behavior('data/feathers-session', function () {
feathersClient.authenticate({strategy: 'jwt', accessToken: token})
.then(function (data) {
var payload = decode(data.accessToken);
payload.isAuthenticated = true;
connection.createInstance(payload);
});
});
Expand All @@ -83,7 +84,9 @@ module.exports = connect.behavior('data/feathers-session', function () {
var requestData = convertLocalAuthData(data);
return feathersClient.authenticate(requestData)
.then(function (response) {
return decode(response.accessToken);
var payload = decode(response.accessToken)
payload.isAuthenticated = true;
return payload;
});
},
getData: function () {
Expand All @@ -92,6 +95,7 @@ module.exports = connect.behavior('data/feathers-session', function () {
feathersClient.authenticate()
.then(function (data) {
var payload = decode(data.accessToken);
payload.isAuthenticated = true;
return resolve(payload);
})
.catch(reject);
Expand Down

0 comments on commit 03b64ea

Please sign in to comment.