Class: Sprangular::FacebookController
- Inherits:
-
BaseController
- Object
- Spree::BaseController
- BaseController
- Sprangular::FacebookController
- Defined in:
- app/controllers/sprangular/facebook_controller.rb
Instance Method Summary collapse
Methods inherited from BaseController
#invalid_resource!, #not_found, #unauthorized
Instance Method Details
#fetch ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/controllers/sprangular/facebook_controller.rb', line 3 def fetch access_token = params['accessToken'] supplied_email = params['email'] fb_user = FbGraph::User.me(access_token).fetch if fb_user # valid token user_authentication = Spree::UserAuthentication.find_or_create_by(uid: fb_user.identifier, provider: 'facebook') if user_authentication && user_authentication.user sign_in :spree_user, user_authentication.user render json: user_authentication.user else email = fb_user.email email ||= supplied_email if email.present? user_authentication = Spree::UserAuthentication.find_or_create_by(uid: fb_user.identifier, provider: 'facebook') if user_authentication.user.blank? user = Spree::User.find_by_email email if user.blank? password = SecureRandom.hex(16) user = Spree::User.create! email: email, password: password, password_confirmation: password end user_authentication.update_attribute(:user_id, user.id) end sign_in :spree_user, user_authentication.user render json: user_authentication.user else error = 'no email provided by facebook' render json: error.to_json, status: 404 end end else error = 'no facebook user' render json: error.to_json, status: 403 end rescue => ex render json: ex..to_json, status: 422 end |