Laravel - sending recovery mail - laravel

I'm trying to send a recovery mail using Laravel. I have the following recovery method:
public function recovery(Request $request)
{
$validator = Validator::make($request->only('email'), [
'email' => 'required'
]);
if($validator->fails()) {
throw new ValidationHttpException($validator->errors()->all());
}
$response = Password::sendResetLink($request->only('email'), function (Message $message) {
$message->subject(Config::get('boilerplate.recovery_email_subject'));
});
switch ($response) {
case Password::RESET_LINK_SENT:
return $this->response->noContent();
case Password::INVALID_USER:
return $this->response->errorNotFound();
}
}
I tried to output $request->email and the reset email is the output, but for some reasons I get the following error:
Undefined index: email
at
"/home/pokemoti/public_html/api/vendor/laravel/framework/src/Illuminate/Auth/Passwords/PasswordBrokerManager.php" on line 74
Any idea what could have gone wrong?

Fixed it by adding the following line in my config/auth.php passwords->users array:
'email' => 'auth.emails.password',
took it from another project where it worked.

Related

Users Controller does not exist

I'm building a CMS in Laravel 5.8 with an in-built Authentication system. My app was working fine but suddenly it starts giving me an error as ReflectionException : Class App\Http\Controllers\Users does not exist on command php artisan route:list -v. However, if i define a new resource route, then that new route page works fine. It means, route file is getting saved but gives error while listing the routes.
I have not created any file named as Users but my user controller file name is UserController
Below is the structure of my application:
cms -> project folder
app
Http
Controllers
Auth
RegisterController.php
LoginController.php and other Auth files
backend
UserController.php and my other controller files
Below is my route file
Auth::routes();
Route::group(['as'=>'cms.' ,'prefix'=>'cms'],function(){
Route::get('/', 'backend\Dashboard#index')->name('dashboard');
Route::resource('/user-management', 'backend\UserController');
});
Below is my UserController file residing in backend folder
<?php
namespace App\Http\Controllers\backend;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Auth;
use App\Models\backend\Admin;
use App\Models\backend\Menu;
use App\Models\backend\Submenu;
use Config;
use illuminate\Support\Facades\Validator;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin');
}
public function AuthRouteAPI(Request $request)
{
return $request->user();
}
public function index()
{
// GET THE CURRENT LOGGED IN USE
$user = Auth::user();
// CODE FOR GETTING MENU LIST FOR CURRENT USER STARTS
$menutable = (new Menu)->getTable();
$arrUserMenu = $user->getUserMenus($menutable);
// CODE FOR GETTING MENU LIST FOR CURRENT USER ENDS
$cms_users = Admin::withTrashed()->get();
return view('backend.pages.users.index', ['arrUserMenu'=>$arrUserMenu, 'cms_users'=>$cms_users]);
}
public function create()
{
// GET THE CURRENT LOGGED IN USE
$user = Auth::user();
// CODE FOR GETTING MENU LIST FOR CURRENT USER STARTS
$menutable = (new Menu)->getTable();
$arrUserMenu = $user->getUserMenus($menutable);
// CODE FOR GETTING MENU LIST FOR CURRENT USER ENDS
return view('backend.pages.users.create', ['arrUserMenu'=>$arrUserMenu]);
}
public function store(Request $request)
{
$rules = [
'name' => 'required|min:'.Config::get('cms_const.USER_NAME_MIN_LEN').'|max:'.Config::get('cms_const.USER_NAME_MAX_LEN').'|regex:/(^[A-Za-z ]+$)+/',
'usrmail' => 'required|email|unique:cms_users,email|max:'.Config::get('cms_const.USER_EMAIL_MAX_LEN'),
'usrname' => 'required|min:'.Config::get('cms_const.USER_ID_MIN_LEN').'|max:'.Config::get('cms_const.USER_ID_MAX_LEN').'|regex:/(^[A-Za-z0-9._]+$)+/|unique:cms_users,username',
'usrpwd' => 'required|min:'.Config::get('cms_const.USER_PWD_MIN_LEN').'|max:'.Config::get('cms_const.USER_PWD_MAX_LEN').'|regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-_]).{7,}$/'
];
$msgs = [
'name.required' => 'Please enter name',
'name.min' => 'Name should not be less then '.Config::get('cms_const.USER_NAME_MIN_LEN').' characters',
'name.max' => 'Name should not be more then '.Config::get('cms_const.USER_NAME_MAX_LEN').' characters',
'name.regex' => 'Only alphabets are allowed in name',
'usrmail.required' => 'Please enter email',
'usrmail.email' => 'Invalid email',
'usrmail.unique' => 'Email already exist',
'usrmail.max' => 'Email should not be more then '.Config::get('cms_const.USER_EMAIL_MAX_LEN').' characters',
'usrname.required' => 'Please enter the user name',
'usrname.min' => 'User name should not be less then '.Config::get('cms_const.USER_ID_MIN_LEN').' characters',
'usrname.max' => 'User name should not be more then '.Config::get('cms_const.USER_ID_MAX_LEN').' characters',
'usrname.regex' => 'Only Alphabets, Numbers, Dot and Underscore allowed in user name',
'usrname.unique' => 'User name already exist',
'usrpwd.required' => 'Please enter the password',
'usrpwd.min' => 'Password should not be less then '.Config::get('cms_const.USER_PWD_MIN_LEN').' characters',
'usrpwd.max' => 'Password should not be more then '.Config::get('cms_const.USER_PWD_MAX_LEN').' characters',
'usrpwd.regex' => 'Invalid password format',
];
$this->validate($request, $rules, $msgs);
Admin::create([
'name' => $request->name,
'username' => $request->usrname,
'password' => bcrypt($request->usrpwd),
'email' => $request->usrmail,
]);
return redirect()->route('cms.user-management.index')->with('success','New user account created successfully.');
}
public function edit($id)
{
if(!$id)
{
return redirect()->route('cms.user-management.index');
die;
}
$arrRecord = Admin::find($id);
if(is_null($arrRecord))
{
return redirect()->route('cms.user-management.index')->withErrors(['error'=>'Record you are trying to edit does not exist']);
die;
}
// GET THE CURRENT LOGGED IN USE
$user = Auth::user();
// CODE FOR GETTING MENU LIST FOR CURRENT USER STARTS
$menutable = (new Menu)->getTable();
$arrUserMenu = $user->getUserMenus($menutable);
// CODE FOR GETTING MENU LIST FOR CURRENT USER ENDS
return view('backend.pages.users.edit', ['arrUserMenu'=>$arrUserMenu, 'arrRecord'=>$arrRecord]);
}
public function update(Request $request, $id)
{
$rules = [
'name' => 'required|min:'.Config::get('cms_const.USER_NAME_MIN_LEN').'|max:'.Config::get('cms_const.USER_NAME_MAX_LEN').'|regex:/(^[A-Za-z ]+$)+/',
'usrmail' => 'required|email|max:'.Config::get('cms_const.USER_EMAIL_MAX_LEN'),
'usrpwd' => 'nullable|min:'.Config::get('cms_const.USER_PWD_MIN_LEN').'|max:'.Config::get('cms_const.USER_PWD_MAX_LEN').'|regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-_]).{7,}$/'
];
$msgs = [
'name.required' => 'Please enter name',
'name.min' => 'Name should not be less then '.Config::get('cms_const.USER_NAME_MIN_LEN').' characters',
'name.max' => 'Name should not be more then '.Config::get('cms_const.USER_NAME_MAX_LEN').' characters',
'name.regex' => 'Only alphabets are allowed in name',
'usrmail.required' => 'Please enter email',
'usrmail.email' => 'Invalid email',
'usrmail.max' => 'Email should not be more then '.Config::get('cms_const.USER_EMAIL_MAX_LEN').' characters',
'usrpwd.min' => 'Password should not be less then '.Config::get('cms_const.USER_PWD_MIN_LEN').' characters',
'usrpwd.max' => 'Password should not be more then '.Config::get('cms_const.USER_PWD_MAX_LEN').' characters',
'usrpwd.regex' => 'Invalid password format',
];
$this->validate($request, $rules, $msgs);
$arrRecord = Admin::find($id);
if(is_null($arrRecord))
{
return redirect()->route('cms.user-management.index')->withErrors(['error'=>'Record you are trying to edit does not exist']);
die;
}
$arrRecord->name = $request->input('name');
$arrRecord->email = $request->input('usrmail');
if($request->input('usrpwd'))
{
$arrRecord->password = bcrypt($request->input('usrpwd'));
}
$arrRecord->save();
return redirect()->route('cms.user-management.index')->with('success','User account details updated successfully.');
}
public function destroy($id)
{
try
{
$user = Admin::findOrFail($id);
$user->delete();
return response()->json(['success'=>'Record successfully deleted', 'status'=>'Suspended']);
}
// catch(Exception $e) catch any exception
catch(ModelNotFoundException $e)
{
return response()->json(['error'=>'Record you are trying to delete does not exist']);
}
}
}
I have tried the below commands and they all run successfully but none resolved the error
php artisan clear-compiled
composer dump-autoload
php artisan optimize
php artisan route:cache
Don't know from where this Users controller class in being referenced.
Can anyone help me in this regard as I'm badly stuck in my development.
Much Regards,
Javed
#ClémentBaconnier the error is resolved now. I made the changes in api.php to Route::middleware('auth:api')->get('/user', 'backend\Users#AuthRouteAPI'); and now all my routes are listed. Thanks u soooo much for ur advice.

