News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Redirect user after login

Started by Julian F, December 22, 2015, 08:45:46 AM

Previous topic - Next topic

Julian F

Hello,
How can I make DoLogin() redirect user to custom URL, I use this function to login user from Facebook social login

if ($user_info['is_guest'])
  {
  require_once($sourcedir . '/LogInOut.php');
  $modSettings['cookieTime'] = 3153600;
  DoLogin();
  }


Any help will be appreciated .
Thank you.

Kindred

what is doLogin()?

the SSI login function (in theory) allowed for the redirect... but doLogin is not an SMF function at all -- you would probably have to ask the mod author
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Suki

DoLogin()'s only job its to set up the cookie and check if the user isn't banned.  It has a hardcoded redirectexit() call so you cannot make it to redirect the user wherever you want it.

The easiest way its to just clone that function and remove the parts you don't need, of course thats just a wild guest since I have no idea on what you are trying to achieve.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Julian F

Thank you Suki for your replay,
Can you please tell me witch line from this code  make that redirectexit??

function DoLogin()
{
global $txt, $scripturl, $user_info, $user_settings, $smcFunc;
global $cookiename, $maintenance, $modSettings, $context, $sourcedir;

// Load cookie authentication stuff.
require_once($sourcedir . '/Subs-Auth.php');

// Call login integration functions.
call_integration_hook('integrate_login', array($user_settings['member_name'], isset($_POST['hash_passwrd']) && strlen($_POST['hash_passwrd']) == 40 ? $_POST['hash_passwrd'] : null, $modSettings['cookieTime']));

// Get ready to set the cookie...
$username = $user_settings['member_name'];
$user_info['id'] = $user_settings['id_member'];

// Bam!  Cookie set.  A session too, just in case.
setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], sha1($user_settings['passwd'] . $user_settings['password_salt']));

// Reset the login threshold.
if (isset($_SESSION['failed_login']))
unset($_SESSION['failed_login']);

$user_info['is_guest'] = false;
$user_settings['additional_groups'] = explode(',', $user_settings['additional_groups']);
$user_info['is_admin'] = $user_settings['id_group'] == 1 || in_array(1, $user_settings['additional_groups']);

// Are you banned?
is_not_banned(true);

// An administrator, set up the login so they don't have to type it again.
if ($user_info['is_admin'] && isset($user_settings['openid_uri']) && empty($user_settings['openid_uri']))
{
$_SESSION['admin_time'] = time();
unset($_SESSION['just_registered']);
}

// Don't stick the language or theme after this point.
unset($_SESSION['language'], $_SESSION['id_theme']);

// First login?
$request = $smcFunc['db_query']('', '
SELECT last_login
FROM {db_prefix}members
WHERE id_member = {int:id_member}
AND last_login = 0',
array(
'id_member' => $user_info['id'],
)
);
if ($smcFunc['db_num_rows']($request) == 1)
$_SESSION['first_login'] = true;
else
unset($_SESSION['first_login']);
$smcFunc['db_free_result']($request);

// You've logged in, haven't you?
updateMemberData($user_info['id'], array('last_login' => time(), 'member_ip' => $user_info['ip'], 'member_ip2' => $_SERVER['BAN_CHECK_IP']));

// Get rid of the online entry for that old guest....
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}log_online
WHERE session = {string:session}',
array(
'session' => 'ip' . $user_info['ip'],
)
);
$_SESSION['log_time'] = 0;

// Just log you back out if it's in maintenance mode and you AREN'T an admin.
if (empty($maintenance) || allowedTo('admin_forum'))
redirectexit('action=login2;sa=check;member=' . $user_info['id'], $context['server']['needs_login_fix']);
else
redirectexit('action=logout;' . $context['session_var'] . '=' . $context['session_id'], $context['server']['needs_login_fix']);

}


What i want to do is to make this mod  http://www.smfpacks.com/sociallogin/ [nofollow] to redirect after login to custom link stored in session.

Kindred

Do note what Suki said:

Quote from: Suki on December 22, 2015, 11:48:49 AMsince I have no idea on what you are trying to achieve.


Why don't you provide some further detail?
What, specifically, are you trying to accomplish?
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Julian F

