Skip to content

Commit 5ada3e4

Browse files
author
Fosco Marotto
committed
First pass at adding login/link with Facebook support.
1 parent a192709 commit 5ada3e4

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

src/Parse/ParseUser.php

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace Parse;
4+
use Zend\I18n\Exception\ParseException;
45

56
/**
67
* ParseUser - Representation of a user object stored on Parse.
@@ -116,7 +117,7 @@ public function signUp()
116117
}
117118

118119
/**
119-
* Logs in a and returns a valid ParseUser, or throws if invalid.
120+
* Logs in and returns a valid ParseUser, or throws if invalid.
120121
*
121122
* @param string $username
122123
* @param string $password
@@ -145,6 +146,87 @@ public static function logIn($username, $password)
145146
return $user;
146147
}
147148

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

0 commit comments

Comments
 (0)