Find data before validate form request laravel

I want to update the data using the request form validation with a unique email role, everything works normally.
Assume I have 3 data from id 1-3 with url:
127.0.0.1:8000/api/user/update/3
Controller:
use App\Http\Requests\Simak\User\Update;
...
public function update(Update $request, $id)
{
try {
// UPDATE DATA
return resp(200, trans('general.message.200'), true);
} catch (\Exception $e) {
// Ambil error
return $e;
}
}
FormRequest "Update":
public function rules()
{
return [
'user_akses_id' => 'required|numeric',
'nama' => 'required|max:50',
'email' => 'required|email|unique:users,email,' . $this->id,
'password' => 'required',
'foto' => 'nullable|image|max:1024|mimes:jpg,png,jpeg',
'ip' => 'nullable|ip',
'status' => 'required|boolean'
];
}
but if the updated id is not found eg:
127.0.0.1:8000/api/user/update/4
The response gets The email has already been taken.
What is the solution so that the return of the data is not found instead of validation first?
The code looks like it should work fine, sharing a few things below that may help.
Solution 1: Check if $this->id contains the id you are updating for.
Solution 2: Try using the following changes, try to get the id from the URL segment.
public function rules()
{
return [
'user_akses_id' => 'required|numeric',
'nama' => 'required|max:50',
'email' => 'required|email|unique:users,email,' . $this->segment(4),
'password' => 'required',
'foto' => 'nullable|image|max:1024|mimes:jpg,png,jpeg',
'ip' => 'nullable|ip',
'status' => 'required|boolean'
];
}
Sharing one more thing that may help you.
Some person uses Request keyword at the end of the request name. The Update sounds generic and the same as the method name you are using the request for. You can use UpdateRequest for more code readability.
What I understand from your question is, you need a way to check if the record really exists or not in the form request. If that's the case create a custom rule that will check if the record exists or not and use that rule inside your request.
CheckRecordRule
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CheckRecordRule implements Rule
{
protected $recordId;
public function __construct($id)
{
$this->recordId = $id;
}
public function passes($attribute, $value)
{
// this will check and return true/false
return User::where('id', $this->recordId)->exists();
}
public function message()
{
return 'Record not found.';
}
}
Update form request
public function rules()
{
return [
'email' => 'required|email|unique:users,email,' . $this->id.'|'. new CheckRecordRule($this->id),
];
}
So when checking for duplicate it will also check if the record really exists or not and then redirect back with the proper message.

