Why bcrypt.compareSync() is always returning me a false - express

I'm comparing the password from a FORM in a login view, with the password in a database in .JSON. But even when I see in the console that both are the same, the method is always returning a false.
for (let i = 0; i<users.length; i++) {
if (users[i].email == req.body.email) {
console.log(req.body.password); //Here I notice that both passwords are the same
console.log(users[i].password);
** if (bcrypt.compareSync(req.body.password, users[i].password)) {
var loggedUser= users[i]
console.log("correct password");
break
}
else{
console.log("wrong password");
}**
}
}

You should check whether you hash the user's password correctly or not. Because of req.body.password and users[i]. Password must be different.

Related

Validating the login window in appcelerator

I am working with appcelerator and i am creating a login window. But all the validations do not seem to work properly. Also the error i am facing for the validation where username should not be numeric is throwing me a runtime error. Please help!
function loginUser() {
var uName = $.username;
var pwd = $.password;
var correctUName = "sayali";
var correctPwd = "123sayali";
var letters = /^[A-Za-z]+$/;
if(uName == "" || pwd == ""){
alert("Please fill all the credentials!!");
}
else{
if(uName.match == letters){
if(uName == correctUName){
if(pwd == correctPwd){
alert("Login Successful!!");
}
else{
alert("Incorrect Password!");
}
}
else{
alert("User doesn't exist!");
}
}
else{
("Numeric values are not allowed in Username!");
}
}
}
Instead of using uName.match == letters you should use i.e. letters.test(uName)
Click here for more information on regular expressions and Javascript

change password code is not working

I was just working on a password changing program for my website, it resulted in all my users passwords changed into the same password.
The code which I used is displayed below.
If any one could help me out it would be a big thanks to him/her.
//if form has been submitted process it
<br/>
if(isset($_POST['submit'])){
$stmt = $db->prepare('SELECT password FROM user WHERE password = :hashedpassword');
$stmt->execute(array(':hashedpassword' => $_POST['password']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if(strlen($_POST['password']) < 3){
$error[] = 'Password is too short.';
}
if(strlen($_POST['passwordConfirm']) < 3){
$error[] = 'Confirm password is too short.';
}
if($_POST['password'] != $_POST['passwordConfirm']){
$error[] = 'Passwords do not match.';
}
//if no errors have been created carry on
if(!isset($error)){
//hash the password
$hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);
try {
//insert into database with a prepared statement
$sql="UPDATE user SET password = :hashedpassword";
$stmt = $db->prepare($sql);
$stmt->execute(array(
':hashedpassword' => $hashedpassword
));
//redirect to index page
header('Location: login.php?action=resetAccount');
exit;
//else catch the exception and show the error.
} catch(PDOException $e) {
$error[] = $e->getMessage();
}
}
}

google directory api -> Get phone numer from user.phones[].primary

I use this code to logger successfully log : user fullname and user primaryEmail.
from property of AdminDirectory.Users.list
But I don't understand how I can get the phone user .
The syntax user.phones[].primary doesn't work
function listAllUsers() {
var pageToken, page;
do {
page = AdminDirectory.Users.list({
domain: 'example.com',
orderBy: 'givenName',
maxResults: 100,
pageToken: pageToken
});
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
Logger.log(user.name.fullName, user.primaryEmail,user.phones[].primary);
}
} else {
Logger.log('No users found.');
}
pageToken = page.nextPageToken;
} while (pageToken);
}
The parameters user.phones[] doesnt' work see google reference
You are attempting to access an array (that's what the [] indicates), therefore you must specify an index.
If you want to access the primary variable of the first value in the phones array, then you would use:
user.phones[0].primary

In yii login functionality when password is wrong

In yii i am creating login functionality. When user enters correct username but wrong password i want to make serach in database for this correct username and want to put that username's id into loginattemmpt table and display wrong password message to him. So can please someone help me.
in userIdentity.php save data in table .
public function authenticate() {
$user = User::model()->findByAttributes(array('username' => $this->username));
if ($user === null) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
}
elseif($user->password !== crypt($this->password, $salt))
{ // save $user->id in attempt table here .
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}else{
//set id
}
and in file from where authenticate function is called setError.
$this->_identity = new UserIdentity($this->username, $this->password);
if (!$this->_identity->authenticate())
if ($this->_identity->errorCode === UserIdentity::ERROR_USERNAME_INVALID) {
$this->addError('password', 'Incorrect email Id');
}elseif($this->_identity->errorCode === UserIdentity::ERROR_PASSWORD_INVALID){
$this->addError('password', 'Incorrect Password');
}

Login with Kohana auth module - what am I doing wrong?

I'm trying to login with the following controller action, but my login attempt keeps failing (I get the 'invalid username and/or password' message). What am I doing wrong? I also tried the other method given in the examples in the auth documentation, Auth::instance()->login($user->username, $form->password);, but I get the same result. Kohana version is 2.3.4.
public function login() {
$auth = Auth::instance();
if ($auth->logged_in()) {
url::redirect('/account/summary');
}
$view = new View('login');
$view->username = '';
$view->password = '';
$post = $this->input->post();
$form = new Validation($post);
$form->pre_filter('trim', 'username')
->pre_filter('trim', 'password')
->add_rules('username', 'required');
$failed = false;
if (!empty($post) && $form->validate()) {
$login = array(
'username' => $form->username,
'password' => $form->password,
);
if (ORM::factory('user')->login($login)) {
url::redirect('/accounts/summary');
} else {
$view->username = $form->username;
$view->message = in_array('required', $form->errors()) ?
'Username and password are required.' :
'Invalid username and/or password.';
}
}
$view->render(true);
}
Figured out my problem... Something in my registration process is missing, because it's creating the user record but not the role-to-user assoc record. Login needs a specific role to log in to, or it won't work even with a valid username and password. Manually inserting the record allowed my to log in, so I'll just have to debug my registration action a bit.