Merge pull request #153 from Stremio/fb-script

Fb script
This commit is contained in:
Nikola Hristov 2020-04-03 17:36:12 +03:00 committed by GitHub
commit ef4f59954c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 42 deletions

View file

@ -13,17 +13,6 @@
<body> <body>
<div id="app"></div> <div id="app"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '1537119779906825',
autoLogAppEvents : false,
xfbml : false,
version : 'v2.5'
});
};
</script>
<script async defer src="https://connect.facebook.net/en_US/sdk.js"></script>
<script type="text/javascript"> <script type="text/javascript">
<%= compilation.assets['main.js'].source() %> <%= compilation.assets['main.js'].source() %>
</script> </script>

View file

@ -75,39 +75,41 @@ const Intro = ({ queryParams }) => {
} }
); );
const loginWithFacebook = React.useCallback(() => { const loginWithFacebook = React.useCallback(() => {
FB.login((response) => { if (typeof FB !== 'undefined') {
if (response.status === 'connected') { FB.login((response) => {
fetch('https://www.strem.io/fb-login-with-token/' + encodeURIComponent(response.authResponse.accessToken), { timeout: 10 * 60 * 1000 }) if (response.status === 'connected') {
.then((resp) => { fetch('https://www.strem.io/fb-login-with-token/' + encodeURIComponent(response.authResponse.accessToken), { timeout: 10 * 60 * 1000 })
if (resp.status < 200 || resp.status >= 300) { .then((resp) => {
throw new Error('Login failed at getting token from Stremio with status ' + resp.status); if (resp.status < 200 || resp.status >= 300) {
} else { throw new Error('Login failed at getting token from Stremio with status ' + resp.status);
return resp.json(); } else {
} return resp.json();
})
.then(({ user }) => {
if (!user || typeof user.fbLoginToken !== 'string' || typeof user.email !== 'string') {
throw new Error('Login failed at getting token from Stremio');
}
core.dispatch({
action: 'Ctx',
args: {
action: 'Authenticate',
args: {
type: 'Login',
email: user.email,
password: user.fbLoginToken
}
} }
})
.then(({ user }) => {
if (!user || typeof user.fbLoginToken !== 'string' || typeof user.email !== 'string') {
throw new Error('Login failed at getting token from Stremio');
}
core.dispatch({
action: 'Ctx',
args: {
action: 'Authenticate',
args: {
type: 'Login',
email: user.email,
password: user.fbLoginToken
}
}
});
})
.catch((err = {}) => {
dispatch({ type: 'error', error: err.message || JSON.stringify(err) });
}); });
}) } else {
.catch((err = {}) => { dispatch({ type: 'error', error: 'Login failed at getting token from Facebook' });
dispatch({ type: 'error', error: err.message || JSON.stringify(err) }); }
}); });
} else { }
dispatch({ type: 'error', error: 'Login failed at getting token from Facebook' });
}
});
}, []); }, []);
const loginWithEmail = React.useCallback(() => { const loginWithEmail = React.useCallback(() => {
if (typeof state.email !== 'string' || state.email.length === 0 || !emailRef.current.validity.valid) { if (typeof state.email !== 'string' || state.email.length === 0 || !emailRef.current.validity.valid) {
@ -272,6 +274,27 @@ const Intro = ({ queryParams }) => {
core.off('Event', onEvent); core.off('Event', onEvent);
}; };
}, [routeFocused]); }, [routeFocused]);
React.useEffect(() => {
var initScriptElement = document.createElement('script');
var sdkScriptElement = document.createElement('script');
initScriptElement.innerHTML = `window.fbAsyncInit = function() {
FB.init({
appId: '1537119779906825',
autoLogAppEvents: false,
xfbml: false,
version: 'v2.5'
});
};`;
sdkScriptElement.src = 'https://connect.facebook.net/en_US/sdk.js';
sdkScriptElement.async = true;
sdkScriptElement.defer = true;
document.body.appendChild(initScriptElement);
document.body.appendChild(sdkScriptElement);
return () => {
document.body.removeChild(initScriptElement);
document.body.removeChild(sdkScriptElement);
};
}, []);
return ( return (
<div className={styles['intro-container']}> <div className={styles['intro-container']}>
<div className={styles['form-container']}> <div className={styles['form-container']}>