Class: Clearance::FacebookController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/clearance/facebook_controller.rb

Instance Method Summary collapse

Instance Method Details

#closedObject

Js is informing that the query as cleared



28
29
30
31
32
# File 'app/controllers/clearance/facebook_controller.rb', line 28

def closed
  sign_out
  flash[:notice] = translate(:facebook_closed, :default =>  "Facebook session expired.")
  render :template => 'sessions/new'
end

#fbtoken(mycode) ⇒ Object



71
72
73
74
# File 'app/controllers/clearance/facebook_controller.rb', line 71

def fbtoken(mycode)
  access_token_hash = MiniFB.oauth_access_token(FB_APP_ID, FB_CALLBACK_URL, FB_SECRET, mycode)
  return access_token = access_token_hash["access_token"]
end

#find_fbuser(myfbuid) ⇒ Object

This method return nil if theres no such user and the user if there is



67
68
69
# File 'app/controllers/clearance/facebook_controller.rb', line 67

def find_fbuser(myfbuid)
  return ::User.find_by_fbid(myfbuid)
end

#indexObject

Js is informing that the cookie was created



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/clearance/facebook_controller.rb', line 4

def index
  if signed_in? then 
      redirect_to LOGGED_PATH #Evita multiples logins y hace que solo tenga sentido llamar el metodo con un nuevo cookie
  else #If there's no signed in user
      #The code arrives here
      user_id = token_user(params[:token])
      if user_id != nil then
        @user = nil
        @user = find_fbuser(user_id) #The one from the DB
        #If the user exists
        if !@user.nil? then
          (@user, params[:token], params[:expiration])
          redirect_to LOGGED_PATH
        else #If theres no user with that id
          #Register this user
          register_fbu(params[:token])
        end   
      else #The token isn't valid
        closed
      end
  end 
end

#register_fbu(token) ⇒ Object

Here I reply the create the new user, I changed te verifications so that fbid is unique and password is optional when fbid isn’t blank



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/clearance/facebook_controller.rb', line 40

def register_fbu(token)
  #get the user from FB
  ##Works sometimes, others sends: JSON::ParserError Exception: source did not contain any JSON!  
  #Bucle posiblemente infinito y chambon para lidiar con la excepcion mientras se soluciona  
  incomplete = true
  while incomplete do
    begin  
      incomplete = false #Intento salir del bucle
      new_user = MiniFB::OAuthSession.new(token, 'es_ES').get "me" 
    rescue JSON::ParserError
      incomplete = true #Reingreso en el bucle
    end
  end
  
  #Build th user in DB
  @user = ::User.new
  @user.fbid = new_user.id
  @user.name = new_user.name
  @user.email2 = new_user.email 
  if @user.save
    (@user, params[:token], params[:expiration])
  else
    render :text => "Please contact the administrator, the Facebook user couldn't be created."
  end    
end