Skip to content

Commit

Permalink
facebook and google login mod
Browse files Browse the repository at this point in the history
  • Loading branch information
hostbrook committed Jan 28, 2023
1 parent ab9736e commit b3c1372
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 267 deletions.
283 changes: 181 additions & 102 deletions App/Controllers/WebApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace App\Controllers;

use \Videna\Core\Router;
use \Videna\Core\Config;
use \Videna\Models\Users;
use \Videna\Core\User;
use \Videna\Core\View;
Expand All @@ -36,143 +37,221 @@ public function actionPrivacyPolicy()


/**
* Check if user exists in Database
* Check if user with Google ID exists in Database
* - if exists - login user
* - if doesn't exist - add user (if permission given)
* @return void
*/
public function actionCheckAccount()
public function actionGoogleAccount($createAccount = false)
{
$result = Users::get(['email' => Router::get('email')], 1);
if ($result !== false) $result = true;
$params = array(
'client_id' => Config::get('google')['client_id'],
'client_secret' => Config::get('google')['client_secret'],
'redirect_uri' => Config::get('google')['redirect_uri'],
'grant_type' => 'authorization_code'
);

// Put in 'email_exists' the result: `true` if user already exists in DB
View::set([
'email_exists' => $result
]);
}
if ($createAccount == false) {
// By Javascript we got a CODE from GIS.
// Now, using CODE we have to get an access token
$params['code'] = Router::get('code');

$url = 'https://accounts.google.com/o/oauth2/token';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($params)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);

$tokenInfo = json_decode($result, true);

if (isset($tokenInfo['access_token'])) {
$accessToken = $tokenInfo['access_token'];
}
else {
View::set(['result' => 'Can not get access token from GIS']);
return;
}

}
else $accessToken = Router::get('accessToken');

// Using access token we can get info about the user
$params['access_token'] = $accessToken;
$userInfo = json_decode(file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo' . '?' . urldecode(http_build_query($params))), true);

// Check if all required data has been received frim FB:
if (isset($userInfo['id']) &&
isset($userInfo['given_name']) &&
isset($userInfo['family_name']) &&
isset($userInfo['email'])) {

// Check if Google ID is linked with any user in DB:
$user = Users::get(['google_id' => $userInfo['id']], 1);
if ($user !== false) {
// User with Google ID exists in DB. Login the user:
User::login($user['id']);
View::set(['result' => 'user logined']);
}
else {
// Check if Google email is linked with any user in DB:
$user = Users::get(['email' => $userInfo['email']], 1);
if ($user !== false) {
// User with Google email exists in DB. Add Google ID and login the user:
$users = DB::update(
'UPDATE users SET google_id = :google_id WHERE id = :id',
['id' => $user['id'], 'google_id' => $userInfo['id']]
);
User::login($user['id']);
View::set(['result' => 'user logined']);
}
else {
// User doesn't exist in DB. If flag 'createAccount' - add user to DB

if ($createAccount) {
// Create new account
$userID = Users::add([
'name' => $userInfo['given_name'],
'last_name' => $userInfo['family_name'],
'email' => $userInfo['email'],
'lang' => Lang::$code,
'account' => USR_REG,
'google_id' => $userInfo['id']
]);

// Login new user
User::login($userID);

View::set(['result' => 'user logined']);
}
else {
View::set([
'result' => 'user not exist',
'first_name' => $userInfo['given_name'],
'email' => $userInfo['email'],
'accessToken' => $accessToken
]);
}
}
}
}
else {
// User data from GIS wasn't provided or incomplete
View::set(['result' => 'Error occurs during the getting user data from GIS']);
}
}

