var app = angular.module('drupal', ['ngCookies']);
app.controller('MainCtrl',
['$scope', 'drupalServices', '$cookies',
function($scope, drupalServices, $cookies) {
$scope.token = null;
$scope.tokenCookie = $cookies['xsrfTOKEN'];
$scope.login = null;
$scope.user = {
name: 'web',
pass: 'Un1v3r53'
};
$scope.svc = {
getToken: function() {
drupalServices
.getToken()
.then(function(data) {
$cookies['xsrfTOKEN'] = data.token;
$scope.token = data.token;
});
},
login: function(user) {
drupalServices
.userLogin(user.name, user.pass)
.then(function(data) {
$scope.user = data;
});
},
createReferral: function(repId, custId, token) {
drupalServices
.postReferral(repId, custId, token)
.then(function(data) {
$scope.referral = data;
});
}
}
/*
drupalServices
.userLogout();
drupalServices
.userLogin('web', 'Un1v3r53')
.then(function(data) {
$scope.user = data;
});
*/
}
]
);
app.factory('drupalServices', ['$http','$q', function ($http, $q) {
return {
userLogout: function () {
var config = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'accept': 'application/json'
},
url: 'http://dev-123-lump-sum-cms.pantheon.io/rest/user/logout'
};
return $http(config)
.then(function(result) {
//return a promise to that data.
return result.data;
});
},
getToken: function () {
var url = 'http://dev-123-lump-sum-cms.pantheon.io/rest/user/token',
data = {},
config = {
headers: {
'Content-Type': 'application/json'
}
};
return $http.post(url, data, config)
.then(function(result) {
return result.data;
});
},
postReferral: function (repId, custId, token) {
var url = 'http://dev-123-lump-sum-cms.pantheon.io/node',
data = {
"type": "panopoly_news_article",
"title": "posted article",
"body":{
"und":[
{
"value":"This is the body of the article."
}
]
}
},
config = {
xsrfHeaderName: 'X-CSRF-Token',
xsrfCookieName: 'xsrfTOKEN',
withCredentials: true,
headers: {
'Content-Type': 'application/json'
}
};
return $http.post(url, data, config)
.then(function(result) {
return result.data;
});
},
userLogin: function (usr, pass) {
var config = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
"username": usr,
"password": pass
},
url: 'http://dev-123-lump-sum-cms.pantheon.io/rest/user/login'
};
return $http(config)
.then(function(result) {
return result.data;
});
}
}
}]);
<!DOCTYPE html>
<html ng-app="drupal">
<head>
<meta charset="utf-8" />
<title>Drupal logout, get token, login and post content.</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script src="http://code.angularjs.org/1.3.16/angular.min.js"></script>
<script src="http://code.angularjs.org/1.3.16/angular-cookies.js"></script>
</head>
<body ng-controller="MainCtrl">
<h3>Token</h3>
<pre>{{token | json}}</pre>
<pre>{{tokenCookie | json}}</pre>
<button ng-click="token = svc.getToken()">Get Token</button>
<h3>User</h3>
<pre>{{user | json}}</pre>
<input type="text" ng-model="user.name" />
<input type="text" ng-model="user.pass" />
<button ng-click="user = svc.login(user)">Login</button>
<button ng-click="svc.userLogout()">Logout</button>
<h3>App State</h3>
<pre>{{login | json}}</pre>
<button ng-click="svc.createReferral('joma', '123', login.token)">Create Referral</button>
<script>
var app = angular.module('drupal', ['ngCookies']);
app.controller('MainCtrl',
['$scope', 'drupalServices', '$cookies',
function($scope, drupalServices, $cookies) {
$scope.token = null;
$scope.tokenCookie = $cookies['xsrfTOKEN'];
$scope.login = null;
$scope.user = {
name: 'web',
pass: 'Un1v3r53'
};
$scope.svc = {
getToken: function() {
drupalServices
.getToken()
.then(function(data) {
$cookies['xsrfTOKEN'] = data.token;
$scope.token = data.token;
});
},
login: function(user) {
drupalServices
.userLogin(user.name, user.pass)
.then(function(data) {
$scope.user = data;
});
},
createReferral: function(repId, custId, token) {
drupalServices
.postReferral(repId, custId, token)
.then(function(data) {
$scope.referral = data;
});
}
}
/*
drupalServices
.userLogout();
drupalServices
.userLogin('web', 'Un1v3r53')
.then(function(data) {
$scope.user = data;
});
*/
}
]
);
app.factory('drupalServices', ['$http','$q', function ($http, $q) {
return {
userLogout: function () {
var config = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'accept': 'application/json'
},
url: 'http://dev-123-lump-sum-cms.pantheon.io/rest/user/logout'
};
return $http(config)
.then(function(result) {
//return a promise to that data.
return result.data;
});
},
getToken: function () {
var url = 'http://dev-123-lump-sum-cms.pantheon.io/rest/user/token',
data = {},
config = {
headers: {
'Content-Type': 'application/json'
}
};
return $http.post(url, data, config)
.then(function(result) {
return result.data;
});
},
postReferral: function (repId, custId, token) {
var url = 'http://dev-123-lump-sum-cms.pantheon.io/node',
data = {
"type": "panopoly_news_article",
"title": "posted article",
"body":{
"und":[
{
"value":"This is the body of the article."
}
]
}
},
config = {
xsrfHeaderName: 'X-CSRF-Token',
xsrfCookieName: 'xsrfTOKEN',
withCredentials: true,
headers: {
'Content-Type': 'application/json'
}
};
return $http.post(url, data, config)
.then(function(result) {
return result.data;
});
},
userLogin: function (usr, pass) {
var config = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
"username": usr,
"password": pass
},
url: 'http://dev-123-lump-sum-cms.pantheon.io/rest/user/login'
};
return $http(config)
.then(function(result) {
return result.data;
});
}
}
}]);
</script>
</body>
</html>
/* Put your css in here */