forked from aws-amplify/amplify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
130 additions
and
140 deletions.
There are no files selected for viewing
135 changes: 65 additions & 70 deletions
135
packages/amplify-category-auth/resources/auth-custom-resource/hostedUILambda.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,76 @@ | ||
const response = require('cfn-response'); | ||
const aws = require('aws-sdk'); | ||
const identity = new aws.CognitoIdentityServiceProvider(); | ||
exports.handler = (event, context, callback) => { | ||
|
||
exports.handler = (event, context) => { | ||
// Don't return promise, response.send() marks context as done internally | ||
const ignoredPromise = handleEvent(event, context); | ||
}; | ||
|
||
async function checkDomainAvailability(domainName) { | ||
const params = { Domain: domainName }; | ||
try { | ||
const res = await identity.describeUserPoolDomain(params).promise(); | ||
if (res.DomainDescription && res.DomainDescription.UserPool) { | ||
return false; | ||
} | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
async function deleteUserPoolDomain(domainName, userPoolId) { | ||
const params = { Domain: domainName, UserPoolId: userPoolId }; | ||
await identity.deleteUserPoolDomain(params).promise(); | ||
} | ||
|
||
async function createUserPoolDomain(domainName, userPoolId) { | ||
const params = { | ||
Domain: domainName, | ||
UserPoolId: userPoolId, | ||
}; | ||
await identity.createUserPoolDomain(params).promise(); | ||
} | ||
|
||
async function handleEvent(event, context) { | ||
const userPoolId = event.ResourceProperties.userPoolId; | ||
const inputDomainName = event.ResourceProperties.hostedUIDomainName; | ||
let deleteUserPoolDomain = (domainName) => { | ||
let params = { Domain: domainName, UserPoolId: userPoolId }; | ||
return identity.deleteUserPoolDomain(params).promise(); | ||
}; | ||
if (event.RequestType == 'Delete') { | ||
deleteUserPoolDomain(inputDomainName) | ||
.then(() => { | ||
response.send(event, context, response.SUCCESS, {}); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
response.send(event, context, response.FAILED, { err }); | ||
}); | ||
} | ||
if (event.RequestType == 'Update' || event.RequestType == 'Create') { | ||
let checkDomainAvailability = (domainName) => { | ||
let params = { Domain: domainName }; | ||
return identity | ||
.describeUserPoolDomain(params) | ||
.promise() | ||
.then((res) => { | ||
if (res.DomainDescription && res.DomainDescription.UserPool) { | ||
return false; | ||
} | ||
return true; | ||
}) | ||
.catch((err) => { | ||
return false; | ||
}); | ||
}; | ||
let createUserPoolDomain = (domainName) => { | ||
let params = { Domain: domainName, UserPoolId: userPoolId }; | ||
return identity.createUserPoolDomain(params).promise(); | ||
}; | ||
identity | ||
.describeUserPool({ UserPoolId: userPoolId }) | ||
.promise() | ||
.then((result) => { | ||
if (inputDomainName) { | ||
if (result.UserPool.Domain === inputDomainName) { | ||
return; | ||
try { | ||
if (event.RequestType === 'Delete') { | ||
await deleteUserPoolDomain(inputDomainName, userPoolId); | ||
} else if (event.RequestType === 'Update' || event.RequestType === 'Create') { | ||
const result = await identity.describeUserPool({ UserPoolId: userPoolId }).promise(); | ||
if (inputDomainName) { | ||
if (result.UserPool.Domain === inputDomainName) { | ||
return; | ||
} else { | ||
if (!result.UserPool.Domain) { | ||
const isDomainAvailable = await checkDomainAvailability(inputDomainName); | ||
if (isDomainAvailable) { | ||
await createUserPoolDomain(inputDomainName, userPoolId); | ||
} else { | ||
throw new Error('Domain not available'); | ||
} | ||
} else { | ||
if (!result.UserPool.Domain) { | ||
return checkDomainAvailability(inputDomainName).then((isDomainAvailable) => { | ||
if (isDomainAvailable) { | ||
return createUserPoolDomain(inputDomainName); | ||
} else { | ||
throw new Error('Domain not available'); | ||
} | ||
}); | ||
const isDomainAvailable = await checkDomainAvailability(inputDomainName); | ||
if (isDomainAvailable) { | ||
await deleteUserPoolDomain(result.UserPool.Domain, userPoolId); | ||
await createUserPoolDomain(inputDomainName, userPoolId); | ||
} else { | ||
return checkDomainAvailability(inputDomainName).then((isDomainAvailable) => { | ||
if (isDomainAvailable) { | ||
return deleteUserPoolDomain(result.UserPool.Domain).then(() => createUserPoolDomain(inputDomainName)); | ||
} else { | ||
throw new Error('Domain not available'); | ||
} | ||
}); | ||
throw new Error('Domain not available'); | ||
} | ||
} | ||
} else { | ||
if (result.UserPool.Domain) { | ||
return deleteUserPoolDomain(result.UserPool.Domain); | ||
} | ||
} | ||
}) | ||
.then(() => { | ||
response.send(event, context, response.SUCCESS, {}); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
response.send(event, context, response.FAILED, { err }); | ||
}); | ||
} else { | ||
if (result.UserPool.Domain) { | ||
await deleteUserPoolDomain(result.UserPool.Domain, userPoolId); | ||
} | ||
} | ||
} | ||
response.send(event, context, response.SUCCESS, {}); | ||
} catch (err) { | ||
console.log(err); | ||
response.send(event, context, response.FAILED, { err }); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters