Skip to content

Commit

Permalink
Replaced broken session with working cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
edlb committed Oct 16, 2015
1 parent e2ddb38 commit 97359a6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
6 changes: 4 additions & 2 deletions app/assets/javascripts/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
ws.init();
user.showFromSession(function(currentUser) {
joined(currentUser, true);
}, function() {
}, function(preventApply) {
vm.isReady = true;
$scope.$apply();
if (!preventApply) {
$scope.$apply();
}
});
}
function currentMessageInit() {
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/app/app.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

module.exports = require('angular').module('app', [
require('angular-cookies'),
require('./services/_index')
])
.controller('AppController', require('./app'))
Expand Down
17 changes: 14 additions & 3 deletions app/assets/javascripts/app/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module.exports = userService;

/* @ngInject */
function userService(ws) {
function userService($cookies, ws) {
// Values

// Service
Expand All @@ -16,10 +16,21 @@

// Functions
function showFromSession(success, failure) {
return ws.trigger('users.show_from_session', {}, success, failure);
var currentUserId = $cookies.get('current_user_id');
if (currentUserId !== undefined) {
return ws.trigger('users.show', { id: currentUserId }, function(currentUser) {
$cookies.put('current_user_id', JSON.parse(currentUser).id);
success(currentUser);
}, failure);
} else {
return failure(true);
}
}
function showOrCreate(name, success, failure) {
return ws.trigger('users.show_or_create', { name: name }, success, failure);
return ws.trigger('users.show_or_create', { name: name }, function(currentUser) {
$cookies.put('current_user_id', JSON.parse(currentUser).id);
success(currentUser);
}, failure);
}
}
})();
6 changes: 3 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ class UsersController < WebsocketRails::BaseController
def initialize_session
end

def show_from_session
if session.include?(:current_user_id) and User.exists?(session[:current_user_id])
trigger_success User.find(session[:current_user_id]).to_json
def show
if User.exists?(message[:id])
trigger_success User.find(message[:id]).to_json
else
trigger_failure
end
Expand Down
2 changes: 1 addition & 1 deletion config/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
end

namespace :users do
subscribe :show_from_session, to: UsersController, with_method: :show_from_session
subscribe :show, to: UsersController, with_method: :show
subscribe :show_or_create, to: UsersController, with_method: :show_or_create
end
end
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"angular": "^1.4.7",
"angular-cookies": "^1.4.7",
"browserify": "^11.2.0",
"browserify-incremental": "^3.0.1",
"browserify-ngannotate": "^1.0.1",
Expand Down

0 comments on commit 97359a6

Please sign in to comment.