The Laravel offers a practical way to create the entire authentication base, using the following command:
php artisan make:auth
If you want to do this manual authentication, in the function you want to authenticate enter the following code:
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
//
public function login(Request $request){
$credenciais = $request->only('email', 'password');
if (Auth::attempt($credenciais)) {
// após autenticar o usuário
}
}
To check if the user is logged in use the following function
use Illuminate\Support\Facades\Auth;
if (Auth::check()) {
// The user is logged in...
}
You can also check if the user is authenticated via middleware in the route file
Route::get('perfil', function () {
// usuário autenticado
})->middleware('auth.basic');
or
Route::group(['middleware' => ['auth']], function() {
Route::get('perfil', function () {
// usuário autenticado
});
});
You can access the documentation of the Standard in the Authentication session, to make any type of variation.
So would it be okay if I didn’t half of the project use this command? It’s a job I’m doing for college.
– spider tek
It would be possible to do without this make::auth command?
– spider tek
Would Joao, I will put an example.
– André Lins
I put an example of how to manually authenticate
– André Lins
In the case 'email' and 'password' would be the form fields?
– spider tek
That Joao, are the fields
– André Lins
And in case I want to get the id of the user who is logged in to insert into another table. And how would it be regarding the session?
– spider tek
Enter the user id inside the credentials before using the
Auth::attempt($credenciais)
– André Lins
In case it worked manual login, but I need to insert in the views something?
– spider tek
Through a middleware you validate the routes without actually needing to put on the screen.
– André Lins
It is because I can access other pages without being logged in
– spider tek
Where I insert the middleware?
– spider tek
Route archive
– André Lins