how can i show user object data by passing user authentication_token in rails 5 restful json api?












0















in v1/controller



class V1::ProfilesController < ApplicationController



def index

if user = User.authentication_keys.present?

user = User.all

render json: {status: 'load', message:'load', user: 'user'},status: :ok

else

render json: {status: 'error', message:'error', user: 'user'},status: :ok

end
end


in routes



namespace :v1 do
resources :profiles
end


i am checking data in postman by passing authentication_token but it not show user object postman image]1



my user data in rails console



User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"










share|improve this question

























  • i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:24
















0















in v1/controller



class V1::ProfilesController < ApplicationController



def index

if user = User.authentication_keys.present?

user = User.all

render json: {status: 'load', message:'load', user: 'user'},status: :ok

else

render json: {status: 'error', message:'error', user: 'user'},status: :ok

end
end


in routes



namespace :v1 do
resources :profiles
end


i am checking data in postman by passing authentication_token but it not show user object postman image]1



my user data in rails console



User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"










share|improve this question

























  • i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:24














0












0








0








in v1/controller



class V1::ProfilesController < ApplicationController



def index

if user = User.authentication_keys.present?

user = User.all

render json: {status: 'load', message:'load', user: 'user'},status: :ok

else

render json: {status: 'error', message:'error', user: 'user'},status: :ok

end
end


in routes



namespace :v1 do
resources :profiles
end


i am checking data in postman by passing authentication_token but it not show user object postman image]1



my user data in rails console



User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"










share|improve this question
















in v1/controller



class V1::ProfilesController < ApplicationController



def index

if user = User.authentication_keys.present?

user = User.all

render json: {status: 'load', message:'load', user: 'user'},status: :ok

else

render json: {status: 'error', message:'error', user: 'user'},status: :ok

end
end


in routes



namespace :v1 do
resources :profiles
end


i am checking data in postman by passing authentication_token but it not show user object postman image]1



my user data in rails console



User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"







ruby ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:43







adarsh

















asked Nov 14 '18 at 8:48









adarshadarsh

12411




12411













  • i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:24



















  • i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:24

















i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

– adarsh
Nov 14 '18 at 12:24





i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

– adarsh
Nov 14 '18 at 12:24












2 Answers
2






active

oldest

votes


















0














You are passing 'user' as a string to your json. So it will just send that string.
You could try doing something like:



render json: {status: 'load', message:'load', user: user.to_json},status: :ok


Or even:



render json: user


That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.



edit



OK, from your comment I think you probably need something along the lines of:



def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end





share|improve this answer


























  • users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help

    – adarsh
    Nov 14 '18 at 12:20





















0














In successful case, you respond with the following json:



{status: 'load', message:'load'}



so everything is working as you described in controller. If you want to return user object, then change it to something like:



render json: { status: 'load', message:'load', user: 'user' }, status: :ok






share|improve this answer
























  • it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object

    – adarsh
    Nov 14 '18 at 9:35













  • i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:22













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296141%2fhow-can-i-show-user-object-data-by-passing-user-authentication-token-in-rails-5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You are passing 'user' as a string to your json. So it will just send that string.
You could try doing something like:



render json: {status: 'load', message:'load', user: user.to_json},status: :ok


Or even:



render json: user


That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.



edit



OK, from your comment I think you probably need something along the lines of:



def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end





share|improve this answer


























  • users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help

    – adarsh
    Nov 14 '18 at 12:20


















0














You are passing 'user' as a string to your json. So it will just send that string.
You could try doing something like:



render json: {status: 'load', message:'load', user: user.to_json},status: :ok


Or even:



render json: user


That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.



edit



OK, from your comment I think you probably need something along the lines of:



def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end





share|improve this answer


























  • users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help

    – adarsh
    Nov 14 '18 at 12:20
















0












0








0







You are passing 'user' as a string to your json. So it will just send that string.
You could try doing something like:



render json: {status: 'load', message:'load', user: user.to_json},status: :ok


Or even:



render json: user


That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.



edit



OK, from your comment I think you probably need something along the lines of:



def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end





share|improve this answer















You are passing 'user' as a string to your json. So it will just send that string.
You could try doing something like:



render json: {status: 'load', message:'load', user: user.to_json},status: :ok


Or even:



render json: user


That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.



edit



OK, from your comment I think you probably need something along the lines of:



def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 14:48

























answered Nov 14 '18 at 10:07









A_rayBA_rayB

863




863













  • users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help

    – adarsh
    Nov 14 '18 at 12:20





















  • users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help

    – adarsh
    Nov 14 '18 at 12:20



















users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help

– adarsh
Nov 14 '18 at 12:20







users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help

– adarsh
Nov 14 '18 at 12:20















0














In successful case, you respond with the following json:



{status: 'load', message:'load'}



so everything is working as you described in controller. If you want to return user object, then change it to something like:



render json: { status: 'load', message:'load', user: 'user' }, status: :ok






share|improve this answer
























  • it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object

    – adarsh
    Nov 14 '18 at 9:35













  • i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:22


















0














In successful case, you respond with the following json:



{status: 'load', message:'load'}



so everything is working as you described in controller. If you want to return user object, then change it to something like:



render json: { status: 'load', message:'load', user: 'user' }, status: :ok






share|improve this answer
























  • it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object

    – adarsh
    Nov 14 '18 at 9:35













  • i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:22
















0












0








0







In successful case, you respond with the following json:



{status: 'load', message:'load'}



so everything is working as you described in controller. If you want to return user object, then change it to something like:



render json: { status: 'load', message:'load', user: 'user' }, status: :ok






share|improve this answer













In successful case, you respond with the following json:



{status: 'load', message:'load'}



so everything is working as you described in controller. If you want to return user object, then change it to something like:



render json: { status: 'load', message:'load', user: 'user' }, status: :ok







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 9:15









sharipov_rusharipov_ru

490310




490310













  • it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object

    – adarsh
    Nov 14 '18 at 9:35













  • i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:22





















  • it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object

    – adarsh
    Nov 14 '18 at 9:35













  • i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

    – adarsh
    Nov 14 '18 at 12:22



















it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object

– adarsh
Nov 14 '18 at 9:35







it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object

– adarsh
Nov 14 '18 at 9:35















i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

– adarsh
Nov 14 '18 at 12:22







i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users

– adarsh
Nov 14 '18 at 12:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296141%2fhow-can-i-show-user-object-data-by-passing-user-authentication-token-in-rails-5%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma