Creating and checking session - Laravel

Asked

Viewed 622 times

1

Good evening guys, I’m new using Laravel and I was creating something basic products, and I’m having doubts regarding the use of session in Laravel. After the user logs in, as I can create the session for him, use his data on other pages. Also, as I could do to check on each page, whether there is a session or not. Thank you

1 answer

3


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.

  • It would be possible to do without this make::auth command?

  • Would Joao, I will put an example.

  • I put an example of how to manually authenticate

  • 1

    In the case 'email' and 'password' would be the form fields?

  • That Joao, are the fields

  • 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?

  • Enter the user id inside the credentials before using the Auth::attempt($credenciais)

  • In case it worked manual login, but I need to insert in the views something?

  • Through a middleware you validate the routes without actually needing to put on the screen.

  • It is because I can access other pages without being logged in

  • Where I insert the middleware?

  • Route archive

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.