Skip to content

Commit 6b61d66

Browse files
committed
Merge pull request #90 from ParsePlatform/gFosco.user-linking
First pass at adding login/link with Facebook support.
2 parents 00d40dd + c8aeb9c commit 6b61d66

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

src/Parse/ParseUser.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function signUp()
116116
}
117117

118118
/**
119-
* Logs in a and returns a valid ParseUser, or throws if invalid.
119+
* Logs in and returns a valid ParseUser, or throws if invalid.
120120
*
121121
* @param string $username
122122
* @param string $password
@@ -145,6 +145,87 @@ public static function logIn($username, $password)
145145
return $user;
146146
}
147147

148+
/**
149+
* Logs in with Facebook details, or throws if invalid.
150+
*
151+
* @param string $id the Facebook user identifier
152+
* @param string $access_token the access token for this session
153+
* @param \DateTime $expiration_date defaults to 60 days
154+
*
155+
* @throws ParseException
156+
*
157+
* @return ParseUser
158+
*/
159+
public static function logInWithFacebook($id, $access_token, $expiration_date = null)
160+
{
161+
if (!$id) {
162+
throw new ParseException("Cannot log in Facebook user without an id.");
163+
}
164+
if (!$access_token) {
165+
throw new ParseException(
166+
"Cannot log in Facebook user without an access token."
167+
);
168+
}
169+
if (!$expiration_date) {
170+
$expiration_date = new DateTime();
171+
$expiration_date->setTimestamp(time() + 86400 * 60);
172+
}
173+
$data = ["facebook" => [
174+
"id" => $id, "access_token" => $access_token,
175+
"expiration_date" => ParseClient::getProperDateFormat($expiration_date)
176+
]];
177+
$result = ParseClient::_request("POST", "/1/users", "", $data);
178+
$user = new ParseUser();
179+
$user->_mergeAfterFetch($result);
180+
$user->handleSaveResult(true);
181+
ParseClient::getStorage()->set("user", $user);
182+
return $user;
183+
}
184+
185+
/**
186+
* Link the user with Facebook details.
187+
*
188+
* @param string $id the Facebook user identifier
189+
* @param string $access_token the access token for this session
190+
* @param \DateTime $expiration_date defaults to 60 days
191+
* @param boolean $useMasterKey whether to override security
192+
*
193+
* @throws ParseException
194+
*
195+
* @return ParseUser
196+
*/
197+
public function linkWithFacebook($id, $access_token, $expiration_date = null, $useMasterKey = false){
198+
if (!$this->getObjectId()) {
199+
throw new ParseException("Cannot link an unsaved user, use ParseUser::logInWithFacebook");
200+
}
201+
if (!$id) {
202+
throw new ParseException("Cannot link Facebook user without an id.");
203+
}
204+
if (!$access_token) {
205+
throw new ParseException(
206+
"Cannot link Facebook user without an access token."
207+
);
208+
}
209+
if (!$expiration_date) {
210+
$expiration_date = new DateTime();
211+
$expiration_date->setTimestamp(time() + 86400 * 60);
212+
}
213+
$data = ["authData" =>
214+
["facebook" => [
215+
"id" => $id, "access_token" => $access_token,
216+
"expiration_date" => ParseClient::getProperDateFormat($expiration_date)
217+
]]
218+
];
219+
$result = ParseClient::_request(
220+
"PUT", "/1/users/" + $this->getObjectId(),
221+
$this->getSessionToken(), $data, $useMasterKey
222+
);
223+
$user = new ParseUser();
224+
$user->_mergeAfterFetch($result);
225+
$user->handleSaveResult(true);
226+
return $user;
227+
}
228+
148229
/**
149230
* Logs in a user with a session token. Calls the /users/me route and if
150231
* valid, creates and returns the current user.

0 commit comments

Comments
 (0)