Skip to content

Instantly share code, notes, and snippets.

@cvega93
Created August 2, 2018 20:59
Show Gist options
  • Save cvega93/8259a7b1486f5ba58926560ce8ddefb2 to your computer and use it in GitHub Desktop.
Save cvega93/8259a7b1486f5ba58926560ce8ddefb2 to your computer and use it in GitHub Desktop.
Promise based SDK Module
export default class {
constructor(options) {
this.req_scope = ['email', 'birth_day'];
this.scope = ['email'];
this.options = options;
if (process.browser) {
this._initFacebookSDK();
window.fbAsyncInit = function onSDKInit() {
FB.init(options);
FB.AppEvents.logPageView();
window.dispatchEvent(new Event('fb-sdk-ready'))
}
}
}
sdk_loaded() {
return new Promise((resolve) => {
window.addEventListener('fb-sdk-ready', function () {
resolve()
})
})
}
async login() {
return new Promise((resolve, reject) => {
this._loginAttemp().then((user) => {
console.log('INTENTENADO LOGIN')
user.status ? resolve(user) : reject(user)
})
})
}
logout() {
FB.logout(function (response) {
console.log(response)
})
}
checkLoginStatus() {
return new Promise((resolve, reject) => {
FB.getLoginStatus((response) => {
if (response.status === 'connected') {
this._permissionsRejected().then((res) => {
if (res) {
reject(res.message)
} else {
resolve(response)
}
})
} else {
resolve()
}
})
})
}
getUserPermissions() {
return new Promise((resolve) => {
FB.api('/me/permissions/', function (response) {
resolve(response)
})
})
}
_initFacebookSDK() {
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/es_ES/sdk/xfbml.customerchat.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
_loginAttemp() {
return new Promise((resolve, rejecet) => {
const options = {
scope: 'public_profile, email',
return_scopes: true,
auth_type: 'rerequest'
};
FB.login((response) => {
console.log('DISPARANDO MODAL FACEBOOK LOGIN')
this._validateLoginResponse(response).then(resolve)
}, options)
})
}
async _validateLoginResponse(response) {
//Si el usuario cierra el popup se ejecuta este error
if (response.status != 'connected') return {'Atención': ['El login ha sido interrumpido']}
//Si hay permisos obligatorios no concedidos se retorna un mensaje de error.
const permissionsRejected = await this._permissionsRejected()
if (permissionsRejected) return permissionsRejected.message
//Si no se encontraron errores devuelve la respuesta original.
return response
}
async _permissionsRejected() {
const permissions = await this.getUserPermissions()
var permissions_rejected = []
return new Promise((resolve, reject) => {
this.req_scope.forEach(function (req) {
permissions_rejected.push(permissions.data.find(function (el) {
if (el.permission == req && el.status != 'granted') {
return true
}
}))
})
if (permissions_rejected[0] != undefined) {
resolve({
permissions_rejected: permissions_rejected,
message: {'Atención': ['El campo Email es obligatorio para usar MonkeyFit']}
})
} else {
resolve(false)
}
})
}
}
import Facebook_sdk from '@/components/Facebook/facebook_SDK'
const facebook = new Facebook_sdk({
appId: [YOUR_APP_ID],
autoLogAppEvents: true,
xfbml: true,
version: 'v2.10',
status: true
});
facebook.login()
.then((response) => {
})
.catch((error) => {
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment