Laravel Fogot password not working - Invalid Token - laravel-8

I tried to implement forgot password functionality and it's sending email correctly but when trying to rest the password its shows an invalid token even though I tested the token manually with Hash::check() which returns true.
Forgot Password Email Sender:
$status = Password::sendResetLink(
$request->only('email')
);
return $status == Password::RESET_LINK_SENT
? redirect()->route('success', 'forgot-password')->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
Reset Password Receiver:
$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
$user->forceFill([
'password' => Hash::make($password),
]);
$user->save();
event(new PasswordReset($user));
}
);

I got the answer, It's my mistake when I created the table to hold the token and email I made created_at as date so only the created date is held on it so the token will be always invalid.
Solution: created_at should be either dateTime or timestamp.(So it can hold the time too)

Related

laravel 5.1 auth login returns an error

I'm trying to use auth()->login() in laravel 5.1 but it returns an error. Please see my code below:
$user = User::where('username', $username)->where('activation_code', $activation_code);
$not_activated_user = $user->where('status', 0)->where('confirmed', 0);
if($not_activated_user->count() == 1){
$not_activated_user->update([
'status' => 1,
'confirmed' => 1
]);
auth()->login($user->where('status', 1)->where('confirmed', 1));
}
I've also import use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; and implements AuthenticatableContract in my User model, but it still returns the same error. Why is that? I also tried to use ->get() in ->login(....->get()) to get the current user, but still same error.
Error:
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Database\Eloquent\Builder given
Try
$user = User::where('username', $username)->where('activation_code', $activation_code)->firstOrFail();
$user->update([
'status' => 1,
'confirmed' => 1
]);
auth()->login($user);
}
It's working now, I just called the User model again to select the current user :)

omnipay - AuthorizeNet_CIM

I am trying to setup a payment processor using omnipay-authorizenet AuthorizeNet_CIM. Not a lot of documentation on this.
Step 1) I create the gateway object successfully and can make requests to the authorize.net sandbox server.
Step 2) is to "create a card" for future use with Token Billing: $gateway->createCard() . This is successful.
From the General Omnipay Token Billing documentation:
"... createCard($options) - returns a response object which includes a cardReference, which can be used for future transactions..."
I don't see a specific 'cardReference' in the above createCard() response object.
So I create a $cardRef array and grab the returned response CustomerProfileId and CustomerPaymentProfileId .
$profileResult['customerProfileId']=$response->getCustomerProfileId();
$profileResult['paymentProfileId']=$response->getCustomerPaymentProfileId();
Step 3) is a function to generate the purchase which fails:
function create_transaction($cardRef,$amount,$description,$invoice_number){
global $status, $gateway;
try {
// Send purchase request
$response = $gateway->purchase(
array(
'cardReference' => $cardRef ,
'amount' => $amount,
'currency' => 'USD',
'description' => $_POST['description'],
'transactionId' => $invoice_number
)
)->send();
if ($response->isSuccessful()) {
// Payment was successful
$status.='Success: '.$response->getMessage();
} elseif ($response->isRedirect()) {
// Redirect to offsite payment gateway
$response->redirect();
} else {
// Payment failed
$status.='Transaction Failure: '.$response->getMessage();
}
} catch (Exception $e) {
$status.='<strong>Error:</strong> '.$e->getMessage(). "<br/>";
}
}
The purchase fails and when I look at the Response object it seems the Request is not populating the cardReference object.
[cardReference] => Omnipay\AuthorizeNet\Model\CardReference Object
(
[customerProfileId:Omnipay\AuthorizeNet\Model\CardReference:private] =>
[paymentProfileId:Omnipay\AuthorizeNet\Model\CardReference:private] =>
[shippingProfileId:Omnipay\AuthorizeNet\Model\CardReference:private] =>
)
I am obviously not passing the correct cardReference data to the purchase method.
Any help would be greatly appreciated.
Thanks
I apparently needed to use the undocumented Omnipay method;
$response->getCardReference();
to get a cardReference object to pass to my create_transaction() function.
All fixed.
Thanks

laravel eloquent error in saving password

