@gadgetinc/react-bigcommerce
The @gadgetinc/react-bigcommerce
package provides a set of utilities to help you build single-click BigCommerce apps.
The package is available on GitHub.
Features
When building single-click BigCommerce apps, the @gadgetinc/react-bigcommerce
package provides the following features:
- Handles OAuth and
access_token
flow required for single-click BigCommerce apps - Manages user sessions in the
session
model using an iframe-secure session with the Gadget backend
Installation
For Gadget apps that have a BigCommerce connection, the @gadgetinc/react-bigcommerce
package is automatically installed.
If you need to install the package manually, you can do so with yarn
:
terminal yarn add @gadgetinc/react-bigcommerce
Gadget apps use yarn
as the package manager.
If you are adding @gadgetinc/react-bigcommerce
to a non-Gadget app, you can use the package manager of your choice.
Usage
To use @gadgetinc/react-bigcommerce
in your app, you can import the <Provider />
and pass in your Gadget app API client as a prop. A useGadget()
hook can be used to check when the OAuth request is loading
and if the user isAuthenticated
.
1import { Provider as GadgetProvider, useGadget } from "@gadgetinc/react-bigcommerce";2import { Box, Link, Panel, ProgressCircle, Text } from "@bigcommerce/big-design";3import { api } from "../api";4import { Outlet } from "react-router";56export default function App() {7 // pass your app's api client to the Provider8 return (9 <GadgetProvider api={api}>10 <AuthenticatedApp />11 </GadgetProvider>12 );13}1415function AuthenticatedApp() {16 // the useGadget hook provides the `isAuthenticated` and `loading` booleans17 const { isAuthenticated, loading } = useGadget();1819 // show a spinner if authentication is in progress20 if (loading) {21 return (22 <div23 style={{24 display: "flex",25 justifyContent: "center",26 alignItems: "center",27 height: "100%",28 width: "100%",29 }}30 >31 <ProgressCircle />32 </div>33 );34 }3536 // if the user is authenticated, render the router Outlet37 // otherwise show an unauthenticated app page38 return (39 <Box marginHorizontal={{ mobile: "none", tablet: "xxxLarge" }} marginVertical={{ mobile: "none", tablet: "xxLarge" }}>40 {isAuthenticated ? <Outlet /> : <UnauthenticatedApp />}41 </Box>42 );43}4445function UnauthenticatedApp() {46 return (47 <Panel description="App must be viewed in the BigCommerce control panel">48 <Text>49 <Link target="_blank" href={`/edit/${process.env.GADGET_PUBLIC_APP_ENV}`}>50 {"Edit this app"}51 </Link>52 </Text>53 </Panel>54 );55}
1import { Provider as GadgetProvider, useGadget } from "@gadgetinc/react-bigcommerce";2import { Box, Link, Panel, ProgressCircle, Text } from "@bigcommerce/big-design";3import { api } from "../api";4import { Outlet } from "react-router";56export default function App() {7 // pass your app's api client to the Provider8 return (9 <GadgetProvider api={api}>10 <AuthenticatedApp />11 </GadgetProvider>12 );13}1415function AuthenticatedApp() {16 // the useGadget hook provides the `isAuthenticated` and `loading` booleans17 const { isAuthenticated, loading } = useGadget();1819 // show a spinner if authentication is in progress20 if (loading) {21 return (22 <div23 style={{24 display: "flex",25 justifyContent: "center",26 alignItems: "center",27 height: "100%",28 width: "100%",29 }}30 >31 <ProgressCircle />32 </div>33 );34 }3536 // if the user is authenticated, render the router Outlet37 // otherwise show an unauthenticated app page38 return (39 <Box marginHorizontal={{ mobile: "none", tablet: "xxxLarge" }} marginVertical={{ mobile: "none", tablet: "xxLarge" }}>40 {isAuthenticated ? <Outlet /> : <UnauthenticatedApp />}41 </Box>42 );43}4445function UnauthenticatedApp() {46 return (47 <Panel description="App must be viewed in the BigCommerce control panel">48 <Text>49 <Link target="_blank" href={`/edit/${process.env.GADGET_PUBLIC_APP_ENV}`}>50 {"Edit this app"}51 </Link>52 </Text>53 </Panel>54 );55}
Reference
Provider
A Provider
component that wraps your BigCommerce single-click app to authenticate the API client and generate a session token.
Props
api: Client
- The Gadget API client to use for the extension
useGadget()
useGadget(): { loading: boolean, isAuthenticated: boolean }
A React hook that determines when the OAuth request is underway and if the user is authenticated.
Returns
loading: boolean
- A boolean that istrue
when the OAuth request is in progressisAuthenticated: boolean
- A boolean that istrue
when the user is authenticated