From 5ada3e40bcdd68c7123be3c58d7b8af79a03571b Mon Sep 17 00:00:00 2001 From: Fosco Marotto Date: Mon, 13 Apr 2015 04:26:00 -0700 Subject: [PATCH 1/2] First pass at adding login/link with Facebook support. --- src/Parse/ParseUser.php | 84 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/Parse/ParseUser.php b/src/Parse/ParseUser.php index 6131a7b3..864cf372 100644 --- a/src/Parse/ParseUser.php +++ b/src/Parse/ParseUser.php @@ -1,6 +1,7 @@ setTimestamp(time() + 86400 * 60); + } + $data = ["facebook" => [ + "id" => $id, "access_token" => $access_token, + "expiration_date" => ParseClient::getProperDateFormat($expiration_date) + ]]; + $result = ParseClient::_request("POST", "/1/users", "", $data); + $user = new ParseUser(); + $user->_mergeAfterFetch($result); + $user->handleSaveResult(true); + ParseClient::getStorage()->set("user", $user); + return $user; + } + + /** + * Link the user with Facebook details. + * + * @param string $id the Facebook user identifier + * @param string $access_token the access token for this session + * @param \DateTime $expiration_date defaults to 60 days + * @param boolean $useMasterKey whether to override security + * + * @throws ParseException + * + * @return ParseUser + */ + public function linkWithFacebook($id, $access_token, $expiration_date = null, $useMasterKey = false){ + if (!$this->getObjectId()) { + throw new ParseException("Cannot link an unsaved user, use ParseUser::logInWithFacebook"); + } + if (!$id) { + throw new ParseException("Cannot link Facebook user without an id."); + } + if (!$access_token) { + throw new ParseException( + "Cannot link Facebook user without an access token." + ); + } + if (!$expiration_date) { + $expiration_date = new DateTime(); + $expiration_date->setTimestamp(time() + 86400 * 60); + } + $data = ["authData" => + ["facebook" => [ + "id" => $id, "access_token" => $access_token, + "expiration_date" => ParseClient::getProperDateFormat($expiration_date) + ]] + ]; + $result = ParseClient::_request( + "PUT", "/1/users/" + $this->getObjectId(), + $this->getSessionToken(), $data, $useMasterKey + ); + $user = new ParseUser(); + $user->_mergeAfterFetch($result); + $user->handleSaveResult(true); + return $user; + } + /** * Logs in a user with a session token. Calls the /users/me route and if * valid, creates and returns the current user. From c8aeb9c9a96bb787aba8603dc09794abc2f0b647 Mon Sep 17 00:00:00 2001 From: Fosco Marotto Date: Tue, 14 Apr 2015 18:38:46 -0700 Subject: [PATCH 2/2] Removed wrong use line inserted by editor... --- src/Parse/ParseUser.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Parse/ParseUser.php b/src/Parse/ParseUser.php index 864cf372..6db52344 100644 --- a/src/Parse/ParseUser.php +++ b/src/Parse/ParseUser.php @@ -1,7 +1,6 @@