File tree Expand file tree Collapse file tree 3 files changed +44
-7
lines changed
Expand file tree Collapse file tree 3 files changed +44
-7
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ in `.env.local` file to make the environment variable accessible on `process.env
4242Example App:
4343
4444``` jsx
45+ // index.js
46+
4547import React from " react" ;
4648import ReactDOM from " react-dom/client" ;
4749import App from " ./App" ;
@@ -50,19 +52,24 @@ import { EasyauthProvider } from "@easyauth.io/easyauth-react";
5052const root = ReactDOM .createRoot (document .getElementById (" root" ));
5153root .render (
5254 < React .StrictMode >
53- < EasyauthProvider
54- authority= {process .env .REACT_APP_EASYAUTH_APP_URL }
55- clientId= {process .env .REACT_APP_EASYAUTH_CLIENT_ID }
56- redirectUri= {process .env .REACT_APP_EASYAUTH_REDIRECT_URL }
57- >
55+ < EasyauthProvider>
5856 < App / >
5957 < / EasyauthProvider>
6058 < / React .StrictMode >
6159);
6260
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>
69+
6370// App.js
6471
65- import { useEasyauth } from " @easyauth.io/easyauth-react" ;
72+ import { useEasyauth , UserProfile } from " @easyauth.io/easyauth-react" ;
6673
6774function App () {
6875 const auth = useEasyauth ();
@@ -86,7 +93,7 @@ function App() {
8693 < div className= " App" >
8794 < header className= " App-header" >
8895 < p> Hello {auth .user ? .profile .sub } < / p>
89- < Profile / >
96+ < UserProfile / >
9097 < button
9198 onClick= {() => {
9299 auth .removeUser ();
Original file line number Diff line number Diff line change 1+ import { useEffect , useState } from 'react' ;
2+ import { useEasyauth } from '../useEasyauth/useEasyauth.jsx' ;
3+ import { getProfile } from '../../api/api.js' ;
4+
5+ export const useUser = ( ) => {
6+ const auth = useEasyauth ( ) ;
7+ const [ user , setUser ] = useState ( { } ) ;
8+ const token = auth . user ?. access_token ;
9+ useEffect ( ( ) => {
10+ const fetchUser = async ( ) => {
11+ const response = await getProfile ( token ) ;
12+ setUser ( response . data ) ;
13+ } ;
14+
15+ if ( token ) {
16+ fetchUser ( ) ;
17+ }
18+ } , [ token ] ) ;
19+
20+ return {
21+ isAuthenticated : auth . isAuthenticated ,
22+ isLoading : Object . keys ( user ) . length ?
23+ false :
24+ auth . isAuthenticated ?
25+ true :
26+ false ,
27+ user : user ,
28+ } ;
29+ } ;
Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ export * from './hooks/useEasyauth/useEasyauth.jsx';
33export * from './components/UserProfile/UserProfile.jsx' ;
44export * from './components/SignedInAndSignedOut/SignedIn.jsx' ;
55export * from './components/SignedInAndSignedOut/SignedOut.jsx' ;
6+ export * from './hooks/useUser/useUser.jsx' ;
You can’t perform that action at this time.
0 commit comments