Because you are not logging in? You will need to show that code if you want help
User is redirected but not logged in (no auth)
I'm using Laravel Auth (php artisan make::auth), but I customized it so it would log in with an username (instead of e-mail) and connect to an external database with a different structure (instead of default localhost).
I enter my username and password, I get redirected to my /dashboard page - but I'm actually not logged in! I can't access any page with auth middleware (I get redirected back to login) + when I check with:
@if (Auth::check())
Logged in
@else
Not logged in
@endif
It also displays that I'm not logged in. So.. even after being successfully directed after login, it seems that I'm not authenticated. Why could this be happening?
I'm not using any custom login authentication, using the default provided by
php artisan make::auth
As I understood from the wiki, this should take care of that logic, and log the user in without editing anything? Or does this just provide basic redirections and I have to make the login logic myself?
Anyway, here's my (almost) default LoginController:
LoginController
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
public function username()
{
return 'username';
}
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
User model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'PLAYERS2';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
I overwrote the default login function from AuthenticatesUsers, and replaced it with my on function in LoginController:
public function login() {
if(!auth()->attempt(request(['username', 'password']))) {
return back();
}
return redirect('dashboard');
}
Once again, I get redirected correctly to dashboard, but the auth didn't work, as I still cant access pages with 'auth' middleware and (Auth::check()) says I'm not logged in.
['username', 'password']
What is that, is the user username, and the password is password?
That's the data that comes from the login page (username and password):
<input type="text" name="username" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }}" placeholder="Username" value="{{ old('username') }}" required autofocus>
<input type="password" name="password" class="form-control {{ $errors->has('password') ? ' is-invalid' : '' }}" placeholder="Password" required>
Thought it was
if (Auth::attempt(['email' => $email, 'password' => $password])) {
I could be wrong.
I guess I'm old fashioned, I get each in variables.
You could probably (from docs)
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
Used your example - it's what I had before but just in different syntax. With wrong credentials I won't get logged in - with a correct user I get "logged in" and redirected to the dashboard, but in reality I'm still not logged in - like before.
The reality is that it won't work with the default Auth or with my overwritten functions, mistake is somewhere else..
Most likely then something wrong with session maintenance. Check if session files are being created in the storage/framework/sessions folder
Yes, a session is created:
a:4:{s:6:"_token";s:40:"1VOGX98bqnIjwWSPkWli2EoRRpfxyud6iZLFXGAL";s:9:"_previous";a:1:{s:3:"url";s:31:"http://localhost:8000/dashboard";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:50:"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d";N;}
And a video to prove that I'm not crazy and what's exactly happening: https://streamable.com/9b8c2
In tinker, can you run config('session') and then paste the result?
=> [
"driver" => "file",
"lifetime" => "120",
"expire_on_close" => false,
"encrypt" => false,
"files" => "C:\Users\Arvut\Desktop\laravel\storage\framework/sessions",
"connection" => null,
"table" => "sessions",
"store" => null,
"lottery" => [
2,
100,
],
"cookie" => "laravel_session",
"path" => "/",
"domain" => null,
"secure" => false,
"http_only" => true,
"same_site" => null,
]
>>>
This is quite a fresh installation of Laravel, installed few days ago. I have only added templates and the login system.
All looks ok there. Must be some reason for your browser to not provide the authenticated session? I seem to be drawing a blank.
Do all your scripts terminate correctly (no dd at any point) (I didn't notice anything in your video)
Yes, everything working like it should. The only thing different from documentation examples provided is that I'm not using a local database - so my suspicion is that the fault is somewhere there? Or in the user model? I don't know how, though.
I'm connecting to an already set up database on my VPS and it currently looks like this: https://i.imgur.com/GC28rTE.png https://i.imgur.com/yuXfSuB.png
changed IP in .env, and table in user model:
protected $table = 'PLAYERS2';
Can't think of anything else now..
If the DB was wrong you would not be authenticated.
Please or to participate in this conversation.