Closed julienV closed 1 year ago
I'm not sure if this is the best way to do it, but here's an example of I have done it when wanting to pass the id
token via request headers on each request. You could do something similar with session.getAccessToken().getJwtToken()
const authProvider = CognitoAuthProvider({
mode: 'oauth',
clientId: process.env.REACT_APP_COGNITO_CLIENT_ID,
userPoolId: process.env.REACT_APP_COGNITO_POOL_ID,
hostedUIUrl: process.env.REACT_APP_COGNITO_HOSTED_UI_URL,
scope: ['openid', 'email', 'profile', 'aws.cognito.signin.user.admin']
});
const userPool = new CognitoUserPool({
ClientId: process.env.REACT_APP_COGNITO_CLIENT_ID,
UserPoolId: process.env.REACT_APP_COGNITO_POOL_ID
});
const httpClient = (url, options = { headers: null }) => {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
const user = userPool.getCurrentUser();
user.getSession((err, session: CognitoUserSession) => {
if (session) {
const idJwt = session.getIdToken().getJwtToken();
options.headers.set('Authorization', `Bearer ${idJwt}`);
}
});
return fetchUtils.fetchJson(url, options);
};
I've experimented with passing the CognitoUserPool
into authProvider
as the support appears to be there in code, but something with my configuration does not play well with the implementation.
Both approach are valid. I would favor https://github.com/marmelab/ra-auth-cognito/issues/6#issuecomment-1503541232 though
@djhi both solution work, but they feel rather workaroundish, why not have a function exported directly from the authprovider so we don't have to do this ?
We are actually thinking about it. In the mean time, that's the recommended way.
It seems like the authProvider doesn't provide a hook or handle to provide the access token back to the data provider. Here is how i deal with it, intercepting the handleCallback call:
i can then get the token from the local storage in the data provider, but i feel like I am maybe missing a better way to do that...