comparison rlgwebd.js @ 19:188bbd857124

RLG-Web: add user registration Update the server and client sides of RLG-Web to make use of the new registration feature of sqlickrypt.
author John "Elwin" Edwards <elwin@sdf.org>
date Tue, 22 May 2012 20:54:33 -0700
parents d3e3d6b4016b
children 5f785e1d5cca
comparison
equal deleted inserted replaced
18:59ea628abb81 19:188bbd857124
319 // If progressdir isn't readable, start a new game anyway. 319 // If progressdir isn't readable, start a new game anyway.
320 startgame(); 320 startgame();
321 }); 321 });
322 } 322 }
323 /* Launch the sqlickrypt utility to check the password. */ 323 /* Launch the sqlickrypt utility to check the password. */
324 var checker = require('child_process').spawn("/bin/sqlickrypt"); 324 var checker = child_process.spawn("/bin/sqlickrypt", ["check"]);
325 checker.on("exit", checkit); 325 checker.on("exit", checkit);
326 checker.stdin.end(username + '\n' + password + '\n', "utf8"); 326 checker.stdin.end(username + '\n' + password + '\n', "utf8");
327 return;
328 }
329
330 function register(req, res, formdata) {
331 var uname, passwd, email;
332 if (typeof (formdata.name) != "string" || formdata.name === "") {
333 sendError(res, 2, "No name given.");
334 return;
335 }
336 else
337 uname = formdata["name"];
338 if (typeof (formdata.pw) != "string" || formdata.pw === "") {
339 sendError(res, 2, "No password given.");
340 return;
341 }
342 else
343 passwd = formdata["pw"];
344 if (typeof (formdata.email) != "string" || formdata.email === "") {
345 /* E-mail is optional */
346 email = "nobody@nowhere.not";
347 }
348 else
349 email = formdata["email"];
350 function checkreg(code, signal) {
351 if (code == 4)
352 sendError(res, 2, "Invalid characters in name or email.");
353 else if (code == 1)
354 sendError(res, 2, "Username " + uname + " is already being used.");
355 else if (code != 0)
356 sendError(res, 0, null);
357 else {
358 res.writeHead(200, {'Content-Type': 'text/plain'});
359 var reply = {"t": "r", "d": uname};
360 res.write(JSON.stringify(reply));
361 res.end();
362 console.log("Added new user: " + uname);
363 }
364 }
365 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]);
366 child_adder.on("exit", checkreg);
367 child_adder.stdin.end(uname + '\n' + passwd + '\n' + email + '\n', "utf8");
327 return; 368 return;
328 } 369 }
329 370
330 function logout(term, res) { 371 function logout(term, res) {
331 if (!term.alive) { 372 if (!term.alive) {
480 readFeed(res, cterm); 521 readFeed(res, cterm);
481 } 522 }
482 else if (target == "/login") { 523 else if (target == "/login") {
483 login(req, res, formdata); 524 login(req, res, formdata);
484 } 525 }
526 else if (target == "/addacct") {
527 register(req, res, formdata);
528 }
485 else { 529 else {
486 res.writeHead(405, resheaders); 530 res.writeHead(405, resheaders);
487 res.end(); 531 res.end();
488 } 532 }
489 } 533 }