mercredi 16 décembre 2020

api authencation for multiple tables using passport

I have four tables 1-admin,2-seller,3-customer,4-webusers i wanted to make api for these tables using passport i have added this tables in config/auth.php

        'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],
            'admin' => [
                'driver' => 'session',
                'provider' => 'admin',
            ],
            'admin-api' => [
                'driver' => 'passport',
                'provider' => 'admin',
                'hash' => true,
            ],
            'webusers' => [
                'driver' => 'session',
                'provider' => 'webusers',
            ],
            'webusers-api' => [
                'driver' => 'passport',
                'provider' => 'webusers',
                'hash' => true,
            ],
            'customer' => [
                'driver' => 'session',
                'provider' => 'customer',
            ],
            'customer-api' => [
                'driver' => 'passport',
                'provider' => 'customer',
                'hash' => true,
            ],
            'seller' => [
                'driver' => 'session',
                'provider' => 'sellers',
            ],
            'seller-api' => [
                'driver' => 'passport',
                'provider' => 'sellers',
                'hash' => true,
            ],
            'api' => [
                'driver' => 'passport',
                'provider' => 'users',
                'hash' => true,
            ],
        ],
       'providers' => [
            'users' => [
                'driver' => 'eloquent',
                'model' => App\Models\User::class,
            ],
            'customer' => [
                'driver' => 'eloquent',
                'model' => App\Models\Customer::class,
            ],
            'admin' => [
                'driver' => 'eloquent',
                'model' => App\Models\Admin::class,
            ],
            'seller' => [
                'driver' => 'eloquent',
                'model' => App\Models\Sellers::class,
            ],
            'webuser' => [
                'driver' => 'eloquent',
                'model' => App\Models\Webuser::class,
            ],
        ],

and i have created first api for customer here is my routes name

Route::post('/customer/register', [CustomerController::class, 'registerCustomer']);
Route::group(['middleware' => 'auth:customer-api'], function () {
    Route::post('/customer/login', [CustomerController::class, 'customerLogin']);
});

and here is my controller for customer

                <?php

            namespace App\Http\Controllers\API;

            use App\Models\Customer;
            use Illuminate\Http\Request;
            use App\Http\Controllers\Controller;
            use Illuminate\Support\Facades\Hash;
            use Illuminate\Support\Facades\Validator;

            class CustomerController extends Controller
            {    
                private $successStatus  =   200;
                public function customerLogin(Request $request)
                {
                    $customerLogin= Customer::where('email', $request->email)
                                            ->orWhere('mobile', $request->mobile)
                                            ->first();
                    if (!$customerLogin) {
                        return response()->json(['error' => 'Email / mobile not found'], 400);
                    }
                    if (!$customerLogin || !Hash::check($request->passWord, $customerLogin->passWord)) {
                        return response()->json(['error' => 'Unauthorised'], 401);
                    }else{
                        $success['success']           =  true;
                        $success['message']           =  "Success! you are logged in successfully";
                        $success['token']             =  $customerLogin->createToken('Register-Customer-Token')->accessToken;
                        $success['tokenExpiryTime']   =  2592000000; //converted 30days minutes in miliseconds
                        $success['customerName']      =  $customerLogin->name;
                        return response()->json(['success' => $success], $this->successStatus);
                    }
                }
                public function registerCustomer(Request $request)
                {
                    $validator              =  Validator::make($request->all(), [
                        'name'              => 'required|min:3|max:55',
                        'email'             => 'required|email|unique:customers',
                        'mobile'            => 'required|digits:10|unique:customers',
                        'countryCode'       => 'required|digits:6',
                        'passWord'          => 'required|alpha_num|min:8',
                        'confirmPassword'   => 'required|same:passWord|alpha_num|min:8',
                    ]);
                    if($validator->fails())
                    {
                        return response()->json(['error'=>'Unprocessable Entity','validationErrors' => $validator->errors()], 422);
                    }
                    $customerData           =  array(
                        'name'              =>    $request->name,
                        'email'             =>    $request->email,
                        'mobile'            =>    $request->mobile,
                        'countryCode'       =>    $request->countryCode,
                        'passWord'          =>    Hash::make($request->passWord),
                        'confirmPassword'   =>    Hash::make($request->confirmPassword),
                        'email_verified'    =>    '0',
                    );
                    $saveCustomerData     =  Customer::create($customerData); 
                    return $this->customerLogin($request);  
                }
            }

And Here My Model For Customer

                <?php
            namespace App\Models;

            use Laravel\Passport\HasApiTokens;
            use Illuminate\Database\Eloquent\Model;
            use Illuminate\Notifications\Notifiable;
            use Illuminate\Database\Eloquent\Factories\HasFactory;

            class Customer extends Model 
            {
                use HasFactory, HasApiTokens, Notifiable;
                protected $table      = 'customers';
                protected $primaryKey = 'id';
                protected $fillable = [
                    'name', 
                    'email',
                    'mobile',
                    'countryCode',
                    'email_verified_at',
                    'email_verified',
                    'passWord',
                    'confirmPassword'
                ];
                protected $hidden = [
                    'email',
                    'passWord',
                    'confirmPassword',
                ];
            }

