Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const authInfo = yield call(api.getOauthInfo)
if (authInfo.ok) {
const { issuer, scope } = authInfo.data
const config = {
issuer,
clientId: '0oai6n2lojEIfg5pp0h7',
scopes: scope.split(' '),
redirectUrl: `${AppConfig.appUrlScheme}://authorize`
}
if (__DEV__ && Platform.OS === 'android') {
// this allows connections to a keycloak instance using http:// in dev
config.dangerouslyAllowInsecureHttpRequests = true
}
try {
// result includes accessToken, accessTokenExpirationDate and refreshToken
const authorizeResult = yield authorize(config)
const { accessToken } = authorizeResult
yield call(api.setAuthToken, accessToken)
yield put(LoginActions.loginSuccess(accessToken))
yield put(AccountActions.accountRequest())
yield put({ type: 'RELOGIN_OK' })
} catch (error) {
console.log(error)
yield put(LoginActions.loginFailure('WRONG'))
}
} else {
yield put(LoginActions.loginFailure('WRONG'))
}
}
const authInfo = yield call(api.getOauthInfo)
if (authInfo.ok) {
const { issuer, scope } = authInfo.data
const config = {
issuer,
clientId: '{yourClientId}',
scopes: scope.split(' '),
redirectUrl: `${AppConfig.appUrlScheme}://authorize`
}
if (__DEV__ && Platform.OS === 'android') {
// this allows connections to a keycloak instance using http:// in dev
config.dangerouslyAllowInsecureHttpRequests = true
}
try {
// result includes accessToken, accessTokenExpirationDate and refreshToken
const authorizeResult = yield authorize(config)
const { accessToken } = authorizeResult
yield call(api.setAuthToken, accessToken)
yield put(LoginActions.loginSuccess(accessToken))
yield put(AccountActions.accountRequest())
yield put({ type: 'RELOGIN_OK' })
} catch (error) {
console.log(error)
yield put(LoginActions.loginFailure('WRONG'))
}
} else {
yield put(LoginActions.loginFailure('WRONG'))
}
}
.then(creds => {
if (creds) {
const data = JSON.parse(creds.password);
const nowSeconds = (new Date()).getTime() / 1000;
const expiration = new Date(data.accessTokenExpirationDate).getTime() / 1000;
if (data.refreshToken && expiration < nowSeconds) {
return refresh(config, { refreshToken: data.refreshToken })
.then(
saveAuthResponse,
() => {
// Null token will produce an error where it is used.
return null;
});
}
return data.accessToken;
}
return null;
});
}
.then((resp) => {
// if 200 then token is valid, no need to review
if (resp.status !== 200) {
// if error attempt to renew access token
return refresh(refreshConfig, {
refreshToken: refreshToken
});
}
}, () => {
throw new Error('Failed to verify token');
onlyAuthorize = async () => {
try {
const authResult = await onlyAuthorize(config);
this.animateState(
{
authResult,
tokenResult: null
},
500
);
this.setState({
authResult,
tokenResult: null
});
} catch (error) {
Alert.alert('Failed to authorize', error.message);
}
return async (dispatch: Dispatch) => {
try {
dispatch({ type: SET_LOGIN_LOADING, payload: true });
const user = await authorize(oAuth.google);
try {
const response = await fetch(`${Config.API_AUTH}/auth/google/token?access_token=${user.accessToken}`);
dispatch({ type: SET_LOGIN_LOADING, payload: false });
if (!response.ok) throw new Error(response.status);
const data = await response.json();
dispatch({
type: SET_LOGIN_AUTH,
payload: {
socialNetwork: 'google',
loggedIn: true,
token: data.token,
oAuthToken: user.accessToken
}
});
} catch (e) {
console.error(e);
return async (dispatch: Dispatch, state: GetState) => {
const { oAuthToken: tokenToRevoke, socialNetwork } = state().user;
dispatch({ type: LOGOUT_REQUEST });
dispatch({ type: RESET_STATE });
try {
await CookieManager.clearAll();
const social = socialNetwork || socialNetworkFallback;
switch (social) {
case 'google': {
if (tokenToRevoke) {
await revoke(oAuth.google, { tokenToRevoke });
}
break;
}
case 'facebook':
await LoginManager.logOut();
break;
default:
break;
}
return dispatch({ type: SET_LOGIN_STATUS, payload: true });
} catch (e) {
console.error(e);
return dispatch({ type: SET_LOGIN_STATUS, payload: false });
}
};
}
authorize = async () => {
try {
const authState = await authorize(config);
this.animateState(
{
hasLoggedInOnce: true,
accessToken: authState.accessToken,
accessTokenExpirationDate: authState.accessTokenExpirationDate,
refreshToken: authState.refreshToken,
scopes: authState.scopes,
},
500
);
} catch (error) {
Alert.alert('Failed to log in', error.message);
}
};
export function signInFlow(): Promise {
return authorize(config)
.then(saveAuthResponse)
.then(() => {
return {
success: true,
};
}, (error: Error) => {
return {
success: false,
error: error.message || error,
};
});
}
const myAuthorize = () => authorize(config);