Laravel 5.4 additional Auth parameters - laravel-5

I have added an email verification to the register process with Laravel 5.4. I am using the built-in Auth configuration as well. I want to now add a "verified" parameter to the authentication process. This used to work in earlier versions of 5.x, but I can't get it to work now.
Editing this file:
project\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
I would normally add the "verified" portion of the Login validation.
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->loginUsername() => 'required',
'password' => 'required',
'verified' => 1,
]);
}
This doesn't appear to be working in 5.4 now. I can login without verifed being true. Is there another way I can change this without touching any back-end classes or traits? Can I do this in the LoginController instead to make it easier to persist across Laravel upgrades?

Yes you can do it. Add this to your LoginController
use Illuminate\Http\Request;
protected function credentials(Request $request)
{
return array_merge($request->only($this->username(), 'password'), ['verified' => 1]);
}
Never modify the core files. You can always override the core class methods in your controller.

Related

Laravel - Request safe method does not exist

I generated my StorePostRequest using artisan make command.
I defined rules on the rules method doing this:
public function rules()
{
return [
'title' => 'required|min:3|max:255',
'slug' => ['required', Rule::unique('posts', 'slug')],
'thumbnail' =>'required|image',
'excerpt' => 'required|min:3',
'body' => 'required|min:3',
'category_id' => 'required|exists:categories,id'
];
}
However, in my PostController, I'm not able to get validated inputs except thumbnail using the safe()->except('thumbnail') like explained here
I'm getting the error
BadMethodCallException
Method App\Http\Requests\StorePostRequest::safe does not exist.
Check your laravel/framework version by running
php artisan --version
The safe method found on the FormRequest class was only added in version 8.55.0.
Just good to keep in mind that just because you're on a version 8 of laravel framework, that doesn't mean you'll have all methods and properties found in the laravel 8.x docs. That is unless you're on the current latest version 8 of course.
Using the except() method directly on $request worked. Thanks to #JEJ for his help.
$request->except('thumbnail');

Laravel validation rules shared between Form request and Validator in command

I have a set of validation rules in a FormRequest, Laravel 6.X like so:
{
return [
'rule1' => 'required|numeric',
'rule2' => 'required|numeric',
...,
'ruleN.*.rule1' => ''required|string|max:50'
];
}
This works just fine for every AJAX HTTP request against the endpoint for it was created. However, I also need to run the same process (Validate this time not against a Request but an array of inputs) via a CLI command, where this FormRequest cannot be used, as it won't be a Request.
By trying to inject the FormRequest into a Command like:
public function handle(CustomFormRequest $validator)
{
...
}
the command execution always fails (Of course), on every run, as the type of input isn't a Request.
Googling a bit I ended up with a solution of the shape of (Rules will be the same ofc):
$validator = Validator::make([
'rule1' => $firstName,
'rule2' => $lastName,
], [
'rule1' => ['required'],
'rule2' => ['required'],
]);
However I'd like to share the set of rules in a common place (A file for example) and that both the FormRequest AND the Validator class can take the same rules, without writing in both places (Form Request and Command), to keep them the same under any changes.
Is there way of writing the "rules" array in a common place (A file for example) and import them into both places? Maybe a base class would work, but it doesn't look right to do it, as they don't share anything else than that. I thought of a Trait, but I'm hesitant tht could be the best solution.

How to get Laravel Login query? I can't pass the login page

I wanna get the Login query because I have some problem on login page.
Actually there's no error when I used Laravel Migrations (the login system worked at all), after a day, I tried again but using SQL Manually and got a problem.
I wanna know what does Laravel do with sql query, seems laravel do query on wrong fields.
I've tried to use DD($request->all()); and the output like this
Die Dump output
^ If you notice at the password, it doesn't encrypted.
But in my database, it's encrypted
password field
And this happen if i didn't use DD
Login credentials do not match
function on LoginController.php for login using username or email, i found this code from this tutorial
protected function validateLogin(Request $request)
{
$this->validate(
$request,
[
'identity' => 'required|string',
'password' => 'required|string',
],
[
'identity.required' => 'Username or email is required',
'password.required' => 'Password is required',
]
);
// dd($request->all());
}
/**
* #param Request $request
* #throws ValidationException
*/
protected function sendFailedLoginResponse(Request $request)
{
$request->session()->flash('login_error', trans('auth.failed'));
throw ValidationException::withMessages(
[
'error' => [trans('auth.failed')],
]
);
}
You can try something mentioned here: https://laracasts.com/discuss/channels/general-discussion/authenticate-on-custom-fields-laravel-53
basically laravel is looking for a specific field name while using default auth, but since yours differ from that you have to change them manually
Please try to use laravel's default authenticatication using this command
php artisan make:auth

Laravel request validation error

Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given, called in app\Http\Controllers\RegistrationController.php on line 23
It doesn't work here
Registration Controller
but at the same time works fine in another controller
AuthController
The reason you're getting this error is because you're passing your validation rules to the request() helper function and not as the 2nd param to $this->validate()
You can still use the request() helper function but you just need to do:
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'password|confirmed', //<-- Is the password rule something you've created?!?
]);
Hope this helps!
function store()should be function store(Request $request) if you want to use the request. However #CBroe is right: please learn to ask your questions better.

Laravel 5.2 Auth::login($user) not working

I am writing a Laravel 5.2 application. I need to manually login the user for which I am using \Auth::login($user). I am doing it in following way.
if ($user = User::where('phone',session('phone'))->first())
{
\Auth::login($user);
// \Auth::loginUsingId($user->id);
// Auth::attempt(['email' => $user->email, 'password' => 'password']);
$data = \Auth::user(); //returning correct results
}
I have tried all the options namely Auth::login($user), Authh:loginUsingId($user->id) and attempt method. These methods are working fine as the $data variable is storing the object of correct user. But the problem is when I move to other route say '/home' the user remain no more authenticated.
What might be the wrong here? How could I do it correctly?
Since Laravel 5.2, you have to attach all your routes that need session with the 'web' middleware. See your app/Http/Kernel.php, the 'web' middleware contains the \Illuminate\Session\Middleware\StartSession.
In routes you have to use web in laravel 5.2
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('/', 'HomeController#index');
Route::get('/profile', 'HomeController#profile');
});

Resources