From 1dac3cfd64d73eb09be47f0c59c8b592b4b27389 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 5 Feb 2014 13:46:24 -0500 Subject: [PATCH] API endpoint for retrieving the current user --- app/controllers/session_controller.rb | 8 ++++++++ config/routes.rb | 1 + spec/controllers/session_controller_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index e762af5eca4..371a7fd4dda 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -55,6 +55,14 @@ class SessionController < ApplicationController render json: {result: "ok"} end + def current + if current_user.present? + render_serialized(current_user, CurrentUserSerializer) + else + render nothing: true, status: 404 + end + end + def destroy reset_session log_off_user diff --git a/config/routes.rb b/config/routes.rb index c22ac90fb8b..bc838c27bd7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -113,6 +113,7 @@ Discourse::Application.routes.draw do end end + get "session/current" => "session#current" get "session/csrf" => "session#csrf" get "composer-messages" => "composer_messages#index" diff --git a/spec/controllers/session_controller_spec.rb b/spec/controllers/session_controller_spec.rb index 98775b04325..4959351ce65 100644 --- a/spec/controllers/session_controller_spec.rb +++ b/spec/controllers/session_controller_spec.rb @@ -199,4 +199,24 @@ describe SessionController do end + describe '.current' do + context "when not logged in" do + it "retuns 404" do + xhr :get, :current + response.should_not be_success + end + end + + context "when logged in" do + let!(:user) { log_in } + + it "returns the JSON for the user" do + xhr :get, :current + response.should be_success + json = ::JSON.parse(response.body) + json['current_user'].should be_present + json['current_user']['id'].should == user.id + end + end + end end