//LoginScreen.js
import signIn from "amplify-communication"
const LoginScreen = props => {
function _signIn(username, password){
const request = signIn(username, password);
if(request.isCommited)
console.log("User is Signed In");
else
console.log("Error Signing In", request.message);
}
}
// amplify-communication.js
import { Auth } from "aws-amplify";
export async function signIn(_username, _password) {
try {
var user = await Auth.signIn(_username, _password);
} catch (error) {
console.log('Error Signing in');
}
finally {
return {
isCommitted: error ? false : true,
message: error,
payload: user
}
}
}
I have separate file for communicating with Amplify. I am returning an object with different keys. On running the code it shows me following warnings.
1 - Possible Unhandled Promise Rejection (id: 0): ReferenceError: error is not defined ReferenceError: error is not defined
2- Using an insecure random number generator, this should only happen when running in a debugger without support for crypto.getRandomValues
CodePudding user response:
error
is only scoped to the catch
block, so when you try to use error
in the finally
block, it's an undeclared identifier. You have the same problem with user
on the success path.
Instead, either track that information in variables or repeat the return
statement.
export async function signIn(_username, _password) {
try {
const user = await Auth.signIn(_username, _password);
return {
isCommitted: true,
payload: user,
message: undefined, // if you need it, otherwise remove
};
} catch (error) {
console.log('Error Signing in');
return {
iscCommitted: false,
payload: undefined, // if you need it, otherwise remove
message: error,
};
}
}
The thing about the random number generator sounds like it's just a warning from the package you're using about the environment in which you're running the code (e.g., that it doesn't have a strong random number generator).
Side note: It can be correct to turn errors into return values like that, but more times than not it's more correct to allow the error to propagate to the caller (and perhaps to its, caller, etc.) rather than forcing every level to check a return value to see if something worked. Again, without knowing what this function is for, that may or may not apply here, but just flagging it up.