Skip to content

First pass at adding login/link with Facebook support. #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion src/Parse/ParseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function signUp()
}

/**
* Logs in a and returns a valid ParseUser, or throws if invalid.
* Logs in and returns a valid ParseUser, or throws if invalid.
*
* @param string $username
* @param string $password
Expand Down Expand Up @@ -145,6 +145,87 @@ public static function logIn($username, $password)
return $user;
}

/**
* Logs in with Facebook details, or throws if invalid.
*
* @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
*
* @throws ParseException
*
* @return ParseUser
*/
public static function logInWithFacebook($id, $access_token, $expiration_date = null)
{
if (!$id) {
throw new ParseException("Cannot log in Facebook user without an id.");
}
if (!$access_token) {
throw new ParseException(
"Cannot log in Facebook user without an access token."
);
}
if (!$expiration_date) {
$expiration_date = new DateTime();
$expiration_date->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.
Expand Down