API endpoint for retrieving the current user
This commit is contained in:
parent
af5254d3b4
commit
1dac3cfd64
|
@ -55,6 +55,14 @@ class SessionController < ApplicationController
|
||||||
render json: {result: "ok"}
|
render json: {result: "ok"}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def current
|
||||||
|
if current_user.present?
|
||||||
|
render_serialized(current_user, CurrentUserSerializer)
|
||||||
|
else
|
||||||
|
render nothing: true, status: 404
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
reset_session
|
reset_session
|
||||||
log_off_user
|
log_off_user
|
||||||
|
|
|
@ -113,6 +113,7 @@ Discourse::Application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get "session/current" => "session#current"
|
||||||
get "session/csrf" => "session#csrf"
|
get "session/csrf" => "session#csrf"
|
||||||
get "composer-messages" => "composer_messages#index"
|
get "composer-messages" => "composer_messages#index"
|
||||||
|
|
||||||
|
|
|
@ -199,4 +199,24 @@ describe SessionController do
|
||||||
|
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue