Log in to Rails app using Facebook SDK











up vote
1
down vote

favorite












I have built a Rails app where users can login using Facebook. They have an account on the Rails app but do any authentication in it other than creating a session if they have logged into Facebook successfully.



The first part is using the SDK to get the access_token if they have logged in:



FB.getLoginStatus(function (response) {
if(response.authResponse) {
$.post('/auth', {access_token: response.authResponse.access_token}, function(response) {
window.location.href = '/';
});
}
});


And then I pass this to my create method in my controller:



def create
user = User.from_facebook(params[:access_token])
session[:user_id] = user.id
end


And the model has the following methods:



def self.from_facebook(access_token)
# get the user from the FB API
fb_user = get_user(access_token)
# either return or create a user from the ID
where(id: fb_user.id).first_or_create do |user|
user.id = fb_user.id
user.name = fb_user.name
end
end

private

def get_user(access_token)
response = RestClient get "https://graph.facebook.com/me?access_token=#{access_token}"
end


So I basically either return an existing user or create a new user with the user id that comes from using the access token. This should prevent any unauthorized access and only log in the correct user as it means that the user is logged in using the access token to get the user from Facebook.



Are there any flaws in this method? I know I don't handle if the access_token is invalid, etc. I'm more interested in the security aspect of logging the user in by getting there ID from the token, but as this is all done server side, it seems pretty secure. I also don't want to use anything like Omniauth or other gems.










share|improve this question
















