Skip to content

Commit

Permalink
fix: add Authorization header on all HTTP requests when signed in
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Apr 27, 2020
1 parent 23cbfb6 commit aecfbd7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
"@wizeline/serverless-amplify-plugin": "^1.2.1",
"serverless-dotenv-plugin": "^2.3.2"
}
}
}
22 changes: 18 additions & 4 deletions packages/ui/src/AuthenticatedApp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import Amplify from 'aws-amplify'
import Amplify, { Auth } from 'aws-amplify'
import {
withAuthenticator,
Loading,
Expand All @@ -13,15 +13,29 @@ import {
Greetings
} from 'aws-amplify-react'
import '@aws-amplify/ui/dist/style.css'
import axios from 'axios'
import stackOutputs from './stack-outputs.json'

const {
REACT_APP_API_SERVER = stackOutputs.ApiEndpoint
} = process.env

// Set Authorization header on all requests if user is signed in
axios.interceptors.request.use(async function (config) {
try {
const currentUserSession = await Auth.currentSession()
const Authorization = currentUserSession.idToken.jwtToken
config.headers.Authorization = Authorization
} catch (e) { /* Auth.currentSession() throws if not signed in 🤷‍♂️ */ }

return config
})

axios.defaults.baseURL = REACT_APP_API_SERVER

async function fetchAndSetUsers({ setUsers }) {
const fetchUsersResponse = await fetch(`${REACT_APP_API_SERVER}/users`)
const users = await fetchUsersResponse.json()
const fetchUsersResponse = await axios.get('/users')
const users = fetchUsersResponse.data
setUsers(users)
}

Expand All @@ -47,7 +61,7 @@ Amplify.configure({
},
})

// We're auto-confirming via the Lambda Function;
// We're auto-confirming via the Lambda Function
// Hack to skip the ConfirmSignUp view
function ConfirmSignUpRedirectToSignIn({ authState, onStateChange }) {
React.useEffect(() => {
Expand Down
22 changes: 11 additions & 11 deletions packages/ui/src/stack-outputs.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"CognitoIdentityPoolId": "us-east-1:7c7c8555-6011-401d-a17b-a958fdfb50a4",
"CognitoUserPoolClientId": "5rg0fo7bs3htub7p1a0qs9f5m1",
"CognitoUserPoolId": "us-east-1_mHgk1Fx61",
"CognitoAutoConfirmUserLambdaFunctionQualifiedArn": "arn:aws:lambda:us-east-1:347971939225:function:myapp-dev-cognitoAutoConfirmUser:14",
"UserTableName": "myapp-dev-UserTable-78HVMXYFD7V8",
"ApiEndpoint": "https://ui0c2gahdg.execute-api.us-east-1.amazonaws.com/dev",
"ExpressLambdaFunctionQualifiedArn": "arn:aws:lambda:us-east-1:347971939225:function:myapp-dev-express:15",
"CognitoPostAuthenticationLambdaFunctionQualifiedArn": "arn:aws:lambda:us-east-1:347971939225:function:myapp-dev-cognitoPostAuthentication:8",
"ServiceEndpoint": "https://ui0c2gahdg.execute-api.us-east-1.amazonaws.com/dev",
"MyappAmplifyDefaultDomain": "dev.d3b425eo47pmdl.amplifyapp.com",
"ServerlessDeploymentBucketName": "myapp-dev-serverlessdeploymentbucket-1t3nth23pmkxw"
"CognitoIdentityPoolId": "us-east-1:75332cdd-9682-4f84-86a1-3fa3e6e1772b",
"CognitoUserPoolClientId": "6kca9f4b8cr876hj3ir58omtqi",
"CognitoUserPoolId": "us-east-1_B5Fg5HB8i",
"CognitoAutoConfirmUserLambdaFunctionQualifiedArn": "arn:aws:lambda:us-east-1:347971939225:function:myapp-dev-cognitoAutoConfirmUser:16",
"UserTableName": "myapp-dev-UserTable-62M153SR7M1Z",
"ApiEndpoint": "https://y39z600b6i.execute-api.us-east-1.amazonaws.com/dev",
"ExpressLambdaFunctionQualifiedArn": "arn:aws:lambda:us-east-1:347971939225:function:myapp-dev-express:18",
"CognitoPostAuthenticationLambdaFunctionQualifiedArn": "arn:aws:lambda:us-east-1:347971939225:function:myapp-dev-cognitoPostAuthentication:10",
"ServiceEndpoint": "https://y39z600b6i.execute-api.us-east-1.amazonaws.com/dev",
"MyappAmplifyDefaultDomain": "dev.d3ndte54h2kan9.amplifyapp.com",
"ServerlessDeploymentBucketName": "myapp-dev-serverlessdeploymentbucket-1dbbfcsiri9fp"
}

0 comments on commit aecfbd7

Please sign in to comment.