Skip to content

Commit

Permalink
Deserialize session using ember-data if installed
Browse files Browse the repository at this point in the history
  • Loading branch information
joefiorini committed Mar 19, 2014
1 parent e9d71ef commit afea458
Show file tree
Hide file tree
Showing 14 changed files with 11,200 additions and 11 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Configuration happens in `config/application.js` as part of the call to `create(
window.App = require('app').default.create({
deviseEmberAuth: {
signInPath: "/sign-in", // the URL users will see in the browser for the sign in page
userModelType: "user", // **ember-data only** name of the model that represents your user; same thing you'd pass to `store.find("...")` in a route
deviseSignInPath: "/users/sign_in", // the URL to POST to for creating a session
deviseSignOutPath: "/users/sign_out", // the URL to DELETE to for signing out
currentSessionPath: "/sessions/current" // the URL for getting the current signed-in state; this is currently added by the gem
Expand Down Expand Up @@ -155,4 +156,15 @@ export default Ember.Route.extend({
});
```

#### Display information about currently signed-in user

You can access a `currentUser` property in any template to get details about the current user. If you are using ember-data, this will deserialize the `/sessions/current` response (provided by the support gem) using a configurable model name (defaults to "user").

For example, assuming you have a fullName & email property on your user model, you can say:

```handlebars
Signed in as: {{currentUser.fullName}} ({{currentUser.email}})
```


[©2014 D-I](http://www.d-i.co)
18 changes: 13 additions & 5 deletions app/models/authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ var Authenticator = Ember.Object.extend({
passwordWillChange: function() {
this.set("passwordInvalid", false);
}.observesBefore("password"),
setupSession: function(session) {
setupSession: function(store, session) {

if(store && typeof store.pushPayload === 'function') {
var type = this.get("userModelType");
store.pushPayload(type, session);
session = store.find(type, session[type].id);

This comment has been minimized.

Copy link
@abuiles

abuiles Mar 19, 2014

Contributor

s/session/currentUser ?

This comment has been minimized.

Copy link
@joefiorini

joefiorini via email Mar 19, 2014

Author Contributor
}

this.set("isSignedIn", true)
.set("currentUser", session);
return session;
Expand All @@ -20,19 +27,20 @@ var Authenticator = Ember.Object.extend({
this.set("isSignedIn", false)
.set("currentUser", null);
},
// store: ember-data store instance; how do we handle non-ember-data?
// Options: skip: true|false // Doesn't make ajax request for session
loadSession: function(storeOrFinder, options) {
loadSession: function(store, options) {
if(this.get("isSignedIn") && this.get("currentUser")) {
return Ember.RSVP.resolve(this.get("currentUser"));
} else if(options.skip) {
return Ember.RSVP.resolve(null);
} else {
return this._loadSession(options);
return this._loadSession(store, options);
}
},
_loadSession: function () {
_loadSession: function (store) {
var result,
setup = this.setupSession.bind(this),
setup = this.setupSession.bind(this, store),
teardown = this.teardownSession.bind(this);

return this.ajax("get", this.get("currentSessionPath"))
Expand Down
1 change: 1 addition & 0 deletions config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var defaults = {
signInPath: "/sign-in",
deviseSignInPath: "/users/sign_in",
deviseSignOutPath: "/users/sign_out",
userModelType: "user",
currentSessionPath: "/sessions/current"
};

Expand Down
3 changes: 2 additions & 1 deletion config/initializers/authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ var initializer = {
initialize: function(container, app) {
var signInPath = getSetting(app, "deviseSignInPath"),
signOutPath = getSetting(app, "deviseSignOutPath"),
userModelType = getSetting(app, "userModelType"),
currentSessionPath = getSetting(app, "currentSessionPath");

var auth = Authenticator.create();

auth.set("signInPath", signInPath)
.set("signOutPath", signOutPath)
.set("userModelType", userModelType)
.set("currentSessionPath", currentSessionPath);

container.register("devise-simple-auth:authenticator", auth, {instantiate: false});
Expand All @@ -21,4 +23,3 @@ var initializer = {
};

export default initializer;

2 changes: 1 addition & 1 deletion examples/ember-rails/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../../
specs:
ember_devise_simple_auth (0.4.2)
ember_devise_simple_auth (0.4.4)
devise (>= 3.0.0)

GEM
Expand Down
9 changes: 9 additions & 0 deletions examples/ember-rails/app/assets/javascripts/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DeviseSimpleAuthExample.User = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
email: DS.attr('string'),
fullName: function() {
return [this.get('firstName'),
this.get('lastName')].join(' ');
}.property('firstName', 'lastName')
});
2 changes: 1 addition & 1 deletion examples/ember-rails/app/assets/javascripts/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
DeviseSimpleAuthExample.Store = DS.Store.extend({
// Override the default adapter with the `DS.ActiveModelAdapter` which
// is built to work nicely with the ActiveModel::Serializers gem.
adapter: '_ams'
adapter: '-active-model'
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<h1>Dashboard</h1>

<p><em>You are signed in as:</em> {{currentUser.email}}</p>
<p><em>You are signed in as:</em> {{currentUser.fullName}} ({{currentUser.email}})</p>

<p>This is your dashboard, where you might some some really interesting stuff.</p>

<p>Actually, not really. We all know there is nothing <em>actually</em> interesting in this world.</p>
3 changes: 3 additions & 0 deletions examples/ember-rails/app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class UserSerializer < ActiveModel::Serializer
attributes :id, :first_name, :last_name, :email
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddNameToUsers < ActiveRecord::Migration
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
end
end
4 changes: 3 additions & 1 deletion examples/ember-rails/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140228151317) do
ActiveRecord::Schema.define(version: 20140319152327) do

create_table "users", force: true do |t|
t.datetime "created_at"
Expand All @@ -26,6 +26,8 @@
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "first_name"
t.string "last_name"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
Expand Down
2 changes: 1 addition & 1 deletion examples/ember-rails/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
password = 'abcd1234'
user = User.create_with(password: 'abcd1234').find_or_create_by(email: 'ember@example.com')
user = User.create_with(password: 'abcd1234').find_or_create_by(email: 'ember@example.com', first_name: 'Tomster', last_name: 'Katz')

puts
puts "Great! You've created a user!"
Expand Down
Loading

0 comments on commit afea458

Please sign in to comment.