/**
* Check if user exists in Database
* - if exist - login user
* - if doesn't exist - add user
* Check if user with FB ID exists in Database
* - if exists - login user
* - if doesn't exist - add user (if permission given)
* @return void
*/
public function actionCheckAccountFB($createAccount = false)
public function actionFacebookAccount($createAccount = false)
{
if ( Router::get('accessToken') != null) {
// Access tokecn has been receved from Facebook

// Prepare and send GET request to FB to get user profile data:
$params = array(
'access_token' => Router::get('accessToken'),
'fields' => 'first_name,last_name,email'
);
$userInfo = json_decode(file_get_contents('https://graph.facebook.com/me' . '?' . urldecode(http_build_query($params))), true);

// Check if all required data has been received frim FB:
if (isset($userInfo['id']) &&
isset($userInfo['first_name']) &&
isset($userInfo['last_name']) &&
isset($userInfo['email'])) {

// Check if Facebook ID is linked with any user in DB:
$user = Users::get(['facebook_id' => $userInfo['id']], 1);
// Prepare and send GET request to FB to get user profile data:
$params = array(
'access_token' => Router::get('accessToken'),
'fields' => 'first_name,last_name,email'
);
$userInfo = json_decode(file_get_contents('https://graph.facebook.com/me' . '?' . urldecode(http_build_query($params))), true);

// Check if all required data has been received frim FB:
if (isset($userInfo['id']) &&
isset($userInfo['first_name']) &&
isset($userInfo['last_name']) &&
isset($userInfo['email'])) {

// Check if Facebook ID is linked with any user in DB:
$user = Users::get(['facebook_id' => $userInfo['id']], 1);
if ($user !== false) {
// User with Facebook ID exists in DB. Login the user:
User::login($user['id']);
View::set(['result' => 'user logined']);
}
else {
// Check if Facebook email is linked with any user in DB:
$user = Users::get(['email' => $userInfo['email']], 1);
if ($user !== false) {
// User with Facebook ID exists in DB. Login the user:
// User with Facebook email exists in DB. Add Facebook ID and login the user:
$users = DB::update(
'UPDATE users SET facebook_id = :facebook_id WHERE id = :id',
['id' => $user['id'], 'facebook_id' => $userInfo['id']]
);
User::login($user['id']);
View::set(['result' => 'user logined']);
}
else {
// Check if Facebook email is linked with any user in DB:
$user = Users::get(['email' => $userInfo['email']], 1);
if ($user !== false) {
// User with Facebook email exists in DB. Add Facebook ID and login the user:
$users = DB::update(
'UPDATE users SET facebook_id = :facebook_id WHERE id = :id',
['id' => $user['id'], 'facebook_id' => $userInfo['id']]
);
User::login($user['id']);
// User doesn't exist in DB. If flag 'createAccount' - add user to DB
if ($createAccount) {
// Create new account
$userID = Users::add([
'name' => $userInfo['first_name'],
'last_name' => $userInfo['last_name'],
'email' => $userInfo['email'],
'lang' => Lang::$code,
'account' => USR_REG,
'facebook_id' => $userInfo['id']
]);

// Login new user
User::login($userID);

View::set(['result' => 'user logined']);
}
else {
// User doesn't exist in DB. If flag 'createAccount' - add user to DB
if ($createAccount) {
// Create new account
$userID = Users::add([
'name' => $userInfo['first_name'],
'last_name' => $userInfo['last_name'],
'email' => $userInfo['email'],
'lang' => Lang::$code,
'account' => USR_REG,
'facebook_id' => $userInfo['id']
]);

// Login new user
User::login($userID);

View::set(['result' => 'user logined']);
}
else {
View::set([
'result' => 'user not exist',
'first_name' => $userInfo['first_name'],
'email' => $userInfo['email']
]);
}

}
}
}
else {
// User data from Facebook wasn't provided or incomplete
View::set(['result' => 'Error occurs during the getting user data from Facebook']);
}
View::set([
'result' => 'user not exist',
'first_name' => $userInfo['first_name'],
'email' => $userInfo['email']
]);
}

}
}
}
else {
// Access tokecn has NOT been receved from Facebook
View::set(['result' => 'FP Access Token doesn\'t exist']);
// User data from Facebook wasn't provided or incomplete
View::set(['result' => 'Error occurs during the getting user data from Facebook']);
}

}


