Posted By

chrisaiv on 09/29/09


Tagged

Flex facebook


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

forze
mrgrob
davidino1978
zoo
donaldleegraham
tspitzr
dubogii
Josueriv


AS3: Facebook Authentication Class


 / Published in: ActionScript 3
 

I wrote an authentication class to ensure that every time a method is called, the Facebook user is logged in. This is by no means refined but it's certainly a nice way to demonstrate an idea. If you try to just copy and past this code you will probably not find much success. Instead just try to understand the idea behind it.

  1. package com.chrisaiv
  2. {
  3. import com.facebook.events.FacebookEvent;
  4. import com.facebook.utils.FacebookSessionUtil;
  5.  
  6. import flash.events.Event;
  7. import flash.events.EventDispatcher;
  8.  
  9. import mx.controls.Alert;
  10.  
  11. public class FBAuthenticate extends EventDispatcher
  12. {
  13. private var session:FacebookSessionUtil;
  14.  
  15. private var _currentMethod:Function;
  16.  
  17. public function FBAuthenticate( fbSession:FacebookSessionUtil, func:Function )
  18. {
  19. _currentMethod = func;
  20.  
  21. session = fbSession;
  22. session.addEventListener( FacebookEvent.WAITING_FOR_LOGIN, fbWaitingForLoginHandler, false, 0, true );
  23. session.addEventListener( FacebookEvent.CONNECT, fbOnConnectHandler, false, 0, true );
  24. session.login();
  25. }
  26.  
  27. /**************************************
  28. * Facebook Connect/Login Event Handlers
  29. **************************************/
  30. private function fbOnConnectHandler( e:FacebookEvent ):void
  31. {
  32. //Continue where the user last left off before requiring Authentication
  33. dispatchEvent( new Event( Event.COMPLETE ) );
  34. }
  35.  
  36. private function fbWaitingForLoginHandler( e:FacebookEvent ):void
  37. {
  38. showAlert( "Click OK after you've logged in", "Logging In" );
  39. }
  40.  
  41. private function fbValidateLogin( e:Event ):void
  42. {
  43. session.validateLogin();
  44. }
  45.  
  46. /**************************************
  47. * Alert
  48. **************************************/
  49. private function showAlert( message:String, header:String ):void
  50. {
  51. //The user has returned from the log-in page and are now clicking "OK"
  52. var alert:Alert = Alert.show( message, header );
  53. alert.addEventListener( Event.CLOSE, fbValidateLogin, false, 0, true );
  54. alert.addEventListener( Event.CLOSE, alertCloseHandler, false, 0, true );
  55. }
  56.  
  57. private function alertCloseHandler( e:Event ):void
  58. {
  59.  
  60. }
  61.  
  62. /**************************************
  63. * Getters / Setters
  64. **************************************/
  65. public function get currentMethod():Function
  66. {
  67. return _currentMethod;
  68. }
  69.  
  70. }
  71. }
  72.  
  73.  
  74. /**************************
  75. * This belongs outside of the class
  76. **************************/
  77.  
  78. private function isAuthenticated():Boolean
  79. {
  80. if( fbSession == null || !fbSession.facebook.is_connected ){
  81. return false;
  82. }
  83. return true;
  84. }
  85.  
  86. private function fbGoAuthenticate( method:Function ):void
  87. {
  88. fbAuthenticate = new FBAuthenticate( fbSession, method );
  89. fbAuthenticate.addEventListener( Event.COMPLETE, fbAuthenticateComplete, false, 0, true );
  90. }
  91.  
  92. private function fbAuthenticateComplete( e:Event ):void
  93. {
  94. updateStatus( "You are logged into Facebook" );
  95.  
  96. var currentMethod:Function = FBAuthenticate(e.currentTarget).currentMethod;
  97. //Continue where the User Last Left Off
  98. currentMethod();
  99. }
  100. private function getPhotoAlbums():void
  101. {
  102. //-- AUTHENTICATE USER: Force USER to Login() Before calling FB API
  103. if( !isAuthenticated() ){ fbGoAuthenticate( arguments.callee ); return; }
  104.  
  105. var call:FacebookCall = fbook.post( new GetAlbums( fbook.uid ) );
  106. call.addEventListener( FacebookEvent.COMPLETE, photoAlbumsCompleteHandler, false, 0, true );
  107. }
  108.  
  109. private function photoAlbumsCompleteHandler( e:FacebookEvent ):void
  110. {
  111. var albumsResponseData:GetAlbumsData = e.data as GetAlbumsData;
  112. }

Report this snippet  

You need to login to post a comment.