Class: Api::V1::AuthenticationController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/fbuser/api/v1/authentication_controller.rb

Instance Method Summary collapse

Instance Method Details

#loginObject



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
# File 'app/controllers/fbuser/api/v1/authentication_controller.rb', line 7

def 
  short_token = params[:token]
  fb_api = ::Fbuser::Facebook.new
  long_token = fb_api.get_long_token(short_token)
  fb_user_id = get_id(fb_api,long_token,short_token)
  if fb_user_id.nil?
    #Couldn't retrieve the facebook user_id from token
    render :json => {error: "Could not retrieve the facebook user_id from your token"}
    return
  end
  @user = ::Fbuser::V1::User.find_by(fb_user_id: fb_user_id)
  if @user.nil?
    #Create new user
    params = {username: "user#{fb_user_id}", fb_user_id: fb_user_id}
    @user = ::Fbuser::V1::User.create(params)
    @api_token = @user.tokens[0]
  else
    #Log in a user
    @api_token = @user.tokens.create
  end
  ::Arcadex::Create.set_token(@api_token,20000,request,nil)
  userHash = {id: @user.id, username: @user.username, fb_user_id: @user.fb_user_id}
  objHash = {user: userHash, api_token: @api_token.auth_token, fb_token: @token}
  render :json => objHash
end

#logoutObject



33
34
35
36
# File 'app/controllers/fbuser/api/v1/authentication_controller.rb', line 33

def logout
  current_token.destroy
  render :json => {}
end