/ 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.
Expand |
Embed | Plain Text
package com.chrisaiv { import com.facebook.events.FacebookEvent; import com.facebook.utils.FacebookSessionUtil; import flash.events.Event; import flash.events.EventDispatcher; import mx.controls.Alert; public class FBAuthenticate extends EventDispatcher { private var session:FacebookSessionUtil; private var _currentMethod:Function; public function FBAuthenticate( fbSession:FacebookSessionUtil, func:Function ) { _currentMethod = func; session = fbSession; session.addEventListener( FacebookEvent.WAITING_FOR_LOGIN, fbWaitingForLoginHandler, false, 0, true ); session.addEventListener( FacebookEvent.CONNECT, fbOnConnectHandler, false, 0, true ); session.login(); } /************************************** * Facebook Connect/Login Event Handlers **************************************/ private function fbOnConnectHandler( e:FacebookEvent ):void { //Continue where the user last left off before requiring Authentication dispatchEvent( new Event( Event.COMPLETE ) ); } private function fbWaitingForLoginHandler( e:FacebookEvent ):void { showAlert( "Click OK after you've logged in", "Logging In" ); } private function fbValidateLogin( e:Event ):void { session.validateLogin(); } /************************************** * Alert **************************************/ private function showAlert( message:String, header:String ):void { //The user has returned from the log-in page and are now clicking "OK" var alert:Alert = Alert.show( message, header ); alert.addEventListener( Event.CLOSE, fbValidateLogin, false, 0, true ); alert.addEventListener( Event.CLOSE, alertCloseHandler, false, 0, true ); } private function alertCloseHandler( e:Event ):void { } /************************************** * Getters / Setters **************************************/ public function get currentMethod():Function { return _currentMethod; } } } /************************** * This belongs outside of the class **************************/ private function isAuthenticated():Boolean { if( fbSession == null || !fbSession.facebook.is_connected ){ return false; } return true; } private function fbGoAuthenticate( method:Function ):void { fbAuthenticate = new FBAuthenticate( fbSession, method ); fbAuthenticate.addEventListener( Event.COMPLETE, fbAuthenticateComplete, false, 0, true ); } private function fbAuthenticateComplete( e:Event ):void { updateStatus( "You are logged into Facebook" ); var currentMethod:Function = FBAuthenticate(e.currentTarget).currentMethod; //Continue where the User Last Left Off currentMethod(); } private function getPhotoAlbums():void { //-- AUTHENTICATE USER: Force USER to Login() Before calling FB API if( !isAuthenticated() ){ fbGoAuthenticate( arguments.callee ); return; } var call:FacebookCall = fbook.post( new GetAlbums( fbook.uid ) ); call.addEventListener( FacebookEvent.COMPLETE, photoAlbumsCompleteHandler, false, 0, true ); } private function photoAlbumsCompleteHandler( e:FacebookEvent ):void { var albumsResponseData:GetAlbumsData = e.data as GetAlbumsData; }
You need to login to post a comment.
