What is authentication
Authentication is the mechanism for confirming “who” the user accessing your application is. In web applications, you receive an email and password via a login form, and if they are correct, save the user’s information in the session. On subsequent requests, that session is used to identify the user. Laravel’s authentication features are built around two concepts: “guards” and “providers.”- Guards define how the user is authenticated on each request. The default is the
sessionguard, which uses sessions and cookies to manage state. - Providers define how users are retrieved from your database. The default uses Eloquent.
The authentication configuration file is
config/auth.php. The default configuration works for most web applications as-is.Authentication with starter kits
In Laravel, by choosing a starter kit when creating your application withlaravel new, authentication features like login, registration, and password reset are automatically built for you. This is the most recommended approach.
1
Create the application
Create your application with the Laravel installer. It will prompt you to choose a starter kit during setup.You can select React, Vue, Livewire, or Svelte as your starter kit.
Choose one that matches your team’s technology stack.
2
Install frontend dependencies
3
Prepare the database
Verify the database configuration in your The initial migrations, including the
.env file, then run migrations.users table, are applied.4
Start the development server
http://localhost:8000 in your browser, and you’ll see “Register” and “Log in” links in the navigation.
Visit /register and try registering a user.Available starter kits
React
Build a modern SPA using React 19, TypeScript, Tailwind, and shadcn/ui. By using Inertia, you can use a React frontend while retaining server-side routing.Vue
Uses Vue Composition API, TypeScript, Tailwind, and shadcn-vue. Like React, it uses Inertia to integrate with the server.Livewire
Uses Livewire to build dynamic UIs entirely in PHP. Ideal for teams that primarily work with Blade templates or want to avoid JavaScript frameworks. Includes the Flux UI component library.Svelte
Uses Svelte 5, TypeScript, Tailwind, and shadcn-svelte. Combines with Inertia to build a modern SPA.The Auth facade
Using theAuth facade, you can get information about the currently authenticated user and check the authentication state.
Getting the authenticated user
Request object.
Checking authentication state
Auth::check() returns true or false depending on whether the user is logged in.
@auth and @guest directives are convenient.
Protecting routes
To create routes that only authenticated users can access, use theauth middleware.
/login.
To protect multiple routes at once, use groups.
Guest-only routes
Use theguest middleware to redirect users who are already logged in.
Applying it to login and registration pages redirects already-logged-in users to the dashboard.
Manual authentication
To implement login manually without using a starter kit, useAuth::attempt().
Auth::attempt().
The password is automatically compared against the hash, so pass it in plaintext.
To implement “remember me” functionality, pass true as the second argument.
Even when implementing manual authentication, we recommend referring to the starter kit’s code. It’s a great reference for a secure implementation.
Logout
To log a user out, callAuth::logout().
It’s a best practice to also invalidate the session and regenerate the CSRF token.
Summary
Next steps
Middleware
Learn about how the
auth middleware works and how to build your own middleware.