Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'amazon-cognito-identity-js' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
const authOptionsWithNoUserPoolId: AuthOptions = {
userPoolId: null,
userPoolWebClientId: 'awsUserPoolsWebClientId',
region: 'region',
identityPoolId: 'awsCognitoIdentityPoolId',
mandatorySignIn: false,
};
const userPool = new CognitoUserPool({
UserPoolId: authOptions.userPoolId,
ClientId: authOptions.userPoolWebClientId,
});
const idToken = new CognitoIdToken({ IdToken: 'idToken' });
const accessToken = new CognitoAccessToken({ AccessToken: 'accessToken' });
const session = new CognitoUserSession({
IdToken: idToken,
AccessToken: accessToken,
});
const cognitoCredentialSpyon = jest
.spyOn(CognitoIdentityCredentials.prototype, 'get')
.mockImplementation(callback => {
callback(null);
});
describe('for react native', () => {
describe('currentUserCredentials test', () => {
test('with federated info', async () => {
// const auth = new Auth(authOptions);
userPoolId: null,
userPoolWebClientId: 'awsUserPoolsWebClientId',
region: 'region',
identityPoolId: 'awsCognitoIdentityPoolId',
mandatorySignIn: false,
};
const userPool = new CognitoUserPool({
UserPoolId: authOptions.userPoolId,
ClientId: authOptions.userPoolWebClientId,
});
const idToken = new CognitoIdToken({ IdToken: 'idToken' });
const accessToken = new CognitoAccessToken({ AccessToken: 'accessToken' });
const session = new CognitoUserSession({
IdToken: idToken,
AccessToken: accessToken,
});
const cognitoCredentialSpyon = jest
.spyOn(CognitoIdentityCredentials.prototype, 'get')
.mockImplementation(callback => {
callback(null);
});
describe('for react native', () => {
describe('currentUserCredentials test', () => {
test('with federated info', async () => {
// const auth = new Auth(authOptions);
// const spyon = jest.spyOn(Credentials, 'currentUserCredentials');
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
Password: '...', // password created in cognito pool
};
const poolData = {
UserPoolId: '...', // Your user pool id here
ClientId: '...', // Your client id here
};
const userPool = new CognitoUserPool(poolData);
const userData = {
Username: '...', // username created in cognito pool
Pool: userPool,
};
const authenticationDetails = new AuthenticationDetails(authenticationData);
const cognitoUser = new CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function(result) {
// console.log("access token = " + result.getAccessToken().getJwtToken());
console.log(`id token = ${result.getIdToken().getJwtToken()}`);
},
onFailure: function(err) {
console.log(err);
},
newPasswordRequired: function(userAttributes) {
// User was signed up by an admin and must provide new
// password and required attributes, if any, to complete
// authentication.
const p = new Promise((res, rej)=>{
// we create an array for our attributes that we want to update, and push all `CognitoUserAttribute` objects into it
const attributeList = []
// loop through the `attrs` array to create our `CognitoUserAttribute` objects
for(let a = 0; a
const p = new Promise((res, rej)=>{
// we create an array for our attributes that we want to update, and push all `CognitoUserAttribute` objects into it
const attributeList = []
// loop through the `attrs` array to create our `CognitoUserAttribute` objects
for(let a = 0; a
function _cognitoLogin(user, dispatch, getState) {
let {config} = getState();
dispatch({type: SET_USER, data: {...user}});
const authenticationData = {
Username: user.userName,
Password: user.password,
};
const authenticationDetails = new AuthenticationDetails(authenticationData);
const poolData = {
UserPoolId: config.userPoolId,
ClientId: config.clientId
};
const userPool = new CognitoUserPool(poolData);
const userData = {
Username: user.userName,
Pool: userPool
};
const cognitoUser = new CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
// login was successful: store aws idToken to state
dispatch({type: "SET_ID_TOKEN", token: result.getIdToken().getJwtToken()});
dispatch({type: "SET_ACCESS_TOKEN", token: result.getAccessToken().getJwtToken()});
},
return new Promise((resolve, reject) => {
const dataEmail = { Name: 'email', Value: email }
const dataFirstName = { Name: 'given_name', Value: firstName }
const dataLastName = { Name: 'family_name', Value: lastName }
const attributeList = [
new CognitoUserAttribute(dataEmail),
new CognitoUserAttribute(dataFirstName),
new CognitoUserAttribute(dataLastName),
]
userPool.signUp(email, password, attributeList, null, (error, result) => {
log(error, result)
if (error) {
// TODO this code shouldn't know about graphql errors. refactor to use separate layers
if (error.code === 'UsernameExistsException') {
reject(new UserError(ERRORS.EmailAlreadyTaken))
} else {
reject(error)
}
return
}
resolve({ id: result.userSub, email })
})
return new Promise((resolve, reject) => {
const dataEmail = { Name: 'email', Value: email }
const dataFirstName = { Name: 'given_name', Value: firstName }
const dataLastName = { Name: 'family_name', Value: lastName }
const attributeList = [
new CognitoUserAttribute(dataEmail),
new CognitoUserAttribute(dataFirstName),
new CognitoUserAttribute(dataLastName),
]
userPool.signUp(email, password, attributeList, null, (error, result) => {
log(error, result)
if (error) {
// TODO this code shouldn't know about graphql errors. refactor to use separate layers
if (error.code === 'UsernameExistsException') {
reject(new UserError(ERRORS.EmailAlreadyTaken))
} else {
reject(error)
}
return
}
resolve({ id: result.userSub, email })
poolData = require('./poolData').poolData;
} else {
poolData = {
UserPoolId : process.env.REACT_APP_Auth_UserPoolId,
ClientId : process.env.REACT_APP_Auth_ClientId
};
}
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = {
Name : 'email',
Value : model.email
};
var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
attributeList.push(attributeEmail);
// Necessary becuase the closure has no access to this.props
let nestedProp = this.props;
let self = this
userPool.signUp(model.email, model.password, attributeList, null, function(err, result) {
if (err) {
toast.warn(err.message, {
position: "top-center",
autoClose: 4000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
});