DoneJS StealJS jQuery++ FuncUnit DocumentJS
4.3.0
5.0.0 3.13.1 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-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-define
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Data Modeling
      • can-connect
      • can-connect-feathers
        • behaviors
          • ./service/
          • ./session/
            • options
              • feathersClient
            • data methods
              • createData
              • destroyData
              • getData
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • 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-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • 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-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • 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
import connect from "can-connect";

import DefineMap from "can-define/map/";
import feathersSessionBehavior from "can-connect-feathers/session";
import dataParse from "can-connect/data/parse/";
import construct from "can-connect/constructor/";
import constructStore from "can-connect/constructor/store/";
import constructCallbacksOnce from "can-connect/constructor/callbacks-once/";
import canMap from "can-connect/can/map/";
import canRef from "can-connect/can/ref/";
import dataCallbacks from "can-connect/data/callbacks/";

// Bring in your user model to setup the relation in your DefineMap.
import User from "./user";

// Bring in the feathersClient instance.
import feathersClient from "./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 4.3.0.

On this page

Get help

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