我在 Javascript 中執(zhí)行了一個查詢來獲取我的應用程序中當前用戶的數(shù)據(jù)。此查詢返回當前用戶(客戶端)的數(shù)據(jù),這些數(shù)據(jù)用于使用 Apollo Client 和 GraphQL 檢查我的 React 應用程序中各個頁面的客戶端訪問權限。但是,對于只有管理員有權訪問的頁面,在進行此查詢時,頁面會呈現(xiàn),以便沒有管理員權限的用戶可以臨時查看頁面的內(nèi)容。一旦檢查了權限并且知道用戶沒有訪問權限,就會產(chǎn)生一個錯誤頁面。我希望立即生成此錯誤頁面,以便沒有權限的客戶根本無法查看任何內(nèi)容。// This is a currentUser.js file that is imported by various React components // which uses the query to check permissionsimport gql from 'graphql-tag';export default apolloClient => apolloClient .query({ query: gql` query CURRENT_USER { name age gender permissions } `, }) .then(({ data }) => ({ currentUser: data })) .catch(() => ({ currentUser: {} }));// This is a AdministratorPage.jsx file that shouldn't render whilst // permissions are checkedimport currentUser from '../lib/currentUser';import React, { Component } from 'react';import { ApolloConsumer } from 'react-apollo';class AdministratorPage extends Component { render() { return ( <ApolloConsumer> {(client) => { currentUser(client).then((data) => { ...}有任何想法嗎?
在檢查客戶端權限之前 React 中的頁面呈現(xiàn)
慕慕森
2021-07-09 14:11:20