https://github.com/instea/ra-cognito-auth
https://github.com/instea/ra-cognito-auth
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/instea/ra-cognito-auth
- Owner: instea
- License: isc
- Created: 2022-12-13T10:37:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-20T12:24:26.000Z (over 2 years ago)
- Last Synced: 2025-05-28T01:42:28.695Z (about 1 year ago)
- Language: TypeScript
- Size: 112 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ra-cognito-auth
AWS Cognito auth provider for react-admin.
## Installation
```sh
npm install ra-cognito-auth amazon-cognito-identity-js-promises aws-jwt-verify
```
## Usage
### Basic usage
Build auth provider and pass it to the react admin.
Example:
```js
import { CognitoUserPool } from 'amazon-cognito-identity-js-promises';
import { buildCognitoAuthProvider } from 'ra-cognito-auth';
import jsonServerProvider from 'ra-data-json-server';
import { Admin, ListGuesser, Resource } from 'react-admin';
const dataProvider = jsonServerProvider('https://jsonplaceholder.typicode.com');
const userPool: CognitoUserPool = new CognitoUserPool({
UserPoolId: 'myUserPool',
ClientId: 'myClientId',
});
const authProvider = buildCognitoAuthProvider({ userPool });
export const App = () => (
);
```
Get access token from the cognito pool to authorize api calls:
```js
// calling `getSession()` will also refresh token if needed
const session = await userPool.getCurrentUser()?.getSession();
const accessToken = session?.getAccessToken().getJwtToken();
```
### Usage with verifying JWT
If you would like to verify the JWT instead of just checking the validity of the session, pass a configured cognito jwt verifier to the build function. The JWT token from the session is then verified against the verifier during the checkAuth method of the provider.
Example with validating if the user has an admin group:
```js
// ...
const cognitoJwtVerifier = CognitoJwtVerifier.create({
tokenUse: 'access',
userPoolId: 'myUserPool',
clientId: 'myClientId',
groups: ['admin'],
});
const authProvider = buildCognitoAuthProvider({ userPool, cognitoJwtVerifier });
// ...
```