How to customize make:auth in laravel 5.4? - laravel

make:auth generated register page accepts name,email,password .
i would like to add additional fields dateofbirth,address.
How can i add more fields?

Override the default register() method in RegisterController. If you don't see any register() method, create a new one and write your code to register a new user.

You should
add the fields you need in the database migration file,
edit the blade view files (directory resources/views/auth)
edit the RegisterController create() function and maybe the validator() too. (directory app/Http/Controllers/Auth)

Related

Get route by name in different namespace

Structury of folders:
-App
-CustomClasses
Menu.php
-Http
-Controllers
TestController.php
When i use Route::getRoutes() from my TestController.php i've got all routes, but when i use same method in Menu.php result is empty. Therefore route('route-name') which i need also doesn't work correctly. I think it might be because of different namespaces, but is there any simple way to get route url by route name in Menu.php or any other file outside Controllers folder?
After some time i found solution myself. In app/Providers/AppServiceProvider.php there are two methods: register() and boot(). I created instance of Menu object in boot() method, but still received empty route list. I notticed that in config/app.php there is a list of service providers, but order is important here.
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
As you can see AppSeriveProvider is before RouteServiceProvider which means Route's boot method is run last, so there are no routes yet if i try to get them in Menu object which is creater in AppServiceProvider.
My solution is to create MenuServiceProvider and load it after Route's provider instead create it in AppServiceProvider.

How to change /home after login in a laravel project

I am new in laravel framework. I install the Auth Class in my project. So when I login in my project it goes to dashboard but url is ('/home'). I want to change this path and after login I want that it goes ('/dashboard'). For that jobs, which file I want to change? I have find 4 file where /home is decleared. Thats are web.php, LoginController.php, HomeController.php, RedirectIfAuthenticated.php. Which file I will change? or any more file there?
Web.php file
homeController.php file
loginController.php file
redirectIfAuthenticated.php file
There's now an easier way to do this in Laravel 6.X.
Simply change the constant in theapp/Providers/RouteServiceProvider.php file.
/**
* The path to the "home" route for your application.
*
* #var string
*/
public const HOME = '/new-url';
After that, change your route in your routes/web.php file.
Route::get('/new-url', 'Controller#method');
// If you don't want to use the HomeController, be sure to include the auth middleware.
Route::get('/new-url', 'Controller#method')->middleware('auth');
You can delete the HomeController if you're not using it in your route, won't cause any issues.
You have to add the redirectTo property to the LoginController, RegisterController, and ResetPasswordController files:
protected $redirectTo = '/';
It is well explained in the Laravel documentation:
https://laravel.com/docs/5.5/authentication#authentication-quickstart
I have come across this before... change the protected
$redirecto = /home to / in the login controller, then got to routes->web and change the Route::get('/home') to Route::get('/')->name('dashboard')
or you can just change $redirecto = "/dashboard" in the login controller and make sure that you create/update a path in the routes.
Hope it helps.

Laravel update Session manually

I am using Laravel 5.4 and I am using builtin Auth so Logging in - out comping behind the scene and it's storing the session automatically.
How can I add more information to my session ?
If you don't want to build a custom Login function, you could go to
AuthenticatesUsers file which handle this task and inside method called
authenticated(Request $request, $user)
You can add whatever you want.
This file can be found in
vendor->Laravel->framework->src->Illuminate->Foundation->Auth
Nour answer works but it's better if dont write code directly in vendor files, it can be overwritten by any composer update.
Instead you can add a function authenticated(Request $request, $user) directly in your App\Http\Controllers\Auth\LoginController and write your custom code that fires on every successful authentication.
Whatever you store in your user table will be stored in session data after log in you just need to access it like this
Auth::user()->column_name

laravel modify how the password is checked in the authentication process

I am using laravel 5.2.
I would like to be able to change a parameter in the .env file like PASSWORD_VALIDATION=...
This could be either LARAVEL or PERSONAL.
If it is LARAVEL then it would use the standard Laravel authentication method with users and passwords in the db. But if I use PERSONAL, I would like that it uses a function I have created that will check if the email address is in the database then verify the password provided with the Active Directory in my company.
I looked at the various files and I can see I have:
app\Http\Controllers\Auth\AuthController.php
In there, I can see:
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
In this file:
vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php
It uses
use AuthenticatesUsers, RegistersUsers {
AuthenticatesUsers::redirectPath insteadof RegistersUsers;
AuthenticatesUsers::getGuard insteadof RegistersUsers;
So I can see in the file
vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
My function to be changed which is:
public function postLogin(Request $request)
{
return $this->login($request);
}
I have tried to copy this one in my file app\Http\Controllers\Auth\AuthController.php but it doesn't change anything if I modify what is inside it...
Thanks
Ok, I found out why, it seems that in my routes, it is pointing to #login and not to #postLogin hence any modification applied to the function postLogin wouldn't do anything

Laravel 5 - Is there a way to use built-in authentication but disable registration?

I am building an administrative back-end and thus need to hide public user registration. It appears that if you want to use the built-in Illuminate authentication you need to add
use AuthenticatesAndRegistersUsers to your controller definition. This trait is defined here.
It appears as if it is impossible to disable registration if you want to use the built-in auth handlers... can someone show me wrong?
I'm using Laravel 5.2+ and I found that if you remove the Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers and use just Illuminate\Foundation\Auth\AuthenticatesUsers does the trick too.
Though /register is still accessible and will throw a fatal error.
This page talks about overriding the auth controller. Its worth a read, at a basic level it seems you can add the following lines to app\Http\Controllers\Auth\AuthController.php :
public function getRegister() {
return redirect('/');
}
public function postRegister() {
return redirect('/');
}
So if a user accesses the registration url it will redirect them away to a place of your choosing.
You can have your own form of registration. The only thing Laravel does is make it easy to authenticate on a users table because they create the model, build the db schema for users and provide helper methods to authenticate on that model/table.
You don't have to have a view hitting the registration page... But if you want to use the built in auth you still need to use (or set) a Model and a driver for database connections.
You can just remove that view and/or controller method from the route that links to the registration view and create your own (or seed the database manually).
But, no, you cannot forgo using Eloquent, and the User model and expect to use built in auth. Built in authentication requires that you specify settings in /config/auth.php. You may specific a different model (other than User) and you may specify a different table, but you cannot forgo the configuration completely.
Laravel is very customizable though, so you can achieve what you are looking to do... plus why not use Eloquent, it's nice.
Based on #shoo's answer, working with Laravel 5.2
Add the following lines to app\Http\Controllers\Auth\AuthController.php :
public function showRegistrationForm() {
return redirect('/');
}
public function register() {
return redirect('/');
}

Resources