英文:
Auth::login() dont work in laravel with driver sqlsrv
问题
I have been stuck for several days with a problem, I am unable to authenticate a user in a Laravel 8.x project using the sqlsrv driver. Before mentioning all the details I will tell you.
- The Laravel project is version 8.x.
- The SQL Server I am connecting to is version 2017 Express.
- The php version I'm using is 8.2.0.
- Add the extensions (extension=php_pdo_sqlsrv_82_ts_x64
extension=php_sqlsrv_82_ts_x64) in the php.ini file. - The connection from Laravel to SQL Server is correct (because it returns data to me).
Now the code:
-
config\database file:
'default' => env('DB_CONNECTION', 'sqlsrv'),
'connections' => [
'sqlite' => [...],
'mysql' => [...],
'pgsql' => [...],
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
], -
.env
DB_CONNECTION=sqlsrv
DB_HOST=PC01\SQLEXPRESS
DB_PORT=1433
DB_DATABASE=SOLUCION
DB_USERNAME=sa
DB_PASSWORD=miclave -
config/auth file:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Usuario::class,
],
], -
app/Models/Usuario file:
'integer',
'NOMBRE' => 'string',
'CLAVE' => 'string',
'ESTA_ACTIVO' => 'boolean'
];
} -
app/Http/Controllers/SistemaController file:
get('usuario')],
['CLAVE', '=', $request->get('clave')],
['ESTA_ACTIVO', '=', true]
])->first();if ($usuario != null) {
Auth::login($usuario); // This statement is not working, because if I then do dd(Auth::id()) -> it returns null when it should return the user ID$request->session()->regenerate();
return redirect()->intended('/pedido');
}return back()->withErrors([
'clave' => 'LAS CREDENCIALES SON INCORRECTAS',
]);
}
}
I have printed the variable $usuario (with this command dd($usuario)) to see if it has a value and this is the result.
In other words, my $usuario variable does have a value, but for some reason I can't authenticate the user. I'd appreciate your help.
Greetings.
英文:
I have been stuck for several days with a problem, I am unable to authenticate a user in a Laravel 8.x project using the sqlsrv driver. Before mentioning all the details I will tell you.
- The Laravel project is version 8.x.
- The SQL Server I am connecting to is version 2017 Express.
- The php version I'm using is 8.2.0.
- Add the extensions (extension=php_pdo_sqlsrv_82_ts_x64
extension=php_sqlsrv_82_ts_x64) in the php.ini file. - The connection from Laravel to SQL Server is correct (because it returns data to me).
Now the code:
- config\database file:
> 'default' => env('DB_CONNECTION', 'sqlsrv'),
> 'connections' => [
> 'sqlite' => [...],
> 'mysql' => [...],
> 'pgsql' => [...],
> 'sqlsrv' => [
> 'driver' => 'sqlsrv',
> 'url' => env('DATABASE_URL'),
> 'host' => env('DB_HOST', 'localhost'),
> 'port' => env('DB_PORT', '1433'),
> 'database' => env('DB_DATABASE', 'forge'),
> 'username' => env('DB_USERNAME', 'forge'),
> 'password' => env('DB_PASSWORD', ''),
> 'charset' => 'utf8',
> 'prefix' => '',
> 'prefix_indexes' => true,
> ],
> ],
- .env
> DB_CONNECTION=sqlsrv
> DB_HOST=PC01\SQLEXPRESS
> DB_PORT=1433
> DB_DATABASE=SOLUCION
> DB_USERNAME=sa
> DB_PASSWORD=miclave
- config/auth file:
> 'providers' => [
> 'users' => [
> 'driver' => 'eloquent',
> 'model' => App\Models\Usuario::class,
> ],
> ],
- app/Models/Usuario file:
> <?php
>
> namespace App\Models;
>
> use Illuminate\Foundation\Auth\User as Authenticatable;
> use Illuminate\Database\Eloquent\Factories\HasFactory;
> use Illuminate\Database\Eloquent\Model;
> use Illuminate\Notifications\Notifiable;
> use Laravel\Sanctum\HasApiTokens;
>
> class Usuario extends Authenticatable
> {
> use HasApiTokens, HasFactory, Notifiable;
>
> protected $table = 'TB__USUARIO';
>
> protected $fillable = [
> 'ID',
> 'NOMBRE',
> 'CLAVE',
> 'ESTA_ACTIVO'
> ];
>
> protected $casts = [
> 'ID' => 'integer',
> 'NOMBRE' => 'string',
> 'CLAVE' => 'string',
> 'ESTA_ACTIVO' => 'boolean'
> ];
> }
- app/Http/Controllers/SistemaController file:
> <?php
>
> namespace App\Http\Controllers;
>
> use Illuminate\Http\Request;
> use Illuminate\Support\Facades\Auth;
> use App\Models\Usuario;
>
> class SistemaController extends Controller
> {
> public function login(Request $request) {
> $usuario = Usuario::where([
> ['NOMBRE', '=', $request->get('usuario')],
> ['CLAVE', '=', $request->get('clave')],
> ['ESTA_ACTIVO', '=', true]
> ])->first();
>
> if ($usuario != null) {
> Auth::login($usuario); // This statement is not working, because if I then do dd(Auth::id()) -> it returns null when it should return the user ID
>
> $request->session()->regenerate();
> return redirect()->intended('/pedido');
> }
>
> return back()->withErrors([
> 'clave' => 'LAS CREDENCIALES SON INCORRECTAS',
> ]);
> }
> }
- I have printed the variable $usuario (with this command dd($usuario)) to see if it has a value and this is the result.
In other words, my $usuario variable does have a value, but for some reason I can't authenticate the user. I'd appreciate your help.
Greetings.
答案1
得分: 0
Sure, here's the translated code:
if ($usuario != null) {
Auth::login($usuario);
$request->session()->regenerate();
return redirect()->intended('/pedido');
}
And the modified code you provided:
if (Auth::attempt($usuario)) {
dd($usuario); // 检查是否返回用户ID。
$request->session()->regenerate();
return redirect()->intended('/pedido');
}
英文:
if ($usuario != null) {
Auth::login($usuario);
$request->session()->regenerate();
return redirect()->intended('/pedido');
}
can you try this:
if (Auth::attempt($usuario)) {
dd($usuario); //check if it returns the user ID.
$request->session()->regenerate();
return redirect()->intended('/pedido');
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论