Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const p = new Promise((res, rej)=>{
// create the `userData` object for instantiating a new `cognitoUser` object
const userData = {
Username: email,
Pool: userPool
}
// create the `cognitoUser` object
const cognitoUser = new CognitoUser(userData)
// and call the `resendConfirmationCode()` of `cognitoUser`
cognitoUser.resendConfirmationCode(function(err, result) {
// reject promise if confirmation code failed
if (err) {
console.log(err);
rej(err)
return
}
// resolve if successfull
res()
})
})
return p
authenticate(username: string, password: string, callback: CognitoCallback) {
console.log("UserLoginService: starting the authentication");
let authenticationData = {
Username: username,
Password: password,
};
let authenticationDetails = new AuthenticationDetails(authenticationData);
let userData = {
Username: username,
Pool: this.cognitoUtil.getUserPool()
};
console.log("UserLoginService: Params set...Authenticating the user");
let cognitoUser = new CognitoUser(userData);
console.log("UserLoginService: config is " + AWS.config);
cognitoUser.authenticateUser(authenticationDetails, {
newPasswordRequired: (userAttributes, requiredAttributes) => callback.cognitoCallback(`User needs to set password.`, null),
onSuccess: result => this.onLoginSuccess(callback, result),
onFailure: err => this.onLoginError(callback, err),
mfaRequired: (challengeName, challengeParameters) => {
callback.handleMFAStep(challengeName, challengeParameters, (confirmationCode: string) => {
cognitoUser.sendMFACode(confirmationCode, {
onSuccess: result => this.onLoginSuccess(callback, result),
onFailure: err => this.onLoginError(callback, err)
});
});
}
});
}
export function forgotPassword(username, onSuccess, onFailure, inputVerificationCode) {
let cognitoUser = new CognitoUser({
Username: username,
Pool: userPool,
});
cognitoUser.forgotPassword({
onSuccess: onSuccess,
onFailure: onFailure,
inputVerificationCode: inputVerificationCode
});
}
function signupConfirm(poolData, body, cb) {
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const { username, confirmationCode } = body;
const userData = {
Username: username,
Pool: userPool,
};
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.confirmRegistration(confirmationCode, true, (err, res) => cb(err, res));
}
module.exports = (poolData, body, cb) => {
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const username = body.username;
const refreshToken = new AmazonCognitoIdentity.CognitoRefreshToken({RefreshToken: body.refreshToken});
const userData = {
Username : username,
Pool : userPool
};
let cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
return cognitoUser.refreshSession(refreshToken, function(err, userSession) {
if (err) {
return cb(err);
}
cognitoUser.signInUserSession = userSession;
return cognitoUser.getUserAttributes(function (err, res) {
if (err) {
return cb(err);
}
return cb(null, res);
});
});
confirmNewPassword = async ({ userName, password, confirmationCode }) => {
const cognitoUser = new CognitoUser({
Username: userName,
Pool: this.userPool
});
const errorResponses = {};
return new Promise((resolve, reject) => {
cognitoUser.confirmPassword(confirmationCode, password, {
onSuccess: resolve,
onFailure: error => {
reject({ general: "invalid confirmation code" });
}
});
});
};
const getCognitoUser = (email) => {
return new CognitoUser({
Username: email,
Pool: getCognitoUserPool(),
});
};
confirmPassword(verficationCode: string, newPassword: string, callback: (error: Error, statusCode: string) => void) {
if (!this.signupData.username) {
callback(new Error('Username is Empty.'), AuthService.statusCodes.incompletedSigninData);
return;
}
const authService = this;
const cognitoUser = new CognitoUser({
Username: this.signupData.username,
Pool: this.userPool
});
cognitoUser.confirmPassword(verficationCode, newPassword, {
onSuccess: () => {
authService.currentStatus = AuthService.statusCodes.passwordChanged;
callback(null, AuthService.statusCodes.success);
},
onFailure: (err: Error) => {
authService.currentStatus = AuthService.statusCodes.unknownError;
callback(err, AuthService.statusCodes.unknownError);
}
});
}
export function forgotPasswordReset(username, verificationCode, newPassword, callback) {
let cognitoUser = new CognitoUser({
Username: username,
Pool: userPool,
});
cognitoUser.confirmPassword(verificationCode, newPassword, callback);
}
confirmRegistration (username, code, cb) {
let cognitoUser = new CognitoUser({
Username: username,
Pool: this.userPool
})
cognitoUser.confirmRegistration(code, true, cb)
}