The development of the mobile apps for any server side web application is becoming very conventional these days. Many of our clients want us to develop mobile apps that integrate with their SocialEngine. This blog explains the integration between mobile app and SocialEngine using a simple example of authentication. We decided to use the SocialEngine basic module structure with the Restful and JSON architecture.
STEP 1: We first started with design of the simple login call that would later be called by the mobile app.
Request: POST /socialapi/auth/post/login HTTP/1.1 Content-Type: application/json { "username":"socialapi", "email":"[email protected]" //optional "password":"123456", } Response: 201 (Created) Content-Type: application/json { "status": true, "user": { "userId": 10, "email":"[email protected]", "username": "socialapi", "userToken": "75a5df6d7abeba4458d6826561f472b7.7142089", "gender": "male" "thumbnail": "http://socialapi/public/album_photo/c4/45/457f_2adc.jpg", }, "count":"10" //unread notifications count "message": "Login successful" }
STEP 2: In Socialengine website, we created a new module and installed the module in our socialengine website. We created a new controller and use Zend_Rest_Controller class as the base class.
<?php class Api_Controller_Auth extends Zend_Rest_Controller { public function postAction() { $operation = $this->_getParam ( 'auth' ); if ($operation == "login") { $data = Engine_Api::_ ()->getApi ( 'auth', 'api' )->userLogin ( $params ); } return $this->format ( $data ); } }
STEP 3:For the definition of the userLogin function ,we created new core class as Auth.php.
<?php class Api_Api_Auth extends Core_Api_Abstract { public function userLogin($params) { // get user login parameter // your implementation // check the user have existing account and user is verified clause // your implementation $authResult = $this->authenticate ( $username, $password ); $authCode = $authResult->getCode (); // user login status $success = Zend_Auth_Result::SUCCESS; // login success user date // your implementation // return user data // your implementation return $loggedin; } }
STEP 4:The definition of the user authenticate function called in the userLogin function.
public function authenticate($identity, $credential) { // user table $userTable = Engine_Api::_ ()->getItemTable ( 'user' ); // Identify the user bases on user login credentials $userIdentity = $userTable->select ()->from ( $userTable, 'user_id' )->where ( '`username` = ?', $identity )->limit ( 1 )->query ()->fetchColumn ( 0 ); $authAdapter = $this->getAuthAdapter ()->setIdentity ( $userIdentity )->setCredential ( $credential ); return $this->getAuth ()->authenticate ( $authAdapter ); }
STEP 5: In iOS app,We simply assigned the parameter username and password of POST login call.
NSMutableDictionary * parameters =[[NSMutableDictionary alloc] init]; DefaultInputs *input = [[WebService webServicesManager]defaults]; [parameters setObject:input.apiKey forKey:@"apiKey"]; [parameters setObject:input.secretKey forKey:@"secretKey"]; [parameters setObject:_usernameTxtField.text forKey:loginWithEmail]; [parameters setObject:_passwordTxtField.text forKey:@"password"]; NSString *path =@"/socialapi/auth/post/login"; [LoginModel getLoginDetail:parameters forPath:path errorBlock:errorHandler cacheBlock:cacheHandler responseBlock:completionHandler];
This blog is going to help you in the implementation of a mobile app with SocialEngine. To reduce your work, we have already created a full version of the above API in the Socialengine API Plugin, which comes with a range of functions. If you install it, you just have to develop the mobile app and you don’t have to write any code for SocialEngine.
For full documentation of the Socialengine REST API, please visit the website