项目作者: AlexZeitler

项目描述 :
An example how to use AWS Cognito (via CDK) with F# Giraffe and React
高级语言: JavaScript
项目地址: git://github.com/AlexZeitler/cognito-fsharp-giraffe-react-sample.git
创建时间: 2021-01-15T02:13:59Z
项目社区:https://github.com/AlexZeitler/cognito-fsharp-giraffe-react-sample

开源协议:MIT License

下载


AWS Cognito (via CDK) F# Giraffe React PKCE flow sample

This sample shows how to use AWS Cognito (deployed via AWS CDK) together with an F# Giraffe API and a React application with Authorization Code Grant (PKCE).

Setup

Clone this repository.

Cognito

Install / configure AWS CDK

  1. npm install -g aws-cdk

Configure Cognito Userpool

Edit src/cognito/lib/config.json:

  1. {
  2. "userpoolName": "sample-user-pool",
  3. "scopeName": "api",
  4. "resourceServerName": "giraffe-server",
  5. "resourceServerIdentifier": "giraffe-server",
  6. "clientName": "react-client",
  7. "domainPrefix": "giraffe-sample"
  8. }

Deploy Cognito

  1. cd src/cognito
  2. yarn
  3. cdk deploy

Your output should look like this:

  1. CognitoStack: deploying...
  2. CognitoStack
  3. Outputs:
  4. CognitoStack.domain = giraffe-sample
  5. CognitoStack.reactClientId = 3ot29hu8k3cbikij2jat97sqou
  6. CognitoStack.scopeName = giraffe-server/api
  7. CognitoStack.userpoolId = eu-central-1_rypunXn4M

Make sure to grab the Information listed below “Outputs” - otherwise you’ll have to lookup them in the AWS Cognito Console.

Create a Cognito User

Sample:

Giraffe API

Configure the Giraffe API

Change the settings in the Config module in src/SampleAPI/Program.fs:

  1. module Config =
  2. let region = "eu-central-1" // your AWS region
  3. let userPoolId = "eu-central-1_rypunXn4M" // CognitoStack.userpoolId
  4. let clientId = "3ot29hu8k3cbikij2jat97sqou" // CognitoStack.reactClientId
  5. let scopeName = "giraffe-server/api" // CognitoStack.scopeName
  6. let clientUri = "http://localhost:3000"

Start the Giraffe API

  1. dotnet run --no-restore

Your API should now be listening:

  1. info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[62]
  2. User profile is available. Using '/Users/alexzeitler/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
  3. info: Microsoft.Hosting.Lifetime[0]
  4. Now listening on: http://localhost:5000
  5. info: Microsoft.Hosting.Lifetime[0]
  6. Now listening on: https://localhost:5001
  7. info: Microsoft.Hosting.Lifetime[0]
  8. Application started. Press Ctrl+C to shut down.

React UI

Configure the React UI

Edit src/ui/src/config/app-config.json:

  1. {
  2. "region": "eu-central-1", // your AWS region
  3. "userPool": "eu-central-1_rypunXn4M", // CognitoStack.userpoolId
  4. "userPoolBaseUri": "https://giraffe-sample.auth.eu-central-1.amazoncognito.com", // https://${CognitoStack.domain}.auth.${AWS_REGION}.amazoncognito.com
  5. "clientId": "3ot29hu8k3cbikij2jat97sqou", // CognitoStack.reactClientId
  6. "callbackUri": "http://localhost:3000/callback",
  7. "signoutUri": "http://localhost:3000",
  8. "tokenScopes": [
  9. "openid",
  10. "email",
  11. "profile",
  12. "giraffe-server/api" // CognitoStack.scopeName
  13. ],
  14. "apiUri": "https://localhost:5001"
  15. }

Start the React UI

  1. cd src/ui
  2. yarn start

Your browser should show this:

Login

Click the “Sign in” link in your UI:

Enter your credentials:

If everything went well, you’ll see this screen:

Credits

The UI is based on https://github.com/arronharden/cognito-demo-ui.