I'm trying to learn how to use Laravel and until now I haven't had any problem.
Then I tried to implements an example of Authentication.
1) I created a migration file where i set the User table (password is set to 60, the token is set to 100 following the instructions on laravel site)
2) I used a seeder to generate fake users:
$faker = Faker::create();
foreach(range(1, 10) as $index)
{
User::create([
'firstname' => $faker->firstName(),
'lastname' => $faker->lastName(),
'email' => $faker->email(),
'password' => Hash::make('password'),
'telephone' => $faker->phoneNumber(),
'admin' => rand(0,1)
]);
}
The table in the database is perfectly seeded without problem.
3) I created a controller UserController with the following function
public function postCreate() {
$validator = Validator::make($data = Input::all(), User::$rules);
if ($validator->fails()) {
return Redirect::to('users/newaccount')->with('message','error')->withInput()->withErrors($validator);
}
unset($data['password_confirmation']);
$data['password'] = Hash::make(Input::get('password'));
User::create($data);
return Redirect::to('users/signin')->with('message','thanks');
}
4) I created a form for register the user
{{ Form::open(array('route'=>array('users.createaccount'), 'method' => 'post')) }}
.....
{{ Form::label('password') }}
{{ Form::password('password') }}
....
{{ Form::submit('Create new account') }}
{{ Form::close() }}
Problem is when i check on the DB table the user is register and saved, and all the fields are filled except for the password (and the token that remains null)..
But if i do a dd($data) before calling User::create($data) this is what i can see:
array(6) { ["_token"]=> string(40) "5bSkWYHLfeBh8E2agiB15J2cQSzs6orI7m5ruFhx" ["firstname"]=> string(4) "mark" ["lastname"]=> string(4) "mark" ["email"]=> string(13) "mark#mark.com" ["password"]=> string(60) "$2y$10$J3eM3nBZ0l8eNa1BxpoDc.BzeQUKzTc7dwwbu7g7GdQLj4gJgjWxe" ["telephone"]=> string(8) "12345678" }
Anyone can tell me why only the password and the token fields are not saved with the Eloquent method in the database, while they are saved using the seeder?
thk
All fields you want to pass to create need to be defined inside the $fillable array. Also password
protected $fillable = array('firstname', 'lastname', 'email', 'password', 'telephone', 'admin');
^^
_token definitely doesn't need to be in there. It's just a token Laravel adds for CSRF protection

Auth::attempt() does not return anything

I'm using laravel 4.
This is my Seeder
User::create(array(
'name' => 'Rubber Gajulu',
'username' => 'awesome',
'email' => 'awesome#awe.com',
'password' => Hash::make('awesome'),
));
And this is where I am testing the Auth::attempt() function
Route::get('testlogin',function(){
$userdata = ['email'=> 'awesome#awe.com','password'=> 'awesome'];
echo Auth::attempt($userdata);
if (Auth::attempt($userdata))
echo 'SUCCESS!';
else
echo 'FAILURE!';
});
It just returns FAILURE. The first echo does not return anything.
Your controller code is wrong. It should be this:
Route::get('testlogin',function(){
$userdata = ['email'=> 'awesome#awe.com','password'=> 'awesome'];
if (Auth::attempt($userdata))
echo 'SUCCESS!';
else
echo 'FAILURE!';
});
Otherwise in your current code you are trying to login the user twice, so the second one fails.
Check if logs/laravel.log shows any error on hitting this url.
Share that.
Okay I found my mistake
Thanks to Isabell Knauers answer
My model had
$table->string('password',32)
Data was being truncated to 32 characters. Now I changed it to
$table->string('password',100)
and everything works as intended

Get more logged-in information in Zend Framework2

I'm implementing a log-in form by using Zend Framework 2.
I have a "user" table in mysql database: user(user_id INT AUTO_INCREMENT,email-address, password).
The user will input email-address and password.
My web application will call authenticate() method with the identity is email-address, the credential is password.
If the validation success, getIdentity() return the email-address.
But not only the email-address, I want to know the user_id field to use for other query after logged-in.
How can I get more information in my "user" table?
I don't want to query from database twice. (once for authentication, once for user_id).
Do you have any suggestion?
Thanks.
Just authenticate the correct user call the AuthenticationService and the method getStorage with will return you the storage container for the current user. just pass a object or your user entity in this and after calling $this->identity() you should become your custom identity object.
$authService = $this->getServiceManager('Zend\Authentication\AuthenticationService');
if ( // user auth success ) {
$userObject = array(
'foo' => 'bar',
'baz' => 'bas'
);
$authService->getStorage()->write( $userObject );
}
now by calling $this->identity() u get the following output
var_dump( $this->identity() );
//output
array(
'foo' => 'bar',
'baz' => 'bas'
)
Sometime like this
// Set the input credential values (e.g., from a login form)
$authAdapter
->setIdentity('my_username')
->setCredential('my_password')
// Print the result row
print_r($authAdapter->getResultRowObject());
/* Output:
user table
Array
(
[user_id] => 1
[email-address] => my_username
[password] => my_password
)
Also read more manual: http://framework.zend.com/manual/current/en/modules/zend.authentication.adapter.dbtable.html