Permalink
Browse files

Merge pull request #11 from peri18/NODEJS-1103-GENERIC-METHOD-ACS

Nodejs 1103 providing a generic method for ACS rest api calls NODEJS-1133 Removing global variable to store session_id
  • Loading branch information...
2 parents e8aab23 + f35db9f commit dba329b85b17a531f5e3ec8b00e46745ed923209 @baobeimm baobeimm committed Nov 12, 2013
View
@@ -11,50 +11,51 @@ Usage
Example 1, do ACS user login:
-- var ACS = require('acs-node');
-- function login(req, res) {
-- var un = req.body.username;
-- var pw = req.body.password;
-- ACS.Users.login({login: un, password: pw}, function(data) {
-- if(data.success) {
-- var user = data.users[0];
-- if(user.first_name && user.last_name) {
-- user.name = user.first_name + ' ' + user.last_name;
-- } else {
-- user.name = user.username;
-- }
-- req.session.user = user;
-- res.redirect('/');
-- } else {
-- res.render('login', {message: data.message});
-- }
-- });
-- }
+~~~
+var ACS = require('acs-node');
+ACS.initACS('<App Key>');
+function login(req, res) {
+ var data = {
+ login: req.body.username,
+ password: req.body.password
+ };
+ ACS.Users.login(data, function(data){
+ if(data.success) {
+ console.log("Successful to login.");
+ console.log("UserInfo: " + JSON.stringify(data.users[0], null, 2))
+ } else {
+ console.log("Error to login: " + data.message);
+ }
+ }, req, res);
+}
+~~~
Example 2, a generic method show how to operate an ACS user:
-- var ACS = require('acs-node');
-- var sdk = ACS.initACS('', '');
-- var user_id = null;
-- var useSecure = true;
-- sdk.sendRequest('users/create.json', 'POST', {
-- username:'test1',
-- password:'test1',
-- password_confirmation:'test1',
-- first_name: 'test_firstname',
-- last_name: 'test_lastname'
-- }, function(data){
-- user_id = data.response.users[0].id;
-- sdk.sendRequest('users/logout.json', 'DELETE',null, function(data){
-- sdk.sendRequest('users/login.json', 'POST', {login:'test1', password:'test1'}, function(data){
-- sdk.sendRequest('users/update.json', 'PUT', {first_name: 'firstname'}, function(data){
-- }, useSecure);
-- }, useSecure);
-- }, useSecure);
-- }, useSecure);
-
-
-More examples, please look up in the folder test.
+~~~
+var ACS = require('acs-node');
+var sdk = ACS.initACS('<App Key>');
+function login(req, res) {
+ var data = {
+ login: req.body.username,
+ password: req.body.password
+ };
+ sdk.rest('users/login.json', 'POST', data, function(data){
+ if(data && data.meta) {
+ if(data.meta.status == 'ok') {
+ console.log("Successful to login.");
+ console.log("UserInfo: " + JSON.stringify(data.response.users[0], null, 2))
+ } else {
+ console.log("Error to login: " + data.meta.message);
+ }
+ } else {
+ console.log("Error to login, try again later.");
+ }
+ }, req, res);
+}
+~~~
+
+More examples, please look up in the folder examples with the command 'acs run'.
Legal
@@ -0,0 +1 @@
+This sample Node.ACS MVC based project uses node-sdk building user api to communicate with ACS.
@@ -0,0 +1,14 @@
+// initialize app
+function start(app, express) {
+ //use connect.session
+ app.use(express.cookieParser());
+ app.use(express.session({ key: 'node.acs', secret: "my secret" }));
+
+ //set favicon
+ app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
+}
+
+// release resources
+function stop() {
+
+}
@@ -0,0 +1,13 @@
+{
+ "routes":
+ [
+ { "path": "/", "callback": "application#index" },
+ { "path": "/login", "method": "get", "callback": "application#login" },
+ { "path": "/login", "method": "post", "callback": "user#login" },
+ { "path": "/update", "method": "get", "callback": "application#update" },
+ { "path": "/update", "method": "post", "callback": "user#update" },
+ { "path": "/logout", "method": "get", "callback": "user#logout" },
+ { "path": "/signup", "method": "get", "callback": "application#signup" },
+ { "path": "/signup", "method": "post", "callback": "user#signup" }
+ ]
+}
@@ -0,0 +1,15 @@
+function index(req, res) {
+ res.render('index', {user: req.session.user});
+}
+
+function login(req, res) {
+ res.render('login');
+}
+
+function update(req, res) {
+ res.render('update');
+}
+
+function signup(req, res) {
+ res.render('signup');
+}
@@ -0,0 +1,96 @@
+var ACS = require('acs').ACS;
+var logger = require('acs').logger;
+var sdk = ACS.initACS('<App Key>');
+
+//create a new user
+function signup(req, res) {
+ var data = {
+ first_name: req.body.first_name,
+ last_name: req.body.last_name,
+ email: req.body.email,
+ password: req.body.password,
+ password_confirmation: req.body.password_confirmation
+ };
+
+ ACS.Users.create(data, function(data) {
+ logger.debug("######Create######");
+ logger.debug(JSON.stringify(data, null, 2));
+ if(data.success) {
+ var user = data.users[0];
+ if(user.first_name && user.last_name) {
+ user.name = user.first_name + ' ' + user.last_name;
+ } else {
+ user.name = user.username;
+ }
+ logger.info('Created user: ' + user.name);
+ req.session.user = user;
+ res.redirect('/');
+ } else {
+ logger.info("Create user error: " + data.meta.message);
+ res.render('signup', {message: data.message});
+ }
+ }, req, res);
+}
+
+//do ACS user login
+function login(req, res) {
+ var data = {
+ login: req.body.username,
+ password: req.body.password
+ };
+ ACS.Users.login(data, function(data){
+ logger.debug("######login######");
+ logger.debug(JSON.stringify(data, null, 2));
+ if(data.success) {
+ var user = data.users[0];
+ if(user.first_name && user.last_name) {
+ user.name = user.first_name + ' ' + user.last_name;
+ } else {
+ user.name = user.username;
+ }
+ req.session.user = user;
+ res.redirect('/');
+ } else {
+ logger.info("Login error: " + data.meta.message);
+ res.render('login', {message: data.meta.message});
+ }
+ }, req, res);
+}
+
+function update(req, res) {
+ var data = {
+ first_name: req.body.first_name,
+ last_name: req.body.last_name,
+ };
+ ACS.Users.update(data, function(data) {
+ logger.debug("######update######");
+ logger.debug(JSON.stringify(data, null, 2));
+ if(data.success) {
+ var user = data.users[0];
+ if(user.first_name && user.last_name) {
+ user.name = user.first_name + ' ' + user.last_name;
+ } else {
+ user.name = user.username;
+ }
+ req.session.user = user;
+ res.redirect('/');
+ } else {
+ logger.info("Update user error: " + data.meta.message);
+ res.render('update', {message: data.meta.message});
+ }
+ }, req, res);
+}
+
+function logout(req, res) {
+ ACS.Users.logout(function(data) {
+ logger.debug("######logout######");
+ logger.debug(JSON.stringify(data, null, 2));
+ if(data.success) {
+ delete req.session.user;
+ res.redirect('/');
+ } else {
+ logger.info("Logout error: " + data.meta.message);
+ res.render('/', {message: data.meta.message});
+ }
+ }, req, res);
+}
No changes.
@@ -0,0 +1,24 @@
+{
+ "name": "UserWithACSAPI",
+ "version": "0.1.0",
+ "description": "",
+ "framework": "mvc",
+ "main": "app.js",
+ "logfile" : "users.log",
+ "keywords": [
+ ],
+ "author": "",
+ "maintainers": [
+ ],
+ "contributors": [
+ ],
+ "repository": {
+ "type": "git",
+ "url": ""
+ },
+ "preferGlobal": false,
+ "dependencies": {
+ },
+ "license":"",
+ "private": true
+}
@@ -0,0 +1,102 @@
+* {
+ font:400 14px/20px "helvetica neue", arial, sans-serif;
+}
+
+.clr{clear:both;}
+
+#ic {
+ width:100%;
+ border:1px solid #ccc;
+ border-bottom:0;
+}
+#ic span{
+ display:block;
+ padding:5px 10px;
+}
+
+#ctnr{
+ padding:10px;
+}
+
+#chat-ctnr {
+ width:100%;
+ height:70%;
+ overflow:auto;
+ border:1px solid #ccc;
+}
+
+#incomingChatMessages {
+ padding:5px;
+ margin:0;
+ list-style:none;
+}
+
+#incomingChatMessages li{
+ position:relative;
+ width:100%;
+}
+
+#incomingChatMessages li .icmb{
+ padding:2px;
+}
+
+#incomingChatMessages li .cu{
+ float:left;
+ width:15%;
+ margin:2px 1% 2px 0;
+}
+
+#incomingChatMessages li .cu span{
+ display:block;
+ padding:3px;
+}
+
+#incomingChatMessages li .cm{
+ width:83%;
+ float:left;
+ -webkit-border-radius:3px;
+ -moz-border-radius:3px;
+ border-radius:3px;
+ background:#f5f5f5;
+ margin:2px 0;
+ transition:all .3s;
+ -moz-transition:all .3s;
+ -webkit-transition:all .3s;
+ -o-transition:all .3s;
+}
+#incomingChatMessages li .cm span{
+ display:block;
+ float:left;
+ padding:3px;
+}
+
+#incomingChatMessages li .cm time{
+ float:right;
+ background:#fff;
+ margin:2px 3px 0;
+ font:400 10px/14px "helvetica neue", arial, sans-serif;
+ -webkit-border-radius:3px;
+ -moz-border-radius:3px;
+ border-radius:3px;
+}
+
+#incomingChatMessages li.alert span{
+ font-weight:bold;
+ padding:4px;
+}
+
+#incomingChatMessages li.active {
+ background:#ddffdd;
+}
+
+#outgoingChatMessage {
+ margin:5px 0;
+ padding:5px;
+ outline:none;
+ border:1px solid #ccc;
+ width:100%;
+}
+
+#outgoingChatMessage:focus {
+ border:1px solid #1c94c4;
+}
Binary file not shown.
@@ -0,0 +1,15 @@
+<html>
+<body>
+<% var message;
+ if(message) { %>
+ <%= message %>
+<% } %>
+
+<% if(user) {%>
+ <div>Welcome, <%= user.name %>! <a href="/logout">logout</a></div>
+ <a href="/update">update your info</a>
+<% } else {%>
+ <%- include login %>
+<% } %>
+</body>
+</html>
@@ -0,0 +1,11 @@
+<% var message;
+ if(message) { %>
+ <%= message %>
+<% } %>
+
+<form action="/login" method="post">
+<div><input name="username" placeholder="user name" title="username" required/></div>
+<div><input name="password" placeholder="password" type="password" title="password" required/></div>
+<div><input type="submit" value="login"/></div>
+</form>
+<div><a href="/signup">signup</div>
Oops, something went wrong.

0 comments on commit dba329b

Please sign in to comment.