I have provide more details in my previews replay. Check it please.

Sir Osis of Liver


/Sources/LogInOut.php



// Set the login_url if it's not already set (but careful not to send us to an attachment).
//if (empty($_SESSION['login_url']) && isset($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'dlattach') === false && preg_match('~(board|topic)[=,]~', $_SESSION['old_url']) != 0)
// $_SESSION['login_url'] = $_SESSION['old_url'];


$_SESSION['login_url'] ='http://simplemachines.org';

// Been guessing a lot, haven't we?


Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Julian F

Quote from: Sir Osis of Liver on December 22, 2015, 02:46:19 PM

/Sources/LogInOut.php



// Set the login_url if it's not already set (but careful not to send us to an attachment).
//if (empty($_SESSION['login_url']) && isset($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'dlattach') === false && preg_match('~(board|topic)[=,]~', $_SESSION['old_url']) != 0)
// $_SESSION['login_url'] = $_SESSION['old_url'];


$_SESSION['login_url'] ='http://simplemachines.org';

// Been guessing a lot, haven't we?




Thank you for your replay. I have already did that and is not working.

Here is the login function for facebook

function FacebookLogin()
{
global $sourcedir, $scripturl, $smcFunc, $user_settings, $modSettings, $user_info;

$manual_sync = isset($_GET['manualsync']);

// init app with app id and secret
FacebookSession::setDefaultApplication($modSettings['sign_facebook_app_id'], $modSettings['sign_facebook_app_secret']);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper($scripturl . '?action=facebook' . ($manual_sync ? '&manualsync' : ''));

try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}

// see if we have a session
if ( isset( $session ) )
{
  // graph api request for user data
  $request = new FacebookRequest($session, 'GET', '/me' );
  $response = $request->execute();
 
  // get response
  $graphObject = $response->getGraphObject();
  $_SESSION['FB'] = array();
  $_SESSION['FB']['ID'] = $graphObject->getProperty('id');           
      $_SESSION['FB']['FULLNAME'] = $graphObject->getProperty('name');
      $_SESSION['FB']['EMAIL'] =  $graphObject->getProperty('email');
 
  if ($graphObject->getProperty('gender') != null && trim($graphObject->getProperty('gender')) != '' && $graphObject->getProperty('gender') == 'male')
  $_SESSION['FB']['GENDER'] = '1';
  else if ($graphObject->getProperty('gender') != null && trim($graphObject->getProperty('gender')) != '' && $graphObject->getProperty('gender') == 'female')
  $_SESSION['FB']['GENDER'] = '2';
 
  if ($graphObject->getProperty('birthday') != null && trim($graphObject->getProperty('birthday')) != '' && $graphObject->getProperty('gender') == 'male')
  $_SESSION['FB']['BIRTHDAY'] = strftime('%Y-%m-%d', strtotime($graphObject->getProperty('birthday')));
 
  // Manual sync?
  if (!$user_info['is_guest'] && !empty($manual_sync))
  {
  // Maybe sync the last status update
  // !!! NOT IMPLEMENTED
  /*if (in_array('status', $_SESSION['facebook_preferences']))
  {
  $request = new FacebookRequest($session, 'GET', '/me/feed' );
  $response = $request->execute();
  $graphObject = $response->getGraphObject();
 
  print_r($graphObject);
  exit;
  }*/
 
 
  $fields = array();
 
  if (in_array('birthday', $_SESSION['facebook_preferences']) && isset($_SESSION['FB']['BIRTHDAY']))
  {
  $fields['birthdate'] = $_SESSION['FB']['BIRTHDAY'];
  }
 
  if (in_array('gender', $_SESSION['facebook_preferences']) && isset($_SESSION['FB']['GENDER']))
  {
  $fields['gender'] = $_SESSION['FB']['GENDER'];
  }
 
  if (!empty($fields))
  updateMemberData($user_info['id'], $fields);
 
  // Redirect!
  redirectexit('action=profile;area=external_accounts');
  }
 
  // Browse the DB
  $request = $smcFunc['db_query']('', '
  SELECT passwd, id_member, id_group, lngfile, is_activated, email_address, additional_groups, member_name, password_salt,
  openid_uri, passwd_flood
  FROM {db_prefix}members
  WHERE facebook_id = {string:fbid}
  LIMIT 1',
array(
'fbid' => $_SESSION['FB']['ID'],
)
  );
  $result = $smcFunc['db_num_rows']($request);
  $user_settings = $smcFunc['db_fetch_assoc']($request);
  $smcFunc['db_free_result']($request);
 
  // Just connecting!
  if (!$user_info['is_guest'] && $result == 0)
  {
  // But first! Mark the id!
updateMemberData($user_info['id'], array(
'facebook_id' => $_SESSION['FB']['ID'],
));

redirectexit('action=profile;area=external_accounts');
  }
  // Register then...
  elseif ($result == 0)
  {
  FacebookRegistration();
  }
  // Login there!
  elseif ($user_info['is_guest'])
  {
  require_once($sourcedir . '/LogInOut.php');
  $modSettings['cookieTime'] = 3153600;
  DoLogin();
  }
  // There is another guy like you!
  else
  fatal_lang_error('external_error_account', false);
}
// Redirect to Facebook
else
{
  $loginUrl = $helper->getLoginUrl(
  array('scope' => 'email')
  );
  header("Location: " . $loginUrl);
  exit;
}
}

Sir Osis of Liver

Am not familiar with Facebook, and not sure exactly what you're trying to do, but looks like at the end of this function user is redirected to Facebook.  If that's what you want to change, it's here -



// Redirect to Facebook
else
{
  $loginUrl = $helper->getLoginUrl(
  array('scope' => 'email')
  );
  header("Location: http://www.simplemachines.org");
  exit;
}


Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Julian F

The part I want to change is this

elseif ($user_info['is_guest'])
  {
  require_once($sourcedir . '/LogInOut.php');
  $modSettings['cookieTime'] = 3153600;
  DoLogin();
  }

In this part of the code user from Facebook is log in the forum using DoLogin() function.

Sir Osis of Liver

Changing $_SESSION['login_url'] should work.  DoLogin() returns to Login2(), which redirects to $_SESSION['login_url'] when login is successful.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Julian F

Quote from: Sir Osis of Liver on December 22, 2015, 03:59:08 PM
Changing $_SESSION['login_url'] should work.  DoLogin() returns to Login2(), which redirects to $_SESSION['login_url'] when login is successful.


So each time when DoLogin() is call will go in Login2()?  Because I have change all the location where $_SESSION['login_url'] get new value and is not working... still going to forum index page. This is very strange.

Sir Osis of Liver

Have you tried setting $_SESSION['login_url'] in DoLogin()?
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Julian F

Quote from: Sir Osis of Liver on December 23, 2015, 12:23:22 PM
Have you tried setting $_SESSION['login_url'] in DoLogin()?

No, where do I need to add that code in DoLogin()?

Sir Osis of Liver

DoLogin() is called from FacebookLogin(), it then goes to Login2(), which completes the login process and redirects the user to $_SESSION['login_url'].  All you're doing is assigning a value to $_SESSION['login_url'], you need to place it somewhere where it's not changed later in the login sequence.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Julian F

Hello,
I have search all the SMF project for $_SESSION['login_url'] and change it with custom link but seams when I login using social login is going to index page of the forum. If I use normal login with user and password all working fine.
Happy New Year!

Julian F

#17
Quote from: Sir Osis of Liver on December 23, 2015, 07:41:33 PM
DoLogin() is called from FacebookLogin(), it then goes to Login2(), which completes the login process and redirects the user to $_SESSION['login_url'].  All you're doing is assigning a value to $_SESSION['login_url'], you need to place it somewhere where it's not changed later in the login sequence.

Hello,
I have found the solution. It was very simple. Thank you.

rockmanx4

it is redirect 301 ? yes or no

Sir Osis of Liver

Redirect 301 is used in .htaccess to redirect from one url to another, and has nothing to do with this discussion.  Please open a new thread and provide details for what you're trying to do.
Ashes and diamonds, foe and friend,
 we were all equal in the end.

                                     - R. Waters

Advertisement: