Skip to content

Commit af13785

Browse files
vferdiansyahBenjamin Wilson Friedman
authored andcommitted
Add Twitter login helper
* Add Twitter login helper * Break down parameters * Make consumer secret optional * Add a general login and link method * Update login function and add new test cases
1 parent 66811f5 commit af13785

File tree

2 files changed

+339
-25
lines changed

2 files changed

+339
-25
lines changed

src/Parse/ParseUser.php

Lines changed: 173 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,76 @@ public static function logInWithFacebook($id, $access_token, $expiration_date =
164164
$expiration_date = new \DateTime();
165165
$expiration_date->setTimestamp(time() + 86400 * 60);
166166
}
167-
$data = ['authData' => [
168-
'facebook' => [
169-
'id' => $id, 'access_token' => $access_token,
170-
'expiration_date' => ParseClient::getProperDateFormat($expiration_date),
171-
],
172-
]];
173-
$result = ParseClient::_request('POST', 'users', '', json_encode($data));
174-
$user = ParseObject::create('_User');
175-
$user->_mergeAfterFetch($result);
176-
$user->handleSaveResult(true);
177-
ParseClient::getStorage()->set('user', $user);
167+
$authData = [
168+
'id' => $id,
169+
'access_token' => $access_token,
170+
'expiration_date' => ParseClient::getProperDateFormat($expiration_date),
171+
];
178172

179-
return $user;
173+
return self::logInWith('facebook', $authData);
174+
}
175+
176+
/**
177+
* Logs in with Twitter details, or throws if invalid.
178+
*
179+
* @param string $id the Twitter user identifier
180+
* @param string $screen_name the user's Twitter handle
181+
* @param string $consumer_key the application's consumer key
182+
* @param string $consumer_secret the application's consumer secret
183+
* @param string $auth_token the authorized Twitter token for the user
184+
* @param string $auth_token_secret the secret associated with $auth_token
185+
*
186+
* @throws ParseException
187+
*
188+
* @return ParseUser
189+
*/
190+
public static function logInWithTwitter(
191+
$id,
192+
$screen_name,
193+
$consumer_key,
194+
$consumer_secret = null,
195+
$auth_token,
196+
$auth_token_secret)
197+
{
198+
if (!$id) {
199+
throw new ParseException('Cannot log in Twitter user without an id.');
200+
}
201+
if (!$screen_name) {
202+
throw new ParseException(
203+
'Cannot log in Twitter user without Twitter screen name.'
204+
);
205+
}
206+
if (!$consumer_key) {
207+
throw new ParseException(
208+
'Cannot log in Twitter user without a consumer key.'
209+
);
210+
}
211+
if (!$auth_token) {
212+
throw new ParseException(
213+
'Cannot log in Twitter user without an auth token.'
214+
);
215+
}
216+
if (!$auth_token_secret) {
217+
throw new ParseException(
218+
'Cannot log in Twitter user without an auth token secret.'
219+
);
220+
}
221+
$authData = [
222+
'id' => $id,
223+
'screen_name' => $screen_name,
224+
'consumer_key' => $consumer_key,
225+
'consumer_secret' => $consumer_secret,
226+
'auth_token' => $auth_token,
227+
'auth_token_secret' => $auth_token_secret,
228+
];
229+
230+
return self::logInWith('twitter', $authData);
180231
}
181232

