I have been struggling with creating a second authentication alongside with the out-of-the-box authentication in Laravel.
I used themake:auth
command fromartisan
and used my custom model as provider and also created a guard for my custom model.
My issue is that the user does not get authenticated after a successful login.
So in other words, when I try to retrieve the user with:Auth::user()
orAuth::guard('employee')->user()
it just gives me null and I cannot understand why this is happening.
The thing is that I get redirected correctly but I don't know if its the session or anything else that is not working? Correct me if I am wrong
My employee model:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Angestellter extends Authenticatable
{
use HasFactory;
public $timestamps = false;
protected $table = 'angestellter';
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'friseurkuerzel',
'vorname',
'nachname',
'email',
'password',
'ist_admin',
'erstelldatum',
'friseursalon_id',
];
}
My guard config from /config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'employee' => [
'driver' => 'session',
'provider' => 'employees',
]
],
My AdminLoginController where I log in my user:
$this->validate($request, $rules);
$remember = $request->get('remember');
if (Auth::guard('employee')->attempt([
'email' => $request->get('email'),
'password' => $request->get('password'),
], $remember)) {
//Authentication passed...
Auth::guard('employee')->login(Auth::guard('employee')->user());
return redirect()->to(route('admin.home'))->send();
}
After theattempt()
method succeeds, I am able todd()
my Auth user with:Auth::guard('employee')->user()
But after the redirect, the Auth user is null wherever Idd()
him.
I have just for the sake of testing tried to access him on my admin.home view viadd()
after a successful redirect.
With what you currently have you can make some minor adjustments to fix that up.Auth::guard('employee')->attempt([...])
is attempting to login the user so when you get inside theif
block the user is already logged in so you don't need to retrieve them from the guard and log them in again. When returning Responses you do not need to call thesend
method on the Response. This is handled after the Response gets out of the Kernel; it is the first to last statement in the script,index.php
, that loads your application:
$response = $kernel->handle(
$request = Request::capture()
)->send();
If you callsend
on a Response while you are still in the middleware stack (which you are if you are in a route action) you are returning headers too early, flushing the response content to the client and finishing the request. Anything else in the middleware stack on the way out can not add headers to the Response that will end up being sent. Once thesend
method is called it won't send headers again. This means the cookies that the StartSession and EncryptCookies middleware add won't be sent.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Foundation, similar to Bootstrap, has become very popular as a more complex framework with some advanced but easy-to-implement CSS components. It is built with Sass, so just like Bootstrap, it is customizable. In addition to this, it also boasts some features that help make the design mobile responsive.
https://get.foundation/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.