when i am registering customer its registered successfully and giving token there is no issu, but whe i am goining to login its givng error

                        BadMethodCallException: Call to undefined method App\Models\Customer::getAuthIdentifierName() in file C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php on line 50

                    #0 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php(36): Illuminate\Database\Eloquent\Model::throwBadMethodCallException('getAuthIdentifi...')
                    #1 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1885): Illuminate\Database\Eloquent\Model-&gt;forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), 'getAuthIdentifi...', Array)
                    #2 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php(51): Illuminate\Database\Eloquent\Model-&gt;__call('getAuthIdentifi...', Array)
                    #3 C:\xampp\htdocs\duopolin\admin\vendor\laravel\passport\src\PassportUserProvider.php(42): Illuminate\Auth\EloquentUserProvider-&gt;retrieveById('11')
                    #4 C:\xampp\htdocs\duopolin\admin\vendor\laravel\passport\src\Guards\TokenGuard.php(159): Laravel\Passport\PassportUserProvider-&gt;retrieveById('11')
                    #5 C:\xampp\htdocs\duopolin\admin\vendor\laravel\passport\src\Guards\TokenGuard.php(110): Laravel\Passport\Guards\TokenGuard-&gt;authenticateViaBearerToken(Object(Illuminate\Http\Request))
                    #6 C:\xampp\htdocs\duopolin\admin\vendor\laravel\passport\src\PassportServiceProvider.php(308): Laravel\Passport\Guards\TokenGuard-&gt;user(Object(Illuminate\Http\Request))
                    #7 [internal function]: Laravel\Passport\PassportServiceProvider-&gt;Laravel\Passport\{closure}(Object(Illuminate\Http\Request), NULL)
                    #8 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Auth\RequestGuard.php(58): call_user_func(Object(Closure), Object(Illuminate\Http\Request), NULL)
                    #9 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Auth\GuardHelpers.php(60): Illuminate\Auth\RequestGuard-&gt;user()
                    #10 C:\xampp\htdocs\duopolin\admin\app\Http\Middleware\RedirectIfAuthenticated.php(25): Illuminate\Auth\RequestGuard-&gt;check()
                    #11 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): App\Http\Middleware\RedirectIfAuthenticated-&gt;handle(Object(Illuminate\Http\Request), Object(Closure), 'customer-api')
                    #12 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #13 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #14 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(127): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #15 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(103): Illuminate\Routing\Middleware\ThrottleRequests-&gt;handleRequest(Object(Illuminate\Http\Request), Object(Closure), Array)
                    #16 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php(55): Illuminate\Routing\Middleware\ThrottleRequests-&gt;handleRequestUsingNamedLimiter(Object(Illuminate\Http\Request), Object(Closure), 'api', Object(Closure))
                    #17 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Routing\Middleware\ThrottleRequests-&gt;handle(Object(Illuminate\Http\Request), Object(Closure), 'api')
                    #18 C:\xampp\htdocs\duopolin\admin\vendor\laravel\sanctum\src\Http\Middleware\EnsureFrontendRequestsAreStateful.php(33): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #19 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful-&gt;Laravel\Sanctum\Http\Middleware\{closure}(Object(Illuminate\Http\Request))
                    #20 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #21 C:\xampp\htdocs\duopolin\admin\vendor\laravel\sanctum\src\Http\Middleware\EnsureFrontendRequestsAreStateful.php(34): Illuminate\Pipeline\Pipeline-&gt;then(Object(Closure))
                    #22 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #23 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #24 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(694): Illuminate\Pipeline\Pipeline-&gt;then(Object(Closure))
                    #25 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(669): Illuminate\Routing\Router-&gt;runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
                    #26 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(635): Illuminate\Routing\Router-&gt;runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
                    #27 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(624): Illuminate\Routing\Router-&gt;dispatchToRoute(Object(Illuminate\Http\Request))
                    #28 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(166): Illuminate\Routing\Router-&gt;dispatch(Object(Illuminate\Http\Request))
                    #29 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel-&gt;Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
                    #30 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #31 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #32 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #33 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #34 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #35 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #36 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(87): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #37 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #38 C:\xampp\htdocs\duopolin\admin\vendor\fruitcake\laravel-cors\src\HandleCors.php(57): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #39 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #40 C:\xampp\htdocs\duopolin\admin\vendor\fideloper\proxy\src\TrustProxies.php(57): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #41 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fideloper\Proxy\TrustProxies-&gt;handle(Object(Illuminate\Http\Request), Object(Closure))
                    #42 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    #43 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(141): Illuminate\Pipeline\Pipeline-&gt;then(Object(Closure))
                    #44 C:\xampp\htdocs\duopolin\admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(110): Illuminate\Foundation\Http\Kernel-&gt;sendRequestThroughRouter(Object(Illuminate\Http\Request))
                    #45 C:\xampp\htdocs\duopolin\admin\public\index.php(52): Illuminate\Foundation\Http\Kernel-&gt;handle(Object(Illuminate\Http\Request))
                    #46 C:\xampp\htdocs\duopolin\admin\server.php(21): require_once('C:\\xampp\\htdocs...')
                    #47 {main}
                    -->


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3nov1xf
via IFTTT

Aucun commentaire:

Enregistrer un commentaire