Auth::login()在Laravel中与驱动程序sqlsrv不起作用。

huangapple go评论56阅读模式
英文:

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.

  1. The Laravel project is version 8.x.
  2. The SQL Server I am connecting to is version 2017 Express.
  3. The php version I'm using is 8.2.0.
  4. Add the extensions (extension=php_pdo_sqlsrv_82_ts_x64
    extension=php_sqlsrv_82_ts_x64) in the php.ini file.
  5. 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.

Auth::login()在Laravel中与驱动程序sqlsrv不起作用。

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.

  1. The Laravel project is version 8.x.
  2. The SQL Server I am connecting to is version 2017 Express.
  3. The php version I'm using is 8.2.0.
  4. Add the extensions (extension=php_pdo_sqlsrv_82_ts_x64
    extension=php_sqlsrv_82_ts_x64) in the php.ini file.
  5. 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.

Auth::login()在Laravel中与驱动程序sqlsrv不起作用。

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-&gt;session()-&gt;regenerate();
    return redirect()-&gt;intended(&#39;/pedido&#39;);
 }

can you try this:

if (Auth::attempt($usuario)) {
        dd($usuario); //check if it returns the user ID.
        $request-&gt;session()-&gt;regenerate();
        return redirect()-&gt;intended(&#39;/pedido&#39;);
    }

huangapple
  • 本文由 发表于 2023年6月9日 11:15:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定