Laravel authentication using mobile number - laravel

I'm currently using Laravel 5.5, as you know Laravel by default offers authentication by email, but in my case I want to change its behaviour and allow the user to login using the mobile number. so Where would I have to do the modifications?

From the docs:
By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:
public function username()
{
return 'mobile_number';
}
https://laravel.com/docs/5.5/authentication#included-authenticating

LoginController
public function username()
{
return 'mobile';
}
also can validate
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
]);
}

Related

Laravel multi authetification with different users tables

I'm trying to build a multiple authentification in laravel with different tables (2 tables) for admin and user. The problème is that the registration and login forms work only with default auth login/register.
I've tried some examples form web tutorials but it didn't work.
HomeController.php:
public function __construct() {
$this->middleware('auth');
}
public function index() {
return view('home');
}
I have added createAdmin function in "Auth/RegisterController.php":
protected function createAdmin(array $data)
{
$this->validator($data->all())->validate();
$admin = Admin::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
return redirect()->intended('login/admin');
}
I have changed email validation rules to:
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'|'unique:admins']
And the route (web.php) is:
Route::post('/register/admin', 'Auth\RegisterController#createAdmin');
When I fill admin register credentials and click register button I get this message:
Symfony\Component\Debug\Exception\FatalThrowableError Too few arguments to function App\Http\Controllers\Auth\RegisterController::createAdmin(), 0 passed and exactly 1 expected
The error is coming from the array $data parameter in your createAdmin() controller method.
Usually, you want to use one of two types of parameters in your controller methods: route parameters or injected dependencies. The $data parameter isn't matching either of those, so Laravel doesn't know to provide it.
If you'd like to access the request (POST) data in the controller, you can either ask for an instance of Illuminate\Http\Request as a parameter:
// Import it at the top of your PHP file
use Illuminate\Http\Request;
// Then your updated method:
public function createAdmin(Request $request)
{
$data = $request->all();
// ...
}
Or, use the request() helper directly:
public function createAdmin()
{
$data = request()->all();
// ...
}

Laravel login with more custom fields

I want a user to login using his/her id_number(school id number) only. Or if he forgets his id number, I want him to login using his Name, course and year graduated.
Is it possible to implement this using Laravel auth?
I am really new to laravel. I would appreciate if you could help me.
Thank you
After you use php artisan make:auth
You can see in LoginController, there is a trait AuthenticatesUsers. The default auth is on AuthenticatesUsers. It can be modify by your own function by overriding validateLogin, attemptLogin, credentials , etc...
for example
protected function validateLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|string',
'ic_number' => 'required|string',
]);
}
protected function credentials(Request $request)
{
return $request->only('username', 'ic_number');
}

remove validation from email parameter in laravel 5.6. I am using authenticate() method

I am trying to authenticate the using using ajax and laravel 5.6 authenticate() method. If I pass 'email' and 'password' parameters then it works fine but If I change the email parameter to user_email then it gives me validation that "Email field is required".
So I need help to authenticate the user using different parameters (like user_email etc.) instead of email and password.
Here is the code file path app\Http\Controllers\AUth\LoginController.php
protected function authenticate(Request $request, $user){
if(!request->ajax()){
return response()->json([
'auth' => auth()->check(),
'user' => $user,
'intented' => $this->redirectPath()
]);
}
}
Thank you
Paste this on your login controller
public function username()
{
return 'user_email';
}

Auth::user() returns null on Module __construct()

I created a new Module named Article using laravel-modules. Some backend routes needed authentication and i added auth middleware and an additional permission view_backend. I am using https://github.com/spatie/laravel-permission package for role-permissions.
the issue is when i try to access the route admin/article/posts it prompts me the login as expected. But after login it show null on __construct() method for Auth::user();
I added web middleware as mentioned on #204 but it did not solve the issue. Can you please guide me to resolve this? My project is on Laravel 5.6 and using the latest version of Laravel-Modules
Route::group(['namespace' => 'Modules\Article\Http\Controllers\Backend', 'as' => 'backend.article.', 'middleware' => ['web', 'auth', 'can:view_backend'], 'prefix' => 'admin/article'], function () {
Route::resource("posts", "PostsController");
});
My project is hosted at Github, https://github.com/nasirkhan/laravel-starter/tree/module
First of all, add Spatie Middleware to your kernel:
protected $routeMiddleware = [
// ...
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
];
Then in your controller check for permission or roles:
public function __construct(Request $request)
{
$this->middleware(['permission: order.index']);
}
Now you can access to your authenticated with $request->user() like:
public function create(Request $request)
{
if ($request->user()->hasRole('admin')) {
// return view("carmodel.create", ["manufacturers"=>$manufacturers]);
} else {
return view("admin.error", ['code'=>'001','msg'=>'err']);
}
}
According to the docs:
In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.
As an alternative, you may define a Closure based middleware directly
in your controller's constructor. Before using this feature, make sure
that your application is running Laravel 5.3.4 or above:
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->projects = Auth::user()->projects;
return $next($request);
});
}
Or you could typehint it:
public function index(Request $request)
{
$projects = $request->user()->projects;
$value = $request->session()->get('key');
}
Docs

changing the authentication parameters in Laravel 5.3 with Hesto multi Auth

I wan't to change the login parameters in authentication. I'm using the Hesto Multi Auth package.
By default Hesto package gives a multi authentication with email and a password. Instead what i wan't to do is change it to a local identity number and the password.
Hesto_Package
It is quite simple.
All i had to do is override the login() function in the required model's LoginController Class. If the Modeis admin then in adminAuth -> LoginController.
public function login(Request $request)
{
echo $request->NIC;
if (Auth::guard('admin')->attempt(['NIC' => $request->NIC, 'password' => $request->password])) {
//
}
}
then also add the field in Models class file.4
protected $fillable = [
'NIC', 'email', 'password',
];

Resources