How can I add a subdomain to a React web app for a different type of users?

We have a web app with frontend on React. I would like to add a subdomain for a second type of users, with limited access and features. This second type of users is a different entity so managing ‘Roles’ or ‘Authority’ is not going to work.

Furthermore, for this subdomain I would like to create a different store with a limited number of reducers.

Here is a chunk of code from the current index.js:

const createStoreWithMiddleware = applyMiddleware(
  LoadingMiddleware,
  ReduxPromise,
  CallbackMiddlware,
  ErrorHandlerMiddleware,
  NotificationMiddlware,
  ReduxThunk
)(createStore)
const store = createStoreWithMiddleware(reducers)

const token = cookie.load('token')
if (token) {
  store.dispatch({type: AUTH_USER})
}

// ========================================

ReactDOM.render(<Provider store={store}>
    <Router history={history}>
      <ScrollToTop/>
      <App/>
    </Router>
    <Alert stack={{limit: 3, spacing: 10}}/>
  </Provider>,
  document.getElementById('root')
)

Update 1: By “sub-domain” I mean something like: user.myapp.com as compared to main domain as myapp.com

Update 2: To be more specific, how can/should I serve a different App based on the subdomain?

I’ve checked several tutorials but still not sure what is the correct approach.

New contributor

Vitalie Rotaru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Now, if you want to have a subdomain—as would be the case in this example—with a different store and likely different views, you can do it in a couple of ways. Here is one well-structured approach towards the solution using React and Redux:

  1. Routing and Subdomain Handling
    You need to handle routing based on the subdomain; user.myapp.com should get routed. This part generally involves server-side configuration so that requests get routed appropriately to your React application.

Configuring the Server: Ensure that requests to user.myapp.com are passed to the same React appl as myapp.com, but distinguished in application-logic by subdomain.
2. Configuration of the Redux Shop
As you would like to have another store with a subset of reducers for the subdomain of the users:

Separate store configuration: Change your existing Redux store setup to hold multiple stores. You will be able to create a different store conditionally and use that store according to the sub-domain.
Here’s how you can modify your index.js:

import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { Router } from 'react-router-dom';
import ReactDOM from 'react-dom';
import history from './history'; // Ensure you have history set up for Router
import reducers from './reducers'; // Your combined reducers
import App from './App'; // Your main App component

const createStoreWithMiddleware = applyMiddleware(
  LoadingMiddleware,
  ReduxPromise,
  CallbackMiddlware,
  ErrorHandlerMiddleware,
  NotificationMiddlware,
  ReduxThunk
)(createStore);

// Function to create a store with specific reducers
const createCustomStore = () => {
  // Define reducers specific to the subdomain
  const customReducers = {
    // Add your specific reducers here
    // Example: user: userReducer
  };

  // Create store with custom reducers
  return createStoreWithMiddleware(combineReducers({
    ...reducers, // Your main reducers
    ...customReducers
  }));
};

const token = cookie.load('token');

let store;
if (token) {
  store = createStoreWithMiddleware(reducers);
} else {
  // Create a different store for users without token
  store = createCustomStore();
}

ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <ScrollToTop/>
      <App/>
    </Router>
    <Alert stack={{limit: 3, spacing: 10}}/>
  </Provider>,
  document.getElementById('root')
);
  1. Component-Based Rendering
    You can conditionally render elements in your React components, or routes in the App, based on whether the user is on the subdomain or not. You could do it based on the URL of any other context available to the application.
New contributor

Yashodhan Advankar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật