npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-ted-session

v1.1.0

Published

Easy user session management for your TED application

Downloads

2

Readme

Ember-ted-session

This addon allows your Ember app to quickly pull in session based data, like current user, from your TED backend.

Requirements

  • JSONAPI
  • Your application must have a User model.
  • If using authorization, your User model must have an isAuthorized property

Install

ember install ember-ted-session

Usage

Current user

As soon as your application boots up you'll probably want to fetch the current user. The ted-session service has a #fetch method for that.

// application/route.js

export default Ember.Route.extend({
  tedSession: Ember.inject.service(),

  beforeModel() {
    this.get('tedSession').fetch();
  }
});

Now any component can access the current user by just injecting the service.

// my-widget/component.js

export default Ember.Component.exnted({
  tedSession: Ember.inject.service(),
  currentUser: Ember.computed.readOnly('tedSession.currentUser')
});

Logging in

If you want to build a login form to let users login you can also use the ted-session service to authenticate with the backend.

// login/route.js

export default Ember.Route.extend({
  tedSession: Ember.inject.service(),

  actions: {
    login(email, password) {
      this.get('tedSession')
        .login(email
          , password)
        .then(() => console.log('it worked'))
        .catch(() => console.loa('nope'));
    }
  }
});

Generating an unauthorized route

If your app distinguishes between authorized and un-authorized users (eg. not all authenticated users are authorized), you will probably want to redirect unauthorized users to a page explaining what happened. This addon contains a custom generator for creating this automagically.

Requirements:
  • a named unauthorized outlet in your application template: {{outlet 'unauthorized'}}
Usage:
  • ember generate unauthorized-route will create a route named unauthorized and add it to your app's router.
  • ember generate unauthorized-route aw-hells-no will create the same but with your custom name (aw-hells-no in this case).

This generator will use your app's pod configuration and also accepts ember-cli's --pod flag.

Session service API

API | Type | About | Returns | Example --- | --- | --- | --- | --- fetch() | function | Fetches the current user from the backend | Promise | tedSessionService.fetch() terminate() | function | Tells the backend to log the current user out | Promise | tedSessionService.terminate() login(email, password) | function | Logs in the user | Promise | tedSessionService.login('[email protected]', 'password'); currentUser | property | Returns the current user | User DS.Model | tedSession.get('currentUser') isLoggedIn | property | Is there a current user | Boolean | tedSession.get('isLoggedIn') isNotLoggedIn | property | Is there no current user | Boolean | tedSession.get('isNotLoggedIn') isAuthorized | property | Returns isAuthorized property of the current user model, false if unavailable. | Boolean | tedSession.get('isAuthorized')

Details

Payload

The get expects a JSON API document.

// GET ted-sessions/current

{
  "data": {
    "id": "current",
    "type": "ted-sessions",
    "links": {
      "self": "/ted-sessions/current"
    },
    "attributes": {
      "csrf-token": "token"
    },
    "relationships": {
      "user": {
        `isAuthorized`: true // optional 
        "links": {
          "self": "/ted-sessions/current/relationships/user",
          "related": "/ted-sessions/current/user"
        }
      }
    }
  }
}

The post however is formatted to match the JSON that a normal devise session controller would expect.

// POST ted-sessions/current

"user": {
  "email": "email",
  "password": "password"
}

CSRF token

If your response payload includes an attribute called csrf-token then it will be used as the X-CSRF-TOKEN in all future XHR requests.