Skip to content

Instantly share code, notes, and snippets.

@singledigit
Last active October 3, 2022 18:00
Show Gist options
  • Save singledigit/cae2ffcb77851945968f24fa6766f8f3 to your computer and use it in GitHub Desktop.
Save singledigit/cae2ffcb77851945968f24fa6766f8f3 to your computer and use it in GitHub Desktop.
Pattern for using Cognito User Pools as authentication against Cognito Identity
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);
})
})
}
}
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;
});
}
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();
}
getUserAttributes() {
return new Promise((resolve, reject) => {
this.session.user.getUserAttributes((err, result) => {
if (err) reject(err);
else resolve(result);
})
})
}
<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>
<script>
AWSCognito.config.region = 'us-east-1';
</script>
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);
}
});
}
logoutUser(){
let cognitoUser = userPool.getCurrentUser();
this.session.user = null;
if(cognitoUser != null) cognitoUser.signOut();
}
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);
}
}
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