Guest User

Untitled

a guest
Nov 3rd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. All the information here is relevant to the task and all app secrets etc. in initializer file are fake
  2. ---
  3.  
  4. `Rails Version: 5.1`
  5. `Ruby Version: 2.3.1`
  6.  
  7. Flow:
  8.  
  9. - app makes __POST__ request to `dev.somewebsite.com/api/v1/guest/registrations/social_login`
  10. - the params contain the `code` attribute obtained from __facebook__ or __google+__
  11. - `params: {code: 'hkjhkjfdyi783iuhkjku8iuhrkwfyiy78u32ioufjfouj3rou8iy788r9fijlekiuy3jlfek2ijou8}`
  12. - expected
  13. - user is created with birthday, city, gender, profile picture (in order to do this let me know if I need to do anything on the facebook/google OAuth settings)
  14. - user details are returned by the api
  15.  
  16.  
  17.  
  18. _Gemfile_
  19. ---
  20.  
  21. ```
  22. gem 'omniauth-facebook'
  23. gem 'omniauth-google-oauth2'
  24. gem 'omniauth-oauth2', '~> 1.3.1'
  25. ```
  26.  
  27. _config/initializers/omniauth.rb_
  28. ---
  29.  
  30. ```
  31. OmniAuth.config.logger = Rails.logger
  32.  
  33. Rails.application.config.middleware.use OmniAuth::Builder do
  34. provider :facebook,
  35. "1234567890987654321",
  36. "1a2b3c4d5f6r6d687585",
  37. scope: 'public_profile',
  38. info_fields: 'id,name,age_range,gender,locale,timezone,picture'
  39.  
  40. provider :google_oauth2,
  41. "abc123abc123abc123.apps.googleusercontent.com",
  42. "HG786hgjh3208kjhh",
  43. scope: 'userinfo.profile'
  44. end
  45. end
  46. ```
  47.  
  48. _config/routes.rb_
  49. ---
  50.  
  51. ```
  52. namespace :api, defaults: { format: :json } do
  53. namespace :v1 do
  54. namespace :guest do
  55. post 'registrations/social', to: 'registrations#social_login', as: :social_login
  56. end
  57. end
  58. end
  59. ```
  60.  
  61. _app/controllers/api/v1/guest/base_controller.rb_
  62. ---
  63.  
  64. ```
  65. class Api::V1::Guest::BaseController < ActionController::Base
  66.  
  67. private
  68.  
  69. def render_login_details(user)
  70. render json: {
  71. data: {
  72. id: user.id,
  73. email: user.email,
  74. token: user.token,
  75. province_id: user.profile.province_id,
  76. name: user.name,
  77. profile_picture: user.profile.picture_url,
  78. provider: user.provider,
  79. deal_categories: DealCategory.get_colours
  80. }
  81. }
  82. rescue
  83. render json: {
  84. message: {
  85. user: ['something went wrong']
  86. }
  87. }, status: 400
  88. end
  89. end
  90.  
  91. ```
  92.  
  93. _app/controllers/api/v1/guest/registrations_controller.rb_
  94. ---
  95.  
  96. ```
  97. class Api::V1::Guest::RegistrationsController < Api::V1::Guest::BaseController
  98. def social_login
  99. auth = request.env["omniauth.auth"]
  100. user = User.where(:provider => auth['provider'],
  101. :uid => auth['uid'].to_s).first || User.create_with_omniauth(auth)
  102. render_login_details(user)
  103. end
  104. end
  105. ```
  106.  
  107. _app/models/concerns/authable.rb_
  108. ---
  109.  
  110. This concern is included in the User Model
  111.  
  112. ```
  113. module Authable
  114. extend ActiveSupport::Concern
  115.  
  116. module ClassMethods
  117.  
  118. def create_with_omniauth(auth)
  119. user = User.find_or_create_by(uid: auth['uid'], provider: auth['provider'])
  120. user.email = "#{auth['uid']}@#{auth['provider']}.com"
  121. user.password = auth['uid']
  122. user.name = auth['info']['name'] || nil
  123. user.city = 'toronto'
  124. user.gender = auth['extra']['raw_info'].gender || nil
  125. user.birthday = (Date.today - 23.years).to_date
  126. user.role_id = Role.where(description: "customer").first.id
  127. user.profile_picture = get_pic_url(user.provider, auth['extra']['raw_info'].picture) || nil
  128.  
  129. if User.exists?(user)
  130. user
  131. else
  132. user.save!
  133. user
  134. end
  135. end
  136.  
  137. private
  138.  
  139. def get_pic_url(provider, info)
  140. provider == 'facebook' ? info.data.url : info
  141. end
  142. end
  143. end
  144. ```
  145.  
  146. _User Table_
  147. ---
  148.  
  149. ```
  150. # == Schema Information
  151. #
  152. # Table name: users
  153. #
  154. # id :integer not null, primary key
  155. # email :string default(""), not null
  156. # encrypted_password :string default(""), not null
  157. # reset_password_token :string
  158. # reset_password_sent_at :datetime
  159. # remember_created_at :datetime
  160. # sign_in_count :integer default(0), not null
  161. # current_sign_in_at :datetime
  162. # last_sign_in_at :datetime
  163. # current_sign_in_ip :inet
  164. # last_sign_in_ip :inet
  165. # created_at :datetime not null
  166. # updated_at :datetime not null
  167. # role_id :integer not null
  168. # name :string
  169. # city :string
  170. # token :text
  171. # provider :string
  172. # uid :string
  173. # gender :string
  174. # birthday :date
  175. # profile_picture :string
  176. #
  177. # Indexes
  178. #
  179. # index_users_on_email (email) UNIQUE
  180. # index_users_on_reset_password_token (reset_password_token) UNIQUE
  181. #
  182. ```
Add Comment
Please, Sign In to add comment