为什么租户中的 api 路由.php Laravel VueJs 在网络工具中使用 200 OK 状态代码,但我收到一个刀片作为响应?



首先,我想评论一下,我正在开发一个基于Laravel VueJs的项目,该项目具有Laravel v3包的租约。

为了进入上下文,我可以完美地登录我的name@example.com在本地开发中任何创建的租户处注册的帐户,但登录后,我主要遇到类型错误:无法在控制台中读取所有与api相关的和VueJs路由的未定义属性(读取'xxxxx'(。我一直在深入研究这个问题并得出结论。

类型错误:无法读取未定义的属性(读取'xxxxx'(

有了这一点,我用GetUserAuth找到了我的GET路由,因为tenant.php上"api"中间件的uri被识别,但没有调用与其相关的方法,而是作为状态代码抛出200 OK。相反,响应是一个刀片,如下图所示。

其中Respuesta响应

同样重要的是,我已经根据stancl/trence v3文档设置了Laravel Passport,特别是在租户应用程序中仅使用共享的护照加密密钥的Passport。

以下代码指的是我的租户.php

<?php
declare(strict_types=1);
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use StanclTenancyMiddlewareInitializeTenancyByDomain;
use StanclTenancyMiddlewarePreventAccessFromCentralDomains;
use AppHttpControllersUserController;
/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider.
|
| Feel free to customize them however you want. Good luck!
|
*/
Route::middleware(['api', InitializeTenancyByDomain::class, PreventAccessFromCentralDomains::class])->group(
function ()
{
/*auth middleware api passport token*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});

// Other routes
Route::middleware(['auth:api', 'Is_Active'])->group(function ()
{
//Other routes
//------------------------------- Users --------------------------\
Route::get('GetUserRole', [UserController::class, "GetUserRole"]);
Route::get('GetUserAuth', [UserController::class, "GetUserAuth"]);
Route::get("/GetPermissions", [UserController::class, "GetPermissions"]);
Route::resource('users', UserController::class);
Route::put('users/Activated/{id}', [UserController::class, "IsActivated"]);
Route::get('users/export/Excel', [UserController::class, "exportExcel"]);
Route::get('users/Get_Info/Profile', [UserController::class, "GetInfoProfile"]);
Route::put('updateProfile/{id}', [UserController::class, "updateProfile"]);
});
});
Route::middleware(['web', InitializeTenancyByDomain::class, PreventAccessFromCentralDomains::class])->group(
function ()
{
// Web routes   
});

以下代码指的是位于C/project-root/resources/src/store/modules/auth.js 的代码

import Vue from 'vue'
import Vuex from 'vuex'
// import VueCookie from 'vue-cookie'
import axios from 'axios'
import router from "./../../router";
Vue.use(Vuex)
// Vue.use(VueCookie)

const state = {
// token: Vue.cookie.get('Stocky_token'),
isAuthenticated:false,
Permissions: null,
user: {},
loading: false,
error: null,
notifs:0,
};

const getters = {
isAuthenticated: state => state.isAuthenticated,
currentUser: state => state.user,
currentUserPermissions: state => state.Permissions,
loading: state => state.loading,
notifs_alert: state => state.notifs,
error: state => state.error
};
const mutations = {
setLoading(state, data) {
state.loading = data;
state.error = null;
},
setError(state, data) {
state.error = data;
state.loggedInUser = null;
state.loading = false;
},
clearError(state) {
state.error = null;
},
// setLoginCred(state, payload) {
//     state.token = payload.token;
//     // state.isAuthenticated = true;
// },
setPermissions(state, Permissions) {
state.Permissions = Permissions;
// state.user = user;
},

setUser(state, user) {
state.user = user;
},
// SET_AUTHENTICATED(state, isAuthenticated) {
//     state.isAuthenticated = isAuthenticated;
// },
Notifs_alert(state, notifs) {
state.notifs = notifs;
},
logout(state) {
// state.token = null;
state.user = null;
state.Permissions = null;
// state.isAuthenticated = false;
// Vue.cookie.delete('Stocky_token');
state.loggedInUser = null;
state.loading = false;
state.error = null;
},
};
const actions = {
// setLoginCred(context, payload) {
//     context.commit('setLoading', true)
//     context.commit('setLoginCred', payload)
// },
async refreshUserPermissions(context) {
await axios.get("GetUserAuth").then((userAuth) => {
let Permissions = userAuth.data.permissions
let user = userAuth.data.user
let notifs = userAuth.data.notifs
// context.commit('SET_AUTHENTICATED', true)
context.commit('setPermissions', Permissions)
context.commit('setUser', user)
context.commit('Notifs_alert', notifs)
}).catch(() => {
// context.commit('SET_AUTHENTICATED', false)
context.commit('setPermissions', null)
context.commit('setUser', null)
context.commit('Notifs_alert', null)
});
},
logout({ commit }) {
axios({method:'post',  url: '/logout', baseURL: '' })
.then((userData) => {
window.location.href='/login';
})
},
};
export default {
state,
getters,
actions,
mutations
};

下面的一个引用了前面提到的UserCon上的方法,该方法与GET路由相关,并将刀片作为响应抛出状态代码200 OK

//------------- GET USER Auth ---------\
public function GetUserAuth(Request $request)
{
$helpers = new helpers();
$user['avatar'] = Auth::user()->avatar;
$user['username'] = Auth::user()->username;
$user['currency'] = $helpers->Get_Currency();
$user['logo'] = Setting::first()->logo;
$user['footer'] = Setting::first()->footer;
$user['developed_by'] = Setting::first()->developed_by;
$user['initCCF'] = Auth::user()->initCCF;
$user['currentCCF'] = Auth::user()->currentCCF;
$user['finalCCF'] = Auth::user()->finalCCF;
$user['initCF'] = Auth::user()->initCF;
$user['currentCF'] = Auth::user()->currentCF;
$user['finalCF'] = Auth::user()->finalCF;
$user['warehouse_id'] = Auth::user()->warehouse_id;
$permissions = Auth::user()->roles()->first()->permissions->pluck('name');
$products_alerts = product_warehouse::join('products', 'product_warehouse.product_id', '=', 'products.id')
->whereRaw('qte <= stock_alert')
->where('product_warehouse.deleted_at', null)
->count();
return response()->json([
'success' => true,
'user' => $user,
'notifs' => $products_alerts,
'permissions' => $permissions,
]);
}

最后,我想说的是,我的应用程序只有中心域和一个数据库,运行得很好,但我不知道问题出在哪里,即使是调试、检查所有相关文档和其他已回答的问题,但都没有解决我的问题,我需要尽快得到帮助。欢迎任何建议或帮助,如果您需要更多信息,请告诉我。提前感谢您慢慢来。

在为laravel使用租赁时,我面临的一个常见问题是您可以购买的SaaS样板中包含的默认Authenticate中间件。它扩展了默认的laravelAuthenticate,并添加了以下代码:

if (! $request->expectsJson()) {
// return a blade view
return route('tenant.login');
}

如果缺少Accept: application/json标头,则会将所有请求重定向到tenant.login视图。

我真的看不到你发送给GetUserAuth的请求的标题,但这可能是原因。

首先,很抱歉我延迟了解决方案。

对于那些在过去一年中使用stancl/tenance v3包遇到同样问题的人,正如我在评论中提到的,我将在这里留下我的passport.php设置:

<?php
return [
/*
|--------------------------------------------------------------------------
| Encryption Keys
|--------------------------------------------------------------------------
|
| Passport uses encryption keys while generating secure access tokens for
| your application. By default, the keys are stored as local files but
| can be set via environment variables when that is more convenient.
|
*/
'private_key' => env('PASSPORT_PRIVATE_KEY', storage_path('oauth-private.key')),
'public_key' => env('PASSPORT_PUBLIC_KEY', storage_path('oauth-public.key')),
/*
|--------------------------------------------------------------------------
| Client UUIDs
|--------------------------------------------------------------------------
|
| By default, Passport uses auto-incrementing primary keys when assigning
| IDs to clients. However, if Passport is installed using the provided
| --uuids switch, this will be set to "true" and UUIDs will be used.
|
*/
'client_uuids' => false,
/*
|--------------------------------------------------------------------------
| Personal Access Client
|--------------------------------------------------------------------------
|
| If you enable client hashing, you should set the personal access client
| ID and unhashed secret within your environment file. The values will
| get used while issuing fresh personal access tokens to your users.
|
*/
'personal_access_client' => [
'id' => env('PASSPORT_PERSONAL_ACCESS_CLIENT_ID'),
'secret' => env('PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET'),
],
/*
|--------------------------------------------------------------------------
| Passport Storage Driver
|--------------------------------------------------------------------------
|
| This configuration value allows you to customize the storage options
| for Passport, such as the database connection that should be used
| by Passport's internal database models which store tokens, etc.
|
*/
'storage' => [
'database' => [
'connection' => null,
],
],
'key_path' => env('OAUTH_KEY_PATH', storage_path(''))
];

希望这将帮助其他开发人员解决上面详述的问题。

致问候,

Ignacio Y.C.

相关内容

  • 没有找到相关文章

最新更新