Skip to content

Commit e0be2dc

Browse files
authored
Merge pull request #17 from omthakare16/main
updated Readme +added useStripe hook
2 parents e04743a + 398bb9d commit e0be2dc

File tree

7 files changed

+96
-31
lines changed

7 files changed

+96
-31
lines changed

README.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ If using Create React App, set :
3939

4040
in `.env.local` file to make the environment variable accessible on `process.env`.
4141

42+
```.env
43+
#.env.local
44+
45+
REACT_APP_EASYAUTH_APP_URL=https://<your_subdomain>.app.easyauth.io
46+
REACT_APP_EASYAUTH_CLIENT_ID=<client_id>
47+
REACT_APP_EASYAUTH_REDIRECT_URL=<redirect_uri such as http://127.0.0.1:3000>
48+
49+
```
50+
4251
Example App:
4352

4453
```jsx
@@ -57,16 +66,11 @@ root.render(
5766
</EasyauthProvider>
5867
</React.StrictMode>
5968
);
69+
```
6070

61-
//User can also pass authority,clientId,redirectUri explicitly as a prop in EasauthProvider component
62-
//for ex. <EasyauthProvider
63-
// authority={process.env.REACT_APP_EASYAUTH_APP_URL}
64-
// clientId={process.env.REACT_APP_EASYAUTH_CLIENT_ID}
65-
// redirectUri={process.env.REACT_APP_EASYAUTH_REDIRECT_URL}
66-
// >
67-
// <App />
68-
// </EasyauthProvider>
71+
_User can also pass authority,clientId,redirectUri explicitly as a prop in EasauthProvider component._
6972

73+
```jsx
7074
//App.js
7175

7276
import { useEasyauth, UserProfile } from "@easyauth.io/easyauth-react";
@@ -118,6 +122,16 @@ function App() {
118122
export default App;
119123
```
120124
125+
```jsx
126+
<EasyauthProvider
127+
authority={process.env.REACT_APP_EASYAUTH_APP_URL}
128+
clientId={process.env.REACT_APP_EASYAUTH_CLIENT_ID}
129+
redirectUri={process.env.REACT_APP_EASYAUTH_REDIRECT_URL}
130+
>
131+
<App />
132+
</EasyauthProvider>
133+
```
134+
121135
_For further details and examples, please refer to our [Documentation](https://easyauth.io/docs/quickstart/react/)._
122136
123137
## License

src/api/api.js

+40-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
const BASE_URL = process.env.REACT_APP_EASYAUTH_APP_URL + '/tenantbackend';
22

3+
4+
const tokenLocalStorage = () => {
5+
let result;
6+
const keys = Object.keys(localStorage);
7+
keys.forEach((key) => {
8+
if (key.startsWith('oidc.user')) {
9+
result = key;
10+
}
11+
});
12+
const value = localStorage.getItem(result);
13+
return JSON.parse(value)?.id_token;
14+
};
15+
316
const commonAPICall = async (
417
PATH,
518
METHOD = 'GET',
@@ -13,7 +26,10 @@ const commonAPICall = async (
1326
const response = await fetch(FULLPATH, {
1427
method: METHOD,
1528
body: BODY,
16-
headers: headers,
29+
headers: {
30+
...headers,
31+
Authorization: `Bearer ${tokenLocalStorage()}`,
32+
},
1733
});
1834

1935
return response;
@@ -36,13 +52,29 @@ const postAPI = async (PATH, DATA) => {
3652
return response;
3753
};
3854

39-
const getProfile = async (token) => {
40-
const headers = {Authorization: `Bearer ${token}`};
41-
const response = await getAPI('/api/profile', headers);
42-
if (!response.ok) {
43-
throw new Error(`Request failed with status ${response.status}`);
44-
}
55+
const getProfile = async () => {
56+
const response = await getAPI('/api/profile');
57+
return response;
58+
};
59+
const getStripeCreatePortalSessionUrl = async () => {
60+
const response = await getAPI('/api/stripe/create-portal-session');
61+
return response;
62+
};
63+
const getStripeCheckoutUrl = async (priceId) => {
64+
const response = await getAPI(`/api/stripe/checkout/${priceId}`);
65+
return response;
66+
};
67+
const getStripeSubscriptions = async () => {
68+
const response = await getAPI('/api/stripe/subscriptions');
4569
return response;
4670
};
4771

48-
export {commonAPICall, getAPI, postAPI, getProfile};
72+
export {
73+
commonAPICall,
74+
getAPI,
75+
postAPI,
76+
getProfile,
77+
getStripeCreatePortalSessionUrl,
78+
getStripeCheckoutUrl,
79+
getStripeSubscriptions,
80+
};

src/components/EasyauthProvider/EasyauthProvider.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export const EasyauthProvider = ({
2828
};
2929

3030
EasyauthProvider.propTypes = {
31-
authority: PropTypes.string.isRequired,
32-
clientId: PropTypes.string.isRequired,
33-
redirectUri: PropTypes.string.isRequired,
31+
authority: PropTypes.string,
32+
clientId: PropTypes.string,
33+
redirectUri: PropTypes.string,
3434
children: PropTypes.node,
3535
};

src/components/UserProfile/UserProfile.jsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export const UserProfile = ({loader}) => {
99

1010
useEffect(() => {
1111
(async () => {
12-
const token = auth.user?.access_token;
13-
const response = await getProfile(token);
12+
const response = await getProfile();
1413
setProfile(response.data);
1514
})();
1615
}, [auth]);

src/hooks/useStripe/useStripe.jsx

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {
2+
getStripeCheckoutUrl,
3+
getStripeCreatePortalSessionUrl,
4+
getStripeSubscriptions,
5+
} from '../../api/api.js';
6+
7+
export const useStripe = () => {
8+
const createPortalSession = async () => {
9+
const response = await getStripeCreatePortalSessionUrl();
10+
return response.data.url;
11+
};
12+
13+
const checkout = async (priceId) => {
14+
const response = await getStripeCheckoutUrl(priceId);
15+
return response.data.url;
16+
};
17+
const subscriptions = async () => {
18+
const response = await getStripeSubscriptions();
19+
return response.data;
20+
};
21+
return {createPortalSession, checkout, subscriptions};
22+
};

src/hooks/useUser/useUser.jsx

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@ import {getProfile} from '../../api/api.js';
55
export const useUser = () => {
66
const auth = useEasyauth();
77
const [user, setUser] = useState({});
8-
const token = auth.user?.access_token;
98
useEffect(() => {
109
const fetchUser = async () => {
11-
const response = await getProfile(token);
10+
const response = await getProfile();
1211
setUser(response.data);
1312
};
1413

15-
if (token) {
16-
fetchUser();
17-
}
18-
}, [token]);
14+
fetchUser();
15+
}, [auth]);
1916

2017
return {
2118
isAuthenticated: auth.isAuthenticated,
2219
isLoading: Object.keys(user).length ?
23-
false :
24-
auth.isAuthenticated ?
25-
true :
26-
false,
20+
false :
21+
auth.isAuthenticated ?
22+
true :
23+
false,
2724
user: user,
2825
};
2926
};

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './components/UserProfile/UserProfile.jsx';
44
export * from './components/SignedInAndSignedOut/SignedIn.jsx';
55
export * from './components/SignedInAndSignedOut/SignedOut.jsx';
66
export * from './hooks/useUser/useUser.jsx';
7+
export * from './hooks/useStripe/useStripe.jsx';

0 commit comments

Comments
 (0)