Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async init() {
try {
const user = await Auth.currentAuthenticatedUser()
this.username = user.username
this.email = user.signInUserSession.idToken.payload.email
} catch (err) {
console.log('error getting user data... ', err)
}
console.log('username:', this.username)
// check if user exists in db, if not then create user
if (this.username !== '') {
this.checkIfUserExists(this.username)
}
}
return function(dispatch) {
console.log('actions.validateUserSession()');
Auth.currentAuthenticatedUser()
.then(currentAuthUser => {
console.log('actions.validateUserSession():Auth.currentAuthenticatedUser() currentAuthUser:', currentAuthUser);
// grab the user session
Auth.userSession(currentAuthUser)
.then(session => {
console.log('actions.validateUserSession():Auth.userSession() session:', session);
// finally invoke isValid() method on session to check if auth tokens are valid
// if tokens have expired, lets call "logout"
// otherwise, dispatch AUTH_USER success action and by-pass login screen
if (session.isValid()) {
// fire user is authenticated
dispatch({ type: AUTH_USER });
//history.push('/');
} else {
// fire user is unauthenticated
dispatch({ type: UNAUTH_USER });
const checkLoggedIn = () => {
Auth.currentAuthenticatedUser()
.then(data => {
const user = { username: data.username, ...data.attributes };
setState({ isLoggedIn: true, user });
})
.catch(error => console.log(error));
};
async function checkUser(dispatch) {
try {
const user = await Auth.currentAuthenticatedUser()
console.log('user: ', user)
dispatch({ type: 'setUser', user })
} catch (err) {
console.log('err: ', err)
dispatch({ type: 'loaded' })
}
}
checkUser = async () => {
await Auth.currentAuthenticatedUser()
.then((user) => {
this.props.navigation.navigate(user ? 'App' : 'Auth');
console.log('Cognito: ', user);
})
.catch((error) => {
this.props.navigation.navigate('Auth');
console.log(error.message);
});
};
const _accountDevices = await currentUser.sdk.getConnectedAccountDevices();
setIsThisDeviceAdded(
_accountDevices.items.some(
(item) =>
item.device.address === currentUser.sdk.state.deviceAddress,
),
);
setAccountDevices(
_accountDevices.items.filter((item) => {
return (
item.device.address !== currentUser.sdk.state.deviceAddress
);
}),
);
const user = await Auth.currentAuthenticatedUser();
const userAttributes = await Auth.userAttributes(user);
setWaitingSdk(false);
if (
userAttributes.find((item) => item.Name === 'custom:named_devices')
) {
setParsedNamedDevices(
JSON.parse(
userAttributes.find(
(item) => item.Name === 'custom:named_devices',
).Value,
),
);
}
} catch (error) {
console.error(error);
setWaitingSdk(false);
async isUserSignedIn() {
try {
const userObj = await Auth.currentAuthenticatedUser();
this.signedIn = true;
console.log(userObj);
} catch (err) {
this.signedIn = false;
console.log(err);
}
},
loginUser() {
checkUser() {
const { authState } = this.state;
const statesJumpToSignIn = ['signedIn', 'signedOut', 'loading'];
Auth.currentAuthenticatedUser()
.then(user => {
if (!this._isMounted) return;
if (user) {
this.checkContact(user);
} else {
if (statesJumpToSignIn.includes(authState)) {
this.handleStateChange(this._initialAuthState, null);
}
}
})
.catch(err => {
if (!this._isMounted) return;
logger.debug(err);
if (statesJumpToSignIn.includes(authState)) {
Auth.signOut()
.then(() => {
checkUser() {
const that = this;
const { authState } = this.state;
return Auth.currentAuthenticatedUser()
.then(user => {
if (!that._isMounted) { return; }
if (authState !== 'signedIn') {
this.setState({
authState: 'signedIn',
authData: user
});
this.changeState('signedIn', user);
}
})
.catch(err => {
if (!that._isMounted) { return; }
if (!authState || authState === 'signedIn') {
this.setState({ authState: 'signIn' });
this.changeState('signIn');
}
checkUser() {
Auth.currentAuthenticatedUser()
.then(user => {
const state = user? 'signedIn' : 'signIn';
this.handleStateChange(state, user)
})
.catch(err => {
this.handleStateChange('signIn', null);
logger.error(err);
});
}