DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
        • behaviors
          • ./service/
          • ./session/
            • options
              • feathersClient
            • data methods
              • createData
              • destroyData
              • getData
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-connect-feathers/session/session

  • Edit on GitHub

feathersSession(baseConnect)

Connects DataInterface methods to the feathers-authentication-client plugin methods for authentication.

connect([
  feathersSession,
  realtime
], {
  feathersClient: feathersClient,
  Map: SessionMap
});

The feathers-session behavior uses the feathers-authentication-client to authenticate with a Feathers server. Three of the DataInterface methods are used:

  • createData attempts to authenticate with the Feathers server, which upon success returns a JSON Web Token (JWT). The JWT contains a payload with information about the current session. That payload is returned as the session object.
  • getData validates a stored JWT and returns its payload if the token hasn't expired.
  • destroyData unauthenticates from the server and discards the JWT token on the client.

Use

Setting up the Feathers Client is a prerequisite for using this behavior. See the can-connect-feathers page for an example of a basic Feathers Client configuration. With the Feathers client setup, it can be used with the feathers-session behavior as demonstrated in the example, below.

// models/session.js
var connect = require('can-connect');
var DefineMap = require('can-define/map/');

var feathersSessionBehavior = require('can-connect-feathers/session');
var dataParse = require('can-connect/data/parse/');
var construct = require('can-connect/constructor/');
var constructStore = require('can-connect/constructor/store/');
var constructCallbacksOnce = require('can-connect/constructor/callbacks-once/');
var canMap = require('can-connect/can/map/');
var canRef = require('can-connect/can/ref/');
var dataCallbacks = require('can-connect/data/callbacks/');
// Bring in your user model to setup the relation in your DefineMap.
var User = require('./user');

// Bring in the feathersClient instance.
var feathersClient = require('./feathers');

export const Session = DefineMap.extend('Session', {
  seal: false
}, {
  exp: 'any',
  userId: 'any',
  user: {
    Type: User,
    // Automatically populate the user data when a userId is received.
    get (lastSetVal, resolve) {
      if (lastSetVal) {
        return lastSetVal;
      }
      if (this.userId) {
        User.get({_id: this.userId}).then(resolve);
      }
    }
  }
});

connect([
  // Include the feathers session behavior in the behaviors list.
  feathersSession,
  dataParse,
  canMap,
  canRef,
  construct,
  constructStore,
  constructCallbacksOnce,
  // Include the realtime behavior.
  realtime,
  dataCallbacks
], {
  // Pass the feathers client as the `feathersClient` property.
  feathersClient: feathersClient,
  idProp: 'exp',
  Map: Session,
  name: 'session'
});

Obtaining current session data

Once authentication has been established, the Map or DefineMap provided as the Map option on the can-connect Model will have a new current property defined. So, if you passed a Session object, Session.current will always hold the current session data. This greatly simplifies the session property in your application ViewModel. Here's an abbreviated example.

import Session from 'my-app/models/session';

const AppViewModel = DefineMap.extend({
  session: {
    get () {
      return Session.current;
    }
  }
});

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

Handling OAuth Logins

The feathers-session behavior is preconfigured to listen to login messages coming in over the 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.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.14.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news