public function actionCreateAccount()
{
self::actionCheckAccountFB(true);
}


/**
* Login the existing user by email or
* create a new account and login new user if doesn't exist
*
* Create account by user permission
* @return void
*/
public function actionSocialLogin()
public function actionCreateAccount()
{
$user = Users::get(['email' => Router::get('email')], 1);
if (!$user) {

// Create new account
$userID = Users::add([
'name' => Router::get('name'),
'last_name' => Router::get('last_name'),
'email' => Router::get('email'),
'lang' => Lang::$code,
'account' => USR_REG
]);

// Login new user
User::login($userID);

if ( Router::get('network') == null || Router::get('accessToken') == null) {
Router::$action = 'Error';
Router::$statusCode = 403;
return;
}
// Login user if it already exists in DB:
else User::login($user['id']);

switch (Router::get('network')) {
case 'facebook':
self::actionFacebookAccount(true);
break;
case 'google':
self::actionGoogleAccount(true);
break;
default:
Router::$action = 'Error';
Router::$statusCode = 403;
}

}


/**
* Delete account of the existing user
*
* @return void
*/
public function actionDeleteAccount()
Expand Down
44 changes: 40 additions & 4 deletions App/Views/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,50 @@
<h1 class="uk-heading-primary animate uk-invisible" style="font-weight: 700;">Videna <?= $_['login'] ?></h1>

<div class="uk-margin-medium-top animate uk-invisible" data-uk-margin data-uk-scrollspy-class="uk-animation-fade uk-invisible">
<button id="google-login" class="uk-button uk-button-default uk-width-2-3 uk-width-auto@s" style="padding:0" title="nue with Google"><span uk-icon="google"></span> Google</button>
<button id="facebook-login" class="uk-button uk-button-default uk-width-2-3 uk-width-auto@s" style="height:42px" title="Continue with Facebook"><span uk-icon="facebook"></span> Facebook</button>

<!-- Regular buttons: -->
<!--<button id="google-login" class="uk-button uk-button-default uk-width-2-3 uk-width-auto@s" disabled title="Continue with Google"><span uk-icon="google"></span> Google</button>
<button id="facebook-login" class="uk-button uk-button-default uk-width-2-3 uk-width-auto@s" disabled title="Continue with Facebook"><span uk-icon="facebook"></span> Facebook</button>-->

<!-- Social buttons with names: -->
<button id="google-login" class="uk-button uk-button-default" style="padding:0;z-index:10;" title="Continue with Google">
<div id="google-login-bkg" style="z-index:-1;position:relative;"></div>
</button>
<br>
<!-- https://developers.facebook.com/docs/facebook-login/web/login-button/ -->
<button id="facebook-login" class="uk-button uk-button-default" style="padding:0;" title="Continue with Facebook">
<div
class="fb-login-button"
data-max-rows="1"
data-size="large"
data-button-type="continue_with"
data-use-continue-as="true"
style="z-index:-1;position:relative;">
</div>
</button>

</div>

</div>
<!-- /CONTENT -->

<script src="https://accounts.google.com/gsi/client" ></script>
<script src="/js/videna-social.js?ver=23012403" ></script> <!-- ?ver=<?= rand(1, 999999) ?> -->
<script src="https://accounts.google.com/gsi/client"></script>
<script>
const config = {
"google": {
"client_id": "<?= $config['google']['client_id'] ?>"
},
"facebook": {
"appId": "<?= $config['facebook']['appId'] ?>",
"appVersion": "<?= $config['facebook']['appVersion'] ?>",
},
"lang": {
"redirection_to_dashboard": "<?= $_['redirection to dashboard'] ?>",
"creating_an_account": "<?= $_['creating an account'] ?>",
"account_not_found": "<?= $_['account not found'] ?>"
}
}
</script>
<script src="/js/videna-social.js" ></script> <!-- ?ver=<?= rand(1, 999999) ?> -->

<?php include PATH_VIEWS . 'inc/footer.php' ?>
Loading

0 comments on commit b3c1372

Please sign in to comment.