182233
/**
183-
* Login as an anonymous User with REST API. See docs https://www.parse.com/docs/php/guide#users
234+
* Login as an anonymous User with REST API. See docs http://parseplatform.org/docs/php/guide/#users
184235
*
185-
* @link https://www.parse.com/docs/rest/guide#users-anonymous-user-code-authdata-code-
236+
* @link http://parseplatform.org/docs/rest/guide/#anonymous-user-authdata
186237
*
187238
* @throws ParseException
188239
*
@@ -195,14 +246,28 @@ public static function loginWithAnonymous()
195246
* @link https://en.wikipedia.org/wiki/Universally_unique_identifier
196247
*/
197248
$uuid_parts = str_split(md5(mt_rand()), 4);
249+
$authData = [
250+
'id' => $uuid_parts[0].$uuid_parts[1].'-'.$uuid_parts[2].'-'.$uuid_parts[3].'-'.$uuid_parts[4].'-'.$uuid_parts[5].$uuid_parts[6].$uuid_parts[7],
251+
];
252+
253+
return self::logInWith('anonymous', $authData);
254+
}
255+
256+
/**
257+
* Logs in with a service.
258+
*
259+
* @param string $serviceName the name of the service
260+
* @param array $authData the array of auth data for $serviceName
261+
*
262+
* @return ParseUser
263+
*/
264+
public static function logInWith($serviceName, $authData)
265+
{
198266
$data = ['authData' => [
199-
'anonymous' => [
200-
'id' => $uuid_parts[0].$uuid_parts[1].'-'.$uuid_parts[2].'-'.$uuid_parts[3].'-'.$uuid_parts[4].'-'.$uuid_parts[5].$uuid_parts[6].$uuid_parts[7],
201-
],
267+
$serviceName => $authData,
202268
]];
203-
204269
$result = ParseClient::_request('POST', 'users', '', json_encode($data));
205-
$user = new self();
270+
$user = ParseObject::create('_User');
206271
$user->_mergeAfterFetch($result);
207272
$user->handleSaveResult(true);
208273
ParseClient::getStorage()->set('user', $user);
@@ -222,10 +287,15 @@ public static function loginWithAnonymous()
222287
*
223288
* @return ParseUser
224289
*/
225-
public function linkWithFacebook($id, $access_token, $expiration_date = null, $useMasterKey = false)
290+
public function linkWithFacebook(
291+
$id,
292+
$access_token,
293+
$expiration_date = null,
294+
$useMasterKey = false)
226295
{
227296
if (!$this->getObjectId()) {
228-
throw new ParseException('Cannot link an unsaved user, use ParseUser::logInWithFacebook');
297+
throw new ParseException(
298+
'Cannot link an unsaved user, use ParseUser::logInWithFacebook');
229299
}
230300
if (!$id) {
231301
throw new ParseException('Cannot link Facebook user without an id.');
@@ -239,11 +309,89 @@ public function linkWithFacebook($id, $access_token, $expiration_date = null, $u
239309
$expiration_date = new \DateTime();
240310
$expiration_date->setTimestamp(time() + 86400 * 60);
241311
}
312+
$authData = [
313+
'id' => $id,
314+
'access_token' => $access_token,
315+
'expiration_date' => ParseClient::getProperDateFormat($expiration_date),
316+
];
317+
318+
return $this->linkWith('facebook', $authData, $useMasterKey);
319+
}
320+
321+
/**
322+
* Link the user with Twitter details.
323+
*
324+
* @param string $id the Twitter user identifier
325+
* @param string $screen_name the user's Twitter handle
326+
* @param string $consumer_key the application's consumer key
327+
* @param string $consumer_secret the application's consumer secret
328+
* @param string $auth_token the authorized Twitter token for the user
329+
* @param string $auth_token_secret the secret associated with $auth_token
330+
* @param bool $useMasterKey whether to override security
331+
*
332+
* @throws ParseException
333+
*
334+
* @return ParseUser
335+
*/
336+
public function linkWithTwitter(
337+
$id,
338+
$screen_name,
339+
$consumer_key,
340+
$consumer_secret = null,
341+
$auth_token,
342+
$auth_token_secret,
343+
$useMasterKey = false)
344+
{
345+
if (!$this->getObjectId()) {
346+
throw new ParseException('Cannot link an unsaved user, use ParseUser::logInWithTwitter');
347+
}
348+
if (!$id) {
349+
throw new ParseException('Cannot link Twitter user without an id.');
350+
}
351+
if (!$screen_name) {
352+
throw new ParseException(
353+
'Cannot link Twitter user without Twitter screen name.'
354+
);
355+
}
356+
if (!$consumer_key) {
357+
throw new ParseException(
358+
'Cannot link Twitter user without a consumer key.'
359+
);
360+
}
361+
if (!$auth_token) {
362+
throw new ParseException(
363+
'Cannot link Twitter user without an auth token.'
364+
);
365+
}
366+
if (!$auth_token_secret) {
367+
throw new ParseException(
368+
'Cannot link Twitter user without an auth token secret.'
369+
);
370+
}
371+
$authData = [
372+
'id' => $id,
373+
'screen_name' => $screen_name,
374+
'consumer_key' => $consumer_key,
375+
'consumer_secret' => $consumer_secret,
376+
'auth_token' => $auth_token,
377+
'auth_token_secret' => $auth_token_secret,
378+
];
379+
380+
return $this->linkWith('twitter', $authData, $useMasterKey);
381+
}
382+
383+
/**
384+
* Link the user with a service.
385+
*
386+
* @param string $serviceName the name of the service
387+
* @param array $authData the array of auth data for $serviceName
388+
*
389+
* @return ParseUser
390+
*/
391+
public function linkWith($serviceName, $authData, $useMasterKey = false)
392+
{
242393
$data = ['authData' => [
243-
'facebook' => [
244-
'id' => $id, 'access_token' => $access_token,
245-
'expiration_date' => ParseClient::getProperDateFormat($expiration_date),
246-
],
394+
$serviceName => $authData,
247395
]];
248396
$result = ParseClient::_request(
249397
'PUT',

0 commit comments

Comments
 (0)