Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function transform(model) {
- var userInfo = model.toJSON();
- var output = {
- "email" : userInfo.email,
- "name" : userInfo.name,
- "wpId" : userInfo.id,
- "id" : model.cid,
- "first_name": userInfo.first_name,
- "last_name": userInfo.last_name,
- "madlavning": userInfo.madlavning
- };
- console.log('output' + output);
- return output;
- }
- var fb = require('facebook');
- fb.requestWithGraphPath('me', {fields: 'id'} , 'GET', function(e) {
- if(e.success) {
- Alloy.Collections.instance("opgaver");
- Alloy.Collections.instance("wpuser");
- Alloy.Collections.instance("opgaver").fetch();
- Alloy.Collections.instance("wpuser").fetch();
- user = Alloy.Collections.wpuser.where({name: fbId});
- var fbId = JSON.parse(e.result);
- Alloy.createController("opdag", user).opdag.open();
- }else
- {
- }
- });
- fb.addEventListener('login', function(e) {
- if (e.success === true) {
- fb.requestWithGraphPath('me', {fields: 'id, name, email, location, picture.type(large), birthday'} , 'GET', function(e) {
- if(e.success) {
- var params = JSON.stringify({
- email: JSON.parse(e.result).email,
- username: JSON.parse(e.result).id,
- password: 'password1234'
- });
- var userdata = {
- "email": JSON.parse(e.result).email,
- "username": JSON.parse(e.result).id,
- "profil_billede": JSON.parse(e.result).picture.data.url,
- "first_name": JSON.parse(e.result).first_name,
- "last_name": JSON.parse(e.result).last_name,
- "birthday": JSON.parse(e.result).birthday,
- "location": JSON.parse(e.result).location.name
- };
- console.log(JSON.stringify(userdata));
- var base64 = Titanium.Utils.base64encode('admin' + ':' + '!ealweb/dev#17');
- var xhr = Ti.Network.createHTTPClient();
- var fbId = JSON.parse(e.result).id;
- xhr.open('POST','http://webdev.hilmarsdottir.com/wp-json/wp/v2/users');
- xhr.setRequestHeader('Content-type', 'application/json');
- xhr.setRequestHeader('Authorization','Basic ' + base64);
- xhr.setTimeout(200000);
- xhr.onload = function(e) {
- user = Alloy.Collections.wpuser.where({name: fbId});
- Alloy.createController("opgaver", user).opdag.open();
- //alert('success ' + JSON.parse(this.responseText));
- };
- xhr.onerror = function(e){
- if(JSON.parse(this.responseText).code === 'existing_user_login') {
- user = Alloy.Collections.wpuser.where({name: fbId});
- Alloy.Collections.instance("opgaver");
- Alloy.Collections.instance("wpuser");
- Alloy.createController("opdag", user).opdag.open();
- Alloy.Collections.instance("opgaver").fetch();
- Alloy.Collections.instance("wpuser").fetch();
- //alert('You shall pass');
- } else {
- Ti.API.debug(e.error);
- fb.logout();
- alert(JSON.parse(this.responseText) + ' : ' + e.error + ' Get out - Add a logout function');
- }
- };
- xhr.send(params);
- }
- else if (e.error) {
- alert(e.error);
- }
- else {
- alert('unknown response');
- }
- });
- //label.text = 'Logged In = ' + fb.loggedIn;
- }
- else if (e.cancelled) {
- //user cancelled
- alert('cancelled');
- }
- else {
- alert(e.error);
- }
- });
- $.loginWin.fbProxy = fb.createActivityWorker({lifecycleContainer: $.loginWin});
- $.loginWin.open();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement