I want to make a login in Laravel that receives only one parameter, i.e. has a simple form that receives one input, and based on that input, the user is authenticated. If user is admin, it will redirect to /adminpage, else if the user is a regular user it redirects to /homepage.
I want to use a custom table, model, and controller. I searched the internet but could not find a solution.
EDIT I have form like this:
<form action="someroute" method="POST">
<input type="text" name="rf_id">
<input type="submit" value="login">
</form>
My migration is:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('lastname');
$table->string('workid');
$table->boolean('isAdmin')->default(False);
$table->string('rf_id');//
$table->timestamps();
});
Now i need controller that handle this. So base on rf_id, controller needs to fin user and thec check his role. I try this but don't work:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class TestController extends Controller
{
public function index(){
return view('singin');
}
public function login(Request $request){
$credentials = $request->only('rf_id');
if(Auth::attempt($credentials)){
//we need to know who is logging in
return $this->filterAndRedirect(Auth::user());
}
return "there is no user with that rfid";
}
private function filterAndRedirect(User $user){
//the function to take care of the routing
if($user->isAdmin){
# return redirect('/homepage');
return "This is admin";
}
else{
# return redirect('/adminpage');
return "This is worker";
}
}
}
Here is how I would do it;
First of all you have to add the table column in your migration file for the user-type and the login parameter.Check below for migration docs
Next is the page where you have the form, I don't know what your parameter is so I'll just call it 'param'. I am assuming you know how to create and submit forms with laravel so I won't put the code here.
Now the interesting part, the controllers:
This is how my user creation in the RegisterController would look;
Note that I'm using the standard laravel auth controllers
If you are gonna have a different form for the Admin registration the function to add the admin user will roughly be the same;
attempt()function to authenticate the user and start a new session for them using your single parameter.You will have these functions
The Relevant Documentation:Database Migration Docs, Laravel Authentication Docs
Suggestion:If I were you I would use Roles/Permissions as opposed to using a table row in your DB, I think its more robust. Check out this library it is well documented and gets updated ->Laravel Permission By Spatie
EDIT:This answer assumes you are using the standard User model and Authentication Controllers because thats the whole point of using a framework isn't it?