Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- All the information here is relevant to the task and all app secrets etc. in initializer file are fake
- ---
- `Rails Version: 5.1`
- `Ruby Version: 2.3.1`
- Flow:
- - app makes __POST__ request to `dev.somewebsite.com/api/v1/guest/registrations/social_login`
- - the params contain the `code` attribute obtained from __facebook__ or __google+__
- - `params: {code: 'hkjhkjfdyi783iuhkjku8iuhrkwfyiy78u32ioufjfouj3rou8iy788r9fijlekiuy3jlfek2ijou8}`
- - expected
- - 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)
- - user details are returned by the api
- _Gemfile_
- ---
- ```
- gem 'omniauth-facebook'
- gem 'omniauth-google-oauth2'
- gem 'omniauth-oauth2', '~> 1.3.1'
- ```
- _config/initializers/omniauth.rb_
- ---
- ```
- OmniAuth.config.logger = Rails.logger
- Rails.application.config.middleware.use OmniAuth::Builder do
- provider :facebook,
- "1234567890987654321",
- "1a2b3c4d5f6r6d687585",
- scope: 'public_profile',
- info_fields: 'id,name,age_range,gender,locale,timezone,picture'
- provider :google_oauth2,
- "abc123abc123abc123.apps.googleusercontent.com",
- "HG786hgjh3208kjhh",
- scope: 'userinfo.profile'
- end
- end
- ```
- _config/routes.rb_
- ---
- ```
- namespace :api, defaults: { format: :json } do
- namespace :v1 do
- namespace :guest do
- post 'registrations/social', to: 'registrations#social_login', as: :social_login
- end
- end
- end
- ```
- _app/controllers/api/v1/guest/base_controller.rb_
- ---
- ```
- class Api::V1::Guest::BaseController < ActionController::Base
- private
- def render_login_details(user)
- render json: {
- data: {
- id: user.id,
- email: user.email,
- token: user.token,
- province_id: user.profile.province_id,
- name: user.name,
- profile_picture: user.profile.picture_url,
- provider: user.provider,
- deal_categories: DealCategory.get_colours
- }
- }
- rescue
- render json: {
- message: {
- user: ['something went wrong']
- }
- }, status: 400
- end
- end
- ```
- _app/controllers/api/v1/guest/registrations_controller.rb_
- ---
- ```
- class Api::V1::Guest::RegistrationsController < Api::V1::Guest::BaseController
- def social_login
- auth = request.env["omniauth.auth"]
- user = User.where(:provider => auth['provider'],
- :uid => auth['uid'].to_s).first || User.create_with_omniauth(auth)
- render_login_details(user)
- end
- end
- ```
- _app/models/concerns/authable.rb_
- ---
- This concern is included in the User Model
- ```
- module Authable
- extend ActiveSupport::Concern
- module ClassMethods
- def create_with_omniauth(auth)
- user = User.find_or_create_by(uid: auth['uid'], provider: auth['provider'])
- user.email = "#{auth['uid']}@#{auth['provider']}.com"
- user.password = auth['uid']
- user.name = auth['info']['name'] || nil
- user.city = 'toronto'
- user.gender = auth['extra']['raw_info'].gender || nil
- user.birthday = (Date.today - 23.years).to_date
- user.role_id = Role.where(description: "customer").first.id
- user.profile_picture = get_pic_url(user.provider, auth['extra']['raw_info'].picture) || nil
- if User.exists?(user)
- user
- else
- user.save!
- user
- end
- end
- private
- def get_pic_url(provider, info)
- provider == 'facebook' ? info.data.url : info
- end
- end
- end
- ```
- _User Table_
- ---
- ```
- # == Schema Information
- #
- # Table name: users
- #
- # id :integer not null, primary key
- # email :string default(""), not null
- # encrypted_password :string default(""), not null
- # reset_password_token :string
- # reset_password_sent_at :datetime
- # remember_created_at :datetime
- # sign_in_count :integer default(0), not null
- # current_sign_in_at :datetime
- # last_sign_in_at :datetime
- # current_sign_in_ip :inet
- # last_sign_in_ip :inet
- # created_at :datetime not null
- # updated_at :datetime not null
- # role_id :integer not null
- # name :string
- # city :string
- # token :text
- # provider :string
- # uid :string
- # gender :string
- # birthday :date
- # profile_picture :string
- #
- # Indexes
- #
- # index_users_on_email (email) UNIQUE
- # index_users_on_reset_password_token (reset_password_token) UNIQUE
- #
- ```
Add Comment
Please, Sign In to add comment