Facebook login implemented

This commit is contained in:
Joey Lai
2017-01-31 16:12:22 +01:00
parent 08f668859c
commit 8e98001217
82 changed files with 11126 additions and 133 deletions

View File

@@ -0,0 +1,71 @@
<?php
$fb = new Facebook\Facebook([
'app_id' => $appID, // Replace {app-id} with your app id
'app_secret' => $appSecret,
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId($appID); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');

View File

@@ -0,0 +1,66 @@
<?php
try{
$fbUsername = str_replace(' ', '', test_input(($_POST["fbUsername"])));
checkInputChoice($fbUsername, "fbUsername");
} catch(usernameException $e){
$fbCorrect = false;
$fbUsernameErr = $e->getMessage();
}
try{
$fbPassword = str_replace(' ', '', test_input(($_POST["fbPassword"])));
checkInputChoice($fbPassword, "longerEight");
matchfbPassword();
} catch(passwordException $e){
$fbCorrect = false;
$fbPasswordErr = $e->getMessage();
} catch(fbConfirmPasswordException $e){
$fbCorrect = false;
$fbConfirmpasswordErr = $e->getMessage();
}
try{
$fbName = test_input(($_POST["fbName"]));
checkInputChoice($fbName, "lettersAndSpaces");
} catch(lettersAndSpacesException $e){
$fbCorrect = false;
}
try {
$fbSurname = test_input(($_POST["fbSurname"]));
checkInputChoice($fbSurname, "lettersAndSpaces");
}
catch(lettersAndSpacesException $e){
$fbCorrect = false;
}
try {
$fbDay_date = test_input(($_POST["fbDay_date"]));
$fbMonth_date = test_input(($_POST["fbMonth_date"]));
$fbYear_date = test_input(($_POST["fbYear_date"]));
$fbBday = $fbYear_date . "-" . $fbMonth_date . "-" . $fbDay_date;
checkInputChoice($fbBday, "bday");
} catch (bdayException $e) {
$fbBdayErr = $e->getMessage();
$fbCorrect = false;
}
try{
$fbEmail = test_input(($_POST["fbEmail"]));
checkInputChoice($fbEmail, "fbEmail");
} catch(emailException $e){
$fbCorrect = false;
$fbEmailErr = $e->getMessage();
}
try {
fbRegisterCheck($fbCorrect);
} catch(registerException $e){
echo "<script>
window.onload = function() {
$('#fbModal').show();
}
</script>";
$fbRegisterErr = $e->getMessage();
}

View File

@@ -1,5 +1,5 @@
function checkLoggedIn() {
if (confirm("U bent al ingelogd!!\nWilt u uitloggen?\nKlik ok om uit te loggen.") == true) {
if (confirm("U bent al ingelogd!\nWilt u uitloggen?\nKlik ok om uit te loggen.") == true) {
window.location.href = "logout.php";
} else {
window.location.href = "profile.php";
@@ -10,6 +10,10 @@ function bannedAlert(){
alert("Uw account is geband!");
}
function frozenAlert(){
alert("Uw account is bevroren!\n");
}
function emailNotConfirmed(){
alert("Uw account is nog niet bevestigd!\nEr is een nieuwe email gestuurd om uw account te bevestigen");
}

View File

@@ -8,96 +8,43 @@
include_once("../queries/emailconfirm.php");
include_once("../queries/requestpassword.php");
include_once("../queries/register.php");
require_once("../queries/Facebook/autoload.php");
?>
<body>
<script>
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
window.fbAsyncInit = function() {
FB.init({
appId : '353857824997532',
cookie : true,
xfbml : true,
version : 'v2.8'
});
FB.AppEvents.logPageView();
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
function fbLogout() {
FB.logout(function (response) {
//Do what ever you want here when logged out like reloading the page
window.location.reload();
});
}
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Here we run a very simple test of the Graph API after login is
// successful. See statusChangeCallback() for when this call is made.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + +'!';
// alert("You are logged in with facebook");
});
}
</script>
<?php
session_start();
// Checks if there's an user already logged in
if(isset($_SESSION["userID"])){
echo "<script>
window.onload=checkLoggedIn();
</script>";
}
// define variables and set to empty values
// Facebook variables
$appID = "353857824997532";
$appSecret = "db47e91ffbfd355fdd11b4b65eade851";
$fbUsername = $fbPassword = $fbConfirmpassword = "";
$fbUsernameErr = $fbPasswordErr = $fbConfirmpasswordErr = $fbEmailErr = $fbBdayErr = "";
$fbCorrect = true;
$fbName = $fbSurname = $fbBday = $fbEmail = "";
$bdayExist = false;
// Register variables
$name = $surname = $bday = $username = $password = $confirmpassword = $location = $housenumber = $email = $confirmEmail = $captcha = $ip = "";
$genericErr = $nameErr = $surnameErr = $bdayErr = $usernameErr = $passwordErr = $confirmpasswordErr = $locationErr = $housenumberErr = $emailErr = $confirmEmailErr = $captchaErr = "";
$correct = true;
$day_date = "dag";
$month_date = "maand";
$year_date = "jaar";
// Define variables and set to empty values
$day_date = $month_date = $year_date = "";
$fbDay_date = $fbMonth_date = $fbYear_date = "";
// Login variables
$user = $psw = $remember ="";
$loginErr = $resetErr ="";
$loginErr = $resetErr = $fbRegisterErr ="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Checks for which button is pressed
switch ($_POST["submit"]) {
case "login":
try {
@@ -122,6 +69,75 @@
break;
case "register":
include("register.php");
break;
case "fbRegister":
include("fbRegister.php");
break;
}
}
$fb = new Facebook\Facebook([
'app_id' => $appID,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.2',
]);
$redirect = "https://myhyvesbookplus.nl/~joey/login.php";
$helper = $fb->getRedirectLoginHelper();
try {
// Returns a `Facebook\FacebookResponse` object
$acces_token = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if(!isset($acces_token)){
$permission=["email", "user_birthday"];
$loginurl=$helper->getLoginUrl($redirect,$permission);
}else {
$fb->setDefaultAccessToken($acces_token);
$response = $fb->get('/me?fields=email,name,birthday');
$usernode = $response->getGraphUser();
// echo $usernode->getName() . "</br>";
// echo $usernode->getId() . "</br>";
// echo $usernode->getProperty("email") . "<br><br>";
// echo "Picture<br>";
// echo "<img src='$image' /><br><br>";
$nameSplit = explode(" ", $usernode->getName());
$fbName = $nameSplit[0];
$fbSurname = $nameSplit[1];
$fbEmail = $usernode->getProperty("email");
$image = 'https://graph.facebook.com/' . $usernode->getId() . '/picture?width=200';
if (fbLogin($fbEmail) == 1) {
$fbUserID = getfbUserID($fbEmail)["userID"];
$fbRole = getfbUserID($fbEmail)["role"];
if($fbRole == "banned"){
echo "<script>
window.onload=bannedAlert();
</script>";
} else if($fbRole == "frozen"){
$_SESSION["userID"] = $fbUserID;
echo "<script>
window.onload=frozenAlert();
window.location.href= 'profile.php';
</script>";
} else {
$_SESSION["userID"] = $fbUserID;
header("location: profile.php");
}
} else {
echo "<script>
window.onload = function() {
$('#fbModal').show();
}
</script>";
}
}
/* This view adds login view */

View File

@@ -50,7 +50,7 @@
try{
$location = test_input(($_POST["location"]));
checkInputChoice($location, "lettersAndSpaces");
checkInputChoice($location, "");
} catch(lettersAndSpacesException $e){
$correct = false;
$locationErr = $e->getMessage();
@@ -80,12 +80,12 @@
try {
getIp();
registerCheck($correct);
sendConfirmEmailUsername($username);
sendConfirmEmail(getUserID()["userID"]);
} catch(registerException $e){
echo "<script>
window.onload = function() {
$('#registerModal').show();
}
</script>";
window.onload = function() {
$('#registerModal').show();
}
</script>";
$genericErr = $e->getMessage();
}

View File

@@ -10,6 +10,19 @@ a.button {
}
a.fbButton {
background-color: #3B5998;
border-radius: 5px;
color: black;
cursor: pointer;
padding: 8px 20px;
font-family: Arial;
font-size: 22px;
color: white;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
/* Body */
body {
height: 100%;
@@ -28,7 +41,7 @@ body {
form {
/*background-color: #a87a87;*/
border-radius: 12px;
height: 85%;
height: 70%;
margin: auto;
width: 600px;
overflow-y: auto;
@@ -133,7 +146,7 @@ label {
background-repeat: repeat-x;
background-attachment: fixed;*/
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
height: 400px;
height: 375px;
margin: 16px auto;
overflow-y: auto;
padding: 20px;
@@ -154,7 +167,7 @@ ul {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 30px; /* Location of the box */
padding-top: 75px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
@@ -216,6 +229,7 @@ ul {
}
.modal-footer {
padding: 2px 8px;
background-color: #FBC02D;
color: black;
}