Class: Spree::Api::V2::Platform::FirebaseController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/spree/api/v2/platform/firebase_controller.rb

Instance Method Summary collapse

Instance Method Details

#findEmail(userInfo) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/spree/api/v2/platform/firebase_controller.rb', line 48

def findEmail(userInfo)
  providerInfo = userInfo['users'][0]['providerUserInfo'][0]['providerId']
  federatedID = userInfo['users'][0]['providerUserInfo'][0]['federatedId']
  if providerInfo == 'password'
    email = userInfo['users'][0]['email']
  elsif providerInfo == 'apple.com'
    email = federatedID + '@apple.com'
  elsif providerInfo == 'facebook.com'
    email = federatedID + '@facebook.com'
  else
    email = federatedID + '@gmail.com'
  end
  email
end

#findPassword(userInfo) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'app/controllers/spree/api/v2/platform/firebase_controller.rb', line 63

def findPassword(userInfo)
  providerInfo = userInfo['users'][0]['providerUserInfo'][0]['providerId']
  if providerInfo == 'password'
    password = userInfo['users'][0]['passwordHash']
  else
    password = userInfo['users'][0]['localId']
  end
  password
end

#firebase_loginObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/spree/api/v2/platform/firebase_controller.rb', line 23

def 
    token = firebase_params.fetch(:token)
    firebase_infos = firebase_verification(token)
    email = findEmail(firebase_infos)
    password = findPassword(firebase_infos)
    user = Spree.user_class.find_for_database_authentication(email: email)
    if user.nil?
      user = ::Spree::User.new
      user.email = email
      user.password = password
      user. = firebase_infos
      user.save
    end
    doorkeeper_response = HTTParty.post(
      'https://ca97-204-157-173-118.ngrok-free.app/spree_oauth/token/', 
      headers: { 'Content-Type' => 'application/json' }, 
      body: { 
        'grant_type' => 'password',
        'scope' => 'admin',
        'username' => email,
        'password' => password
      }.to_json )
    render json: doorkeeper_response
end

#firebase_paramsObject



17
18
19
20
21
# File 'app/controllers/spree/api/v2/platform/firebase_controller.rb', line 17

def firebase_params
    params
      .require(:data)
      .permit(:token)
end

#firebase_verification(token) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'app/controllers/spree/api/v2/platform/firebase_controller.rb', line 7

def firebase_verification(token)
    url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=AIzaSyCfY9JHxTinBizeZX7rt9E42dPRgj9NiNQ"
    firebase_verification_call = HTTParty.post(url, headers: { 'Content-Type' => 'application/json' }, body: { 'idToken' => token }.to_json )
    if firebase_verification_call.response.code == "200"
      firebase_infos = firebase_verification_call.parsed_response
    else
      raise 'Error'
    end
end