0

当我尝试更改表用户时,默认情况下与 laravel 中的 Auth 链接我的 API 总是给我“未经授权的用户”响应,当我使用默认表用户时它工作正常。我想知道为什么会这样?

我已经完成了将表从用户切换到我的选择表所需的所有更改。

我的表名是:apitable,Model_name:apitablemodel

auth.php 的变化:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'api',
        'Passwords' => 'apitable',               //if want to change the table attached with auth then make changes here
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [ 
        'web' => [ 
            'driver' => 'session', 
            'provider' => 'apitabel', 
        ], 
        'api' => [ 
            'driver' => 'passport', 
            'provider' => 'apitable', 
        ],


    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
         'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
        'apitable' => [
            'driver' => 'eloquent',
            'model' => App\apitablemodel::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
         'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'apitable' => [
            'provider' => 'apitable',
            'table' => 'password_resets',
            'expire' => 60,
        ],

    ],

];

这是我的模型文件

 <?php

namespace App;
use Laravel\Passport\HasApiTokens;
// use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class apitablemodel extends Authenticatable
{  use HasApiTokens, Notifiable;
    //
    protected $table='apitable';

    protected $fillable = ['Email', 'Password','token'];
    public $timestamps=false; 
    protected $hidden = [
        'Password', 'token',
    ];
}

这是我的控制器文件

<?php

namespace App\Http\Controllers\API;
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use App\apitablemodel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
// response()->json(['success' => $success], $this-> successStatus);
class product extends Controller
{  public $successStatus = 200;
    public function login(Request $request){ 


      if(Auth::check(['Email' => request('Email'), 'Password' => request('Password')])){ 
        $user = Auth::user(); 
        $success['token'] =  $user->createToken('MyApp')-> accessToken; 
         $user->token=$success['token'];
        //  $token->expires_at=Carbon::now()->addMinutes(1);
         $user->save();

         return response()->json(["token_expires in :"=>"2min","token"=>$user->token]); 
    } 
    else{ 
        return response()->json(['error'=>'Unauthorised'], 401); 
    } 

    }
    public function update(Request $req){


    $obj= User::find($req->id);
    $obj->email=$req->email;

    if($obj->save()){

        return response()->json(['success'=>'Updated']); 
    }

else{
    return ["status:"=>"Unauthorized user, make sure your are login"];
}

    }
}

我的控制器中有两个功能,一个用于更新“apitable”表中存在的数据,另一个用于登录,以便只有经过身份验证的用户才能更新数据库表值。

当我在邮递员中点击我的 API 时,它会给我未经授权的响应,因为我已经在我的控制器中写入了条件。我想知道如果条件失败的原因。

4

1 回答 1

0

我认为您应该使用Auth::attempt()登录方法。

 public function login(Request $request){ 

    $credentials = $request->only('Email', 'Password');
    if(Auth::attempt($credentials)){ 
        $user = Auth::user(); 
        $success['token'] =  $user->createToken('MyApp')-> accessToken; 
        $user->token=$success['token'];
        //  $token->expires_at=Carbon::now()->addMinutes(1);
        $user->save();

        return response()->json(["token_expires in :"=>"2min","token"=>$user->token]); 
    } else { 
        return response()->json(['error'=>'Unauthorised'], 401); 
    } 
于 2020-02-25T07:06:58.217 回答