Laravel Test on controller returning uncorrect status code

I'm trying to create a test on a rest API using Laravel. My test is in 3 steps : Arrange, act and assert.
During the first step I create an entry in the database. For the second test i query my DB, finally i check if the tests are true or not.
use RefreshDatabase;
/** #test */
public function createTest() {
// Arrange : name & message OK
$message = ChatMessages::create([
'username' => 'My name is long enough',
'message' => 'my message is also OK'
]);
// Act
$getAllMessages = ChatMessages::all();
$this->post('/message', $message->toArray())
->assertStatus(200)
->assertExactJson([
'status' => 'ok', 'chat_message' => $message->toArray()
]);
// assert : if name too short, data is not persisted
$this->assertEquals(sizeof($getAllMessages), 1);
}
My route :
Route::post('/message', 'ChatController#create');
I have the following error message :
Expected status code 200 but received 404.
Failed asserting that false is true.
In my Controller I have this code :
public function create(Request $request) {
$input = $request->validate([
'username' => 'required|string|min:3|max:255',
'message' => 'required|string|min:1',
]);
try {
$message = ChatMessages::create([
'username' => $input['username'],
'message' => $input['message'],
]);
} catch (\Exception $err) {
Log::error("Cannot fetch chat messages");
Log::error($err);
abort(500);
}
return response()->json(['status' => 'ok', 'chat_message' => $message]);
}
$request->validate should give a 302 redirection if not satisfied and 200 if OK.

Laravel : Validation not working when testing on live

Create a user validate function with validation in API integration When i try to validate user with some validation and check it in postman it is working perfect in local machine but when i uploaded code on server and test, it is not working.
Controller function :
public function confirmUser(ForgotPasswordRequest $request)
{
// ForgotPasswordRequest for validate
$user = User::leftjoin('user_details','users.id', '=', 'user_details.user_id')
->where('user_details.phone', '=', $request->post('phone'))
->where('users.email', '=', $request->post('email'))
->first();
if(!$user) {
return response()->json([
'message' => "The credentials you provided cannot be determined to be authentic.!!",
'status_code' => 404,
]);
}
$token = JWTAuth::fromUser($user);
return response()->json([
'message' => "Your account is eligible for change password.!!",
'token' => $token,
'status_code' => 200
]);
}
My forgotPasswordRequest.php code :
<?php
namespace App\Api\V1\Requests;
use Config;
use Dingo\Api\Http\FormRequest;
class ForgotPasswordRequest extends FormRequest
{
public function rules()
{
return Config::get('boilerplate.forgot_password.validation_rules');
}
public function authorize()
{
return true;
}
}
And in config folder boilerplate.php file contain forgot_password rules :
'forgot_password' => [
'validation_rules' => [
'email' => 'required|email',
'phone' => 'required|min:11|numeric'
]
],
When email and phone null it returns only email validation not phone:
{
"error": {
"message": "422 Unprocessable Entity",
"errors": {
"email": [
"The email field is required."
]
},
"status_code": 422
}
}
What i am doing wrong ? It is related to cookies ?
There is nothing going wrong here.
Laravel checks whether the email field is properly filled in. It isn't, Laravel breaks out of the validation method and returns the exception.
If you fill in the email field, but leave the phone field blank, then the validation response will be about the phone field.

Better way for testing validation errors

I'm testing a form where user must introduce some text between let's say 100 and 500 characters.
I use to emulate the user input:
$this->actingAs($user)
->visit('myweb/create')
->type($this->faker->text(1000),'description')
->press('Save')
->see('greater than');
Here I'm looking for the greater than piece of text in the response... It depends on the translation specified for that validation error.
How could do the same test without having to depend on the text of the validation error and do it depending only on the error itself?
Controller:
public function store(Request $request)
{
$success = doStuff($request);
if ($success){
Flash::success('Created');
} else {
Flash::error('Fail');
}
return Redirect::back():
}
dd(Session::all()):
`array:3 [
"_token" => "ONoTlU2w7Ii2Npbr27dH5WSXolw6qpQncavQn72e"
"_sf2_meta" => array:3 [
"u" => 1453141086
"c" => 1453141086
"l" => "0"
]
"flash" => array:2 [
"old" => []
"new" => []
]
]
you can do it like so -
$this->assertSessionHas('flash_notification.level', 'danger'); if you are looking for a particular error or success key.
or use
$this->assertSessionHasErrors();
I think there is more clear way to get an exact error message from session.
/** #var ViewErrorBag $errors */
$errors = request()->session()->get('errors');
/** #var array $messages */
$messages = $errors->getBag('default')->getMessages();
$emailErrorMessage = array_shift($messages['email']);
$this->assertEquals('Already in use', $emailErrorMessage);
Pre-requirements: code was tested on Laravel Framework 5.5.14
get the MessageBag object from from session erros and get all the validation error names using $errors->get('name')
$errors = session('errors');
$this->assertSessionHasErrors();
$this->assertEquals($errors->get('name')[0],"The title field is required.");
This works for Laravel 5 +
Your test doesn't have a post call. Here is an example using Jeffery Way's flash package
Controller:
public function store(Request $request, Post $post)
{
$post->fill($request->all());
$post->user_id = $request->user()->id;
$created = false;
try {
$created = $post->save();
} catch (ValidationException $e) {
flash()->error($e->getErrors()->all());
}
if ($created) {
flash()->success('New post has been created.');
}
return back();
}
Test:
public function testStoreSuccess()
{
$data = [
'title' => 'A dog is fit',
'status' => 'active',
'excerpt' => 'Farm dog',
'content' => 'blah blah blah',
];
$this->call('POST', 'post', $data);
$this->assertTrue(Post::where($data)->exists());
$this->assertResponseStatus(302);
$this->assertSessionHas('flash_notification.level', 'success');
$this->assertSessionHas('flash_notification.message', 'New post has been created.');
}
try to split your tests into units, say if you testing a controller function
you may catch valication exception, like so:
} catch (ValidationException $ex) {
if it was generated manually, this is how it should be generated:
throw ValidationException::withMessages([
'abc' => ['my message'],
])->status(400);
you can assert it liks so
$this->assertSame('my message', $ex->errors()['abc'][0]);
if you cannot catch it, but prefer testing routs like so:
$response = $this->json('POST', route('user-post'), [
'name' => $faker->name,
'email' => $faker->email,
]);
then you use $response to assert that the validation has happened, like so
$this->assertSame($response->errors->{'name'}[0], 'The name field is required.');
PS
in the example I used
$faker = \Faker\Factory::create();
ValidationException is used liks this
use Illuminate\Validation\ValidationException;
just remind you that you don't have to generate exceptions manually, use validate method for common cases:
$request->validate(['name' => [
'required',
],
]);
my current laravel version is 5.7

Resources