bumped to the homepage by Community 20 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    1
    down vote

    favorite












    I have built a Rails app where users can login using Facebook. They have an account on the Rails app but do any authentication in it other than creating a session if they have logged into Facebook successfully.



    The first part is using the SDK to get the access_token if they have logged in:



    FB.getLoginStatus(function (response) {
    if(response.authResponse) {
    $.post('/auth', {access_token: response.authResponse.access_token}, function(response) {
    window.location.href = '/';
    });
    }
    });


    And then I pass this to my create method in my controller:



    def create
    user = User.from_facebook(params[:access_token])
    session[:user_id] = user.id
    end


    And the model has the following methods:



    def self.from_facebook(access_token)
    # get the user from the FB API
    fb_user = get_user(access_token)
    # either return or create a user from the ID
    where(id: fb_user.id).first_or_create do |user|
    user.id = fb_user.id
    user.name = fb_user.name
    end
    end

    private

    def get_user(access_token)
    response = RestClient get "https://graph.facebook.com/me?access_token=#{access_token}"
    end


    So I basically either return an existing user or create a new user with the user id that comes from using the access token. This should prevent any unauthorized access and only log in the correct user as it means that the user is logged in using the access token to get the user from Facebook.



    Are there any flaws in this method? I know I don't handle if the access_token is invalid, etc. I'm more interested in the security aspect of logging the user in by getting there ID from the token, but as this is all done server side, it seems pretty secure. I also don't want to use anything like Omniauth or other gems.










    share|improve this question
















    bumped to the homepage by Community 20 hours ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have built a Rails app where users can login using Facebook. They have an account on the Rails app but do any authentication in it other than creating a session if they have logged into Facebook successfully.



      The first part is using the SDK to get the access_token if they have logged in:



      FB.getLoginStatus(function (response) {
      if(response.authResponse) {
      $.post('/auth', {access_token: response.authResponse.access_token}, function(response) {
      window.location.href = '/';
      });
      }
      });


      And then I pass this to my create method in my controller:



      def create
      user = User.from_facebook(params[:access_token])
      session[:user_id] = user.id
      end


      And the model has the following methods:



      def self.from_facebook(access_token)
      # get the user from the FB API
      fb_user = get_user(access_token)
      # either return or create a user from the ID
      where(id: fb_user.id).first_or_create do |user|
      user.id = fb_user.id
      user.name = fb_user.name
      end
      end

      private

      def get_user(access_token)
      response = RestClient get "https://graph.facebook.com/me?access_token=#{access_token}"
      end


      So I basically either return an existing user or create a new user with the user id that comes from using the access token. This should prevent any unauthorized access and only log in the correct user as it means that the user is logged in using the access token to get the user from Facebook.



      Are there any flaws in this method? I know I don't handle if the access_token is invalid, etc. I'm more interested in the security aspect of logging the user in by getting there ID from the token, but as this is all done server side, it seems pretty secure. I also don't want to use anything like Omniauth or other gems.










      share|improve this question















      I have built a Rails app where users can login using Facebook. They have an account on the Rails app but do any authentication in it other than creating a session if they have logged into Facebook successfully.



      The first part is using the SDK to get the access_token if they have logged in:



      FB.getLoginStatus(function (response) {
      if(response.authResponse) {
      $.post('/auth', {access_token: response.authResponse.access_token}, function(response) {
      window.location.href = '/';
      });
      }
      });


      And then I pass this to my create method in my controller:



      def create
      user = User.from_facebook(params[:access_token])
      session[:user_id] = user.id
      end


      And the model has the following methods:



      def self.from_facebook(access_token)
      # get the user from the FB API
      fb_user = get_user(access_token)
      # either return or create a user from the ID
      where(id: fb_user.id).first_or_create do |user|
      user.id = fb_user.id
      user.name = fb_user.name
      end
      end

      private

      def get_user(access_token)
      response = RestClient get "https://graph.facebook.com/me?access_token=#{access_token}"
      end


      So I basically either return an existing user or create a new user with the user id that comes from using the access token. This should prevent any unauthorized access and only log in the correct user as it means that the user is logged in using the access token to get the user from Facebook.



      Are there any flaws in this method? I know I don't handle if the access_token is invalid, etc. I'm more interested in the security aspect of logging the user in by getting there ID from the token, but as this is all done server side, it seems pretty secure. I also don't want to use anything like Omniauth or other gems.







      javascript ruby-on-rails authentication facebook






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 27 '17 at 2:46









      200_success

      127k15148411




      127k15148411










      asked Jul 28 '17 at 22:40









      Cameron

      220110




      220110





      bumped to the homepage by Community 20 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 20 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I've implemented a few social apps before. What you've done is pretty standard practice when it comes to oauth authentication and is secure.



          The one thing you could change:



           where(id: fb_user.id).first_or_create do |user|
          user.name = fb_user.name
          end


          When you use first_or_create when it creates it'll set the where query as attributes. So in your case the id will already be set as fb_user.id.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            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: "196"
            };
            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',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fcodereview.stackexchange.com%2fquestions%2f171468%2flog-in-to-rails-app-using-facebook-sdk%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I've implemented a few social apps before. What you've done is pretty standard practice when it comes to oauth authentication and is secure.



            The one thing you could change:



             where(id: fb_user.id).first_or_create do |user|
            user.name = fb_user.name
            end


            When you use first_or_create when it creates it'll set the where query as attributes. So in your case the id will already be set as fb_user.id.






            share|improve this answer

























              up vote
              0
              down vote













              I've implemented a few social apps before. What you've done is pretty standard practice when it comes to oauth authentication and is secure.



              The one thing you could change:



               where(id: fb_user.id).first_or_create do |user|
              user.name = fb_user.name
              end


              When you use first_or_create when it creates it'll set the where query as attributes. So in your case the id will already be set as fb_user.id.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I've implemented a few social apps before. What you've done is pretty standard practice when it comes to oauth authentication and is secure.



                The one thing you could change:



                 where(id: fb_user.id).first_or_create do |user|
                user.name = fb_user.name
                end


                When you use first_or_create when it creates it'll set the where query as attributes. So in your case the id will already be set as fb_user.id.






                share|improve this answer












                I've implemented a few social apps before. What you've done is pretty standard practice when it comes to oauth authentication and is secure.



                The one thing you could change:



                 where(id: fb_user.id).first_or_create do |user|
                user.name = fb_user.name
                end


                When you use first_or_create when it creates it'll set the where query as attributes. So in your case the id will already be set as fb_user.id.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 29 '17 at 0:28









                Cameron Barker

                12




                12






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f171468%2flog-in-to-rails-app-using-facebook-sdk%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

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin