magento got invalid login or password error for customer - magento

i added one customer programmatically and send mail to that customer for new password and also in admin side i found that customer but when customer try to login with email id and password got error 'Invalid login or password'
here is my code
what is problem with this?
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::init();
Mage::setIsDeveloperMode(true);
ini_set('memory_limit', '-1'); //for unlimited memory being more trickey
ini_set('display_errors', 1);
Varien_Profiler::enable();
umask(0);
Mage::app('default');
$customer_email = 'test#testemail.com'; // email adress that will pass by the questionaire
$customer_fname = 'test_firstname'; // we can set a tempory firstname here
$customer_lname = 'test_lastname'; // we can set a tempory lastname here
$passwordLength = 10; // the lenght of autogenerated password
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email);
/*
* Check if the email exist on the system.
* If YES, it will not create a user account.
*/
if(!$customer->getId()) {
//setting data such as email, firstname, lastname, and password
$customer->setEmail($customer_email);
$customer->setFirstname($customer_fname);
$customer->setLastname($customer_lname);
$customer->setPassword($customer->generatePassword($passwordLength));
}
try{
//the save the data and send the new account email.
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customer->sendNewAccountEmail();
}
catch(Exception $ex){
}
Advance thanks....

I would guess it's not automatically setting the customer to active. You can confirm by running this query on your db;
SELECT * FROM customer_entity WHERE email = 'the#email.com'
There is a column in there called 'is_active', almost certainly set to 0 for no.
In your script to create the customer add this before you save;
$customer->setIsActive(1);

Related

Joomla 3 get profile

I use function onUserAfterSave and $isnew, but I can't get phone and address of user registered in Joomla 3.X. I only can get name, username, email, but no data from profile.
To access the user profile data you need to use JUserHelper class
jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$userProfile = JUserHelper::getProfile( $user->id ); //Get userprofile data
//You can get all the user input of profile data. If you want to get country of user use
echo "Country :" . $userProfile->profile['country'];
Amit Ray,
I use in my plugin:
public function onUserAfterSave($user, $isnew, $success, $msg)
{
//die(print_r($user));
if ($isnew){
.............
$pluginParams = new JRegistry($plugin->params);
$name = $user['name'];
$name_usuario = $user['username'];
$email_user = $user['email'];
$id_usuario = $user['id'];
jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$userProfile = JUserHelper::getProfile( $id_usuario );
//You can get all the user input of profile data. If you want to get country of user use
$user_phone = $userProfile->profile['phone'];
$user_address = $userProfile->profile['address1'];
But $user_phone and $user_address no have data... whats is error? Thanks

Add user to group in Joomla not working

I am trying to add a user to a group. I can run this PHP code without any errors, but the user group is still not changed.
<?php
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );
$userId = 358;
$groupId = 11;
echo JUserHelper::addUserToGroup($userId, $groupId);
?>
I run in the same issue with payment callback. I found that user groups save in the database correctly, but are not refreshed in Juser object (becouse You add user to group in different session). When user interacts on a page groups are restored.
Another think that i found is that changing groups in administrator panel works the same way if user is logged in.
To deal with it I made system plugin and in onAfterInitialise function i do:
//get user
$me = JFactory::getUser();
//check if user is logged in
if($me->id){
//get groups
$groups = JUserHelper::getUserGroups($me->id);
//check if current user object has right groups
if($me->groups != $groups){
//if not update groups and clear session access levels
$me->groups = $groups;
$me->set('_authLevels', null);
}
}
Hope it will help.
Possible "Easy" Solution:
The code is correct and it should put your $userId and $groupId in the db to be precise in #__user_usergroup_map .
Btw consider that this method is rising an error if you use a wrong groupId but it's not raising any error if you insert a wrong $userId and for wrong I mean that it doesn't exist.
So there are canches that the user with $userId = 358; doesn't exist.
Update - Hard Debugging:
Ok in this case I suggest you to digg in the code of the helper.
The file is :
libraries/joomla/user/helper.php
On line 33 You have JUserHelper::addUserToGroup.
This is the code:
public static function addUserToGroup($userId, $groupId)
{
// Get the user object.
$user = new JUser((int) $userId);
// Add the user to the group if necessary.
if (!in_array($groupId, $user->groups))
{
// Get the title of the group.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__usergroups'))
->where($db->quoteName('id') . ' = ' . (int) $groupId);
$db->setQuery($query);
$title = $db->loadResult();
// If the group does not exist, return an exception.
if (!$title)
{
throw new RuntimeException('Access Usergroup Invalid');
}
// Add the group data to the user object.
$user->groups[$title] = $groupId;
// Store the user object.
$user->save();
}
if (session_id())
{
// Set the group data for any preloaded user objects.
$temp = JFactory::getUser((int) $userId);
$temp->groups = $user->groups;
// Set the group data for the user object in the session.
$temp = JFactory::getUser();
if ($temp->id == $userId)
{
$temp->groups = $user->groups;
}
}
return true;
}
The bit that save the group is $user->save();.
Try to var_dump() till there and see where is the issue.
Author already halfish solved this problem so this answer may help to others. It took me hours of debugging with Eclipse and XDebug to find the issue. This error was very tricky because addUserToGroup() was returning true for me and user object was as well with successful changes, but they were not saved in database. The problem was that onUserBeforeSave() method in my plugin was throwing exception whenever addUserToGroup() was trying to save a user. So check your implementation of onUserBeforeSave() if you touched it. If not you must install Eclipse with XDebug and try to debug what is your exact problem.

How to build login api to other sites in Codeigniter different sites ex. IP.Board / wordpress

I'm starting with Codeigniter and i have a question. How (if possible) to create authorization api to other sites (no facebook, twitter etc.).
I have three sites - CI system, IP.Board and WordPress, and my point is a integrating all those systems in one auth api.
I heared about Oauth and OpenId, but I don't know how this things has postponement to CI. Is complicated?
Hello It can be possible
I can suggestion you can use temporary table store that table logged user id and current date& time and also ip address ,and store that one in to Sharing Cookies Across Multiple Domains/send hidden stored id to your multiple site.based on id and ip and time you can implement logic
note: logout and session time out times you can delete the record form table
I have written small example plz check it once . it my be helpful you
Session will most likely not to work. I thought of using JSON but I failed to figure out how to make the Javascript to be dynamic as I always prefer to use jQuery (if you have an idea, feel free to share). In the end, I decided to use PHP along with MySQL and cURL.
We will first start by creating a table to handle the login session:
CREATE TABLE IF NOT EXISTS `sso_login` (`user_id` int(11) NOT NULL)
Purpose of the table is to store logged in users and remove then upon signing out.
Next is the PHP script which will insert new user id upon signing in (file name: register.php):
<?php
/**
*
* #
*/
$userId = $_GET['id'];
$sql = sprintf("INSERT INTO sso_login VALUES ('%s')", $userId);
mysql_query($sql);
?>
Simple, isn’t it? All it does is inserting the user id into the table.
Next is the file which delete the user id when user is signing out (file name: drop.php):
<?php
/**
*
*
*/
$userId = $_GET['id'];
$sql = sprintf("DELETE FROM sso_login WHERE user_id = '%s'", $userId);
mysql_query($sql);
?>
I believe the script is pretty much self-explainatory.
Next is the file which checks whether user is already logged in or vice versa (file name: check.php):
<?php
/**
*
*
*/
$userId = $_GET['userId'];
$sql = sprintf("SELECT COUNT(*) FROM sso_login WHERE user_id = '%s'", $userId);
$query = mysql_query($sql);
if (mysql_result($query, 0, 0) > 0)
{
echo 'User '.$userId.' has logged in';
}
else
{
echo 'User '.$userId.' is not logged in.';
}
?>
This script will check whether the user id being passed is available in the table or not. If not it means that the user have not logged in.
These three scripts will be store on the other party’s server. Next are scripts which will be store on BC’s server where the login and logout process being done. All these processes are using cURL.
First is the script which will update the other site when user is logged in (file name: login.php):
<
?php
/**
*
*
*/
if($_POST)
{
$username = $_POST['username'];
$password = $_POST['password'];
if (($username == "bcoffee") && ($password == "demo"))
{
$userId = 10;
$target_site = sprintf("http://the-other-site.com/register.php?id=%s", $userId);
$timeout = 5;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_site);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
curl_close($ch);
header('location: index.php');
}
}
?>
You might notice that the username and password are hard coded, this was for P.O.C purpose, so it does not matter. Back to the tutorial.
As you can see, it will reach register.php with the user’s id. Refer back to content of register.php which will then insert the user id into the table. This way, the other server will know that user is already logged in.
Next is the script which will notify the other server when the user is signing out (file name: logout.php):
<?php
/**
*
*
*/
$userId = 10;
$target_site = sprintf("http://the-other-site.com/drop.php?id=%s", $userId);
$timeout = 5;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_site);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
curl_close($ch);
header('location: login.html');
?>
Similar to login.php but this time, it will remove the user id when user is signing out. Hence when the system checks (using check.php) and found that the user id is not available, it will know that the user is not currently logged in.

Magento doesn't assign website id when creating customer programatically

I m creating customer programatically but customer created successfully but can't login with frontend.issue is website id dosen't assign to customer i have tried below code
if ($detail->ContactEmail && $detail->ContactName != '' && filter_var($detail->ContactEmail, FILTER_VALIDATE_EMAIL)) {
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(1);
$customer->loadByEmail($detail->ContactEmail);
/*
* Check if the email exist on the system.
* If YES, it will not create a user account.
*/
if (!$customer->getId()) {
//setting data such as email, firstname, lastname, and password
$customer->setEmail($detail->ContactEmail);
$name = preg_split('/\s+/', trim($detail->ContactName));
if (count($name) == 1) {
$customer->setFirstname($name[0]);
$customer->setLastname($name[0]);
} else {
$customer->setFirstname($name[0]);
$customer->setLastname($name[1]);
}
//$customer->setWebsiteId(array(1));
$customer->setcontactJobTitle($detail->ContactJobTitle);
$customer->setcontactSeqNo($detail->ContactSeqNo);
$customer->setdebtorAccNo($detail->DebtorAccNo);
$customer->setdebtorApiKey($debtorAPI);
$customer->setStoreId(Mage::app()->getStore('default')->getId());
$customer->setPassword($customer->generatePassword($passwordLength));
}
try {
//the save the data and send the new account email.
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customer->sendNewAccountEmail();
$customerCount[] = $i;
//echo 'contact added';
}
catch (Exception $e) {
//echo 'contact not added';
}
I have found where is the problem to saving customer. whenever we load the customer by loadByEmail function we must set website id to load customer otherwise customer save raise the exception Customer website ID must be specified when using the website scope. so I have made following changes to set website id when creating customer.
if ($detail->ContactEmail && $detail->ContactName != '' && filter_var($detail->ContactEmail, FILTER_VALIDATE_EMAIL)) {
$customer = Mage::getModel('customer/customer')->setWebsiteId(1);
$customer->loadByEmail($detail->ContactEmail); /* changed line */
/*
* Check if the email exist on the system.
* If YES, it will not create a user account.
*/
if (!$customer->getId()) {
//setting data such as email, firstname, lastname, and password
$customer->setEmail($detail->ContactEmail);
$name = preg_split('/\s+/', trim($detail->ContactName));
if (count($name) == 1) {
$customer->setFirstname($name[0]);
$customer->setLastname($name[0]);
} else {
$customer->setFirstname($name[0]);
$customer->setLastname($name[1]);
}
$customer->setcontactJobTitle($detail->ContactJobTitle);
$customer->setcontactSeqNo($detail->ContactSeqNo);
$customer->setdebtorAccNo($detail->DebtorAccNo);
$customer->setdebtorApiKey($debtorAPI);
$customer->setStoreId(Mage::app()->getStore('default')->getId());
$customer->setPassword($customer->generatePassword($passwordLength));
}
try {
//the save the data and send the new account email.
$customer->save();
$customer->setConfirmation(null);
$customer->setWebsiteId(1); /* changed line */
$customer->save();
$customer->sendNewAccountEmail();
$customerCount[] = $i;
//echo 'contact added';
}
catch (Exception $e) {
//echo 'contact not added';
}
please find the below code for creating customer programmatically:
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
if (is_dir('downloader')) {
header("Location: downloader");
} else {
echo $mageFilename." was not found";
}
exit;
}
require_once $mageFilename;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
$customer_email = 'test#testemail.com';
$customer_fname = 'test_firstname';
$customer_lname = 'test_lastname';
$passwordLength = 10;
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email);
if(!$customer->getId()) {
$customer->setEmail($customer_email);
$customer->setFirstname($customer_fname);
$customer->setLastname($customer_lname);
$customer->setPassword($customer->generatePassword($passwordLength));
}
try{
$customer->save();
$customer->setConfirmation(null);
$customer->save();
}
================================================================================

Magento. Customer online, but not logged in

Here is the problem:
I am using a script to create a user in Magento and trying to login this user if he he/she already exists.
try {
// If new, save customer information
$customer -> firstname = $firstname;
$customer -> lastname = $lastname;
$customer -> email = $email;
$customer -> password_hash = md5($password);
if ($customer -> save()) {
echo $customer -> firstname . " " . $customer -> lastname . " information is saved!";
$customer->setConfirmation(null);
$customer->save();
} else {
echo "An error occured while saving customer";
}
} catch(Exception $e) {
// If customer already exists, initiate login
if (preg_match('/This customer email already exists/', $e)) {
$customer -> loadByEmail($email);
$session = Mage::getSingleton('customer/session');
$session -> login($email, $password);
echo $session -> isLoggedIn() ? $session -> getCustomer() -> getName() . ' is online!' : 'not logged in';
}
}
the script echoes "user is online!", but when I go to the main page, it shows me the login button, as if I am not logged in. How do I login the user?
Did you set the website id?
You could also check if the customer exist by loadByEmail
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
// This customer email exists
if($customer->getId()){
Mage::getSingleton('core/session', array('name' => 'frontend'));
Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
}
else{
.....
$customer->save();
}
or
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
if(!$customer->getId()){
$customer->setFirstname($firstname);
$customer->setLastname($lastname);
.....
$customer->save();
}
Mage::getSingleton('core/session', array('name' => 'frontend'));
Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
See
Auto Login customer to Magento
Programatically create customer

Resources