Last active
October 3, 2022 18:00
-
-
Save singledigit/cae2ffcb77851945968f24fa6766f8f3 to your computer and use it in GitHub Desktop.
Pattern for using Cognito User Pools as authentication against Cognito Identity
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
import {inject} from 'aurelia-framework'; | |
import {Session} from './session'; | |
@inject(Session) | |
export class Auth { | |
// App specific | |
userPoolId = 'us-east-1_fgCWraBkF'; | |
appClientId = '57lq262n28o7ddt8i36jcjj7qd'; | |
constructor(session) { | |
this.session = session; | |
// Required as mock credentials | |
AWSCognito.config.update({accessKeyId: 'mock', secretAccessKey: 'mock'}); | |
// pool data | |
this.poolData = { | |
UserPoolId: this.userPoolId, | |
ClientId: this.appClientId | |
}; | |
// create user pool | |
this.userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(this.poolData); | |
} | |
registerUser(user) { | |
let attributes = []; | |
let emailData = { | |
Name: 'email', | |
Value: user.email | |
}; | |
let nameData = { | |
Name: 'name', | |
Value: user.name | |
}; | |
attributes.push(new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(emailData)); | |
attributes.push(new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(nameData)); | |
this.userPool.signUp(user.username, user.password, attributes, null, (err, result) => { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
this.session.registered = true; | |
}); | |
} | |
confirmUser(username, code) { | |
let userData = { | |
Username: username, | |
Pool: this.userPool | |
}; | |
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); | |
cognitoUser.confirmRegistration(code, true, (err, result) => { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
this.session.confirmed = true; | |
}); | |
} | |
loginUser(username, password) { | |
let authData = { | |
Username: username, | |
Password: password | |
}; | |
let authDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authData); | |
let userData = { | |
Username: username, | |
Pool: this.userPool | |
}; | |
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); | |
cognitoUser.authenticateUser(authDetails, { | |
onSuccess: (result) => { | |
this.session.user = cognitoUser; | |
}, | |
onFailure: (err) => { | |
console.log(err); | |
} | |
}); | |
} | |
getSession() { | |
let cognitoUser = this.userPool.getCurrentUser(); | |
if (cognitoUser != null) { | |
cognitoUser.getSession((err, session) => { | |
if (err) { | |
this.logoutUser(); | |
return; | |
} | |
this.session.user = cognitoUser; | |
}); | |
} | |
else this.logoutUser(); | |
} | |
logoutUser() { | |
let cognitoUser = this.userPool.getCurrentUser(); | |
this.session.user = null; | |
if (cognitoUser != null) cognitoUser.signOut(); | |
} | |
getUserAttributes() { | |
return new Promise((resolve, reject) => { | |
this.session.user.getUserAttributes((err, result) => { | |
if (err) reject(err); | |
else resolve(result); | |
}) | |
}) | |
} | |
} |
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
confirmUser(username, code) { | |
let userData = { | |
Username: username, | |
Pool: this.userPool | |
}; | |
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); | |
cognitoUser.confirmRegistration(code, true, (err, result) => { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
this.session.confirmed = true; | |
}); | |
} |
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
getSession() { | |
let cognitoUser = this.userPool.getCurrentUser(); | |
if (cognitoUser != null) { | |
cognitoUser.getSession((err, session) => { | |
if (err) { | |
this.logoutUser(); | |
return; | |
} | |
this.session.user = cognitoUser; | |
}); | |
} | |
else this.logoutUser(); | |
} |
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
getUserAttributes() { | |
return new Promise((resolve, reject) => { | |
this.session.user.getUserAttributes((err, result) => { | |
if (err) reject(err); | |
else resolve(result); | |
}) | |
}) | |
} |
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
<script src="//serverless-assets.s3-website-us-east-1.amazonaws.com/jsbn.js"></script> | |
<script src="//serverless-assets.s3-website-us-east-1.amazonaws.com/jsbn2.js"></script> | |
<script src="//serverless-assets.s3-website-us-east-1.amazonaws.com/sjcl.js"></script> | |
<script src="//serverless-assets.s3-website-us-east-1.amazonaws.com/moment.js"></script> | |
<script src="//serverless-assets.s3-website-us-east-1.amazonaws.com/aws-cognito-sdk.min.js"></script> | |
<script src="//serverless-assets.s3-website-us-east-1.amazonaws.com/amazon-cognito-identity.min.js"></script> | |
<script src="//serverless-assets.s3-website-us-east-1.amazonaws.com/aws-sdk.min.js"></script> |
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
<script> | |
AWSCognito.config.region = 'us-east-1'; | |
</script> |
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
loginUser(username, password) { | |
let authData = { | |
Username: username, | |
Password: password | |
}; | |
let authDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authData); | |
let userData = { | |
Username: username, | |
Pool: this.userPool | |
}; | |
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); | |
cognitoUser.authenticateUser(authDetails, { | |
onSuccess: (result) => { | |
this.session.user = cognitoUser; | |
}, | |
onFailure: (err) => { | |
console.log(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
logoutUser(){ | |
let cognitoUser = userPool.getCurrentUser(); | |
this.session.user = null; | |
if(cognitoUser != null) cognitoUser.signOut(); | |
} |
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
import {inject} from 'aurelia-framework'; | |
import {Session} from './session'; | |
@inject(Session) | |
export class Auth { | |
userPoolId = 'us-east-1_fgCWraBkF'; | |
appClientId = '57lq262n28o7ddt8i36jcjj7qd'; | |
constructor(session) { | |
this.session = session; | |
// Required as mock credentials | |
AWSCognito.config.update({accessKeyId: 'mock', secretAccessKey: 'mock'}); | |
// pool data | |
this.poolData = { | |
UserPoolId: this.userPoolId, | |
ClientId: this.appClientId | |
}; | |
// create user pool | |
this.userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(this.poolData); | |
} | |
} |
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
registerUser(user) { | |
let attributes = []; | |
let emailData = { | |
Name: 'email', | |
Value: user.email | |
}; | |
let nameData = { | |
Name: 'name', | |
Value: user.name | |
}; | |
attributes.push(new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(emailData)); | |
attributes.push(new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(nameData)); | |
this.userPool.signUp(user.username, user.password, attributes, null, (err, result) => { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
this.session.registered = true; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment