mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
refactoring register to use form post instead of socket (for passport integration)
This commit is contained in:
@@ -41,7 +41,7 @@ var templates = {};
|
|||||||
function init() {
|
function init() {
|
||||||
loadTemplates([
|
loadTemplates([
|
||||||
'header', 'footer', 'register', 'home', 'topic',
|
'header', 'footer', 'register', 'home', 'topic',
|
||||||
'login', 'reset', 'reset_code', 'account_settings',
|
'login', 'reset', 'reset_code',
|
||||||
'emails/reset', 'emails/reset_plaintext'
|
'emails/reset', 'emails/reset_plaintext'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
<h1>Register</h1>
|
<h1>Register</h1>
|
||||||
<div class="well">
|
<div class="well">
|
||||||
<label for="email">Email Address</label><input type="email" placeholder="Enter Email Address" id="email" /> <span id="email-notify" class="label label-important"></span> <br />
|
<form method="post" action="/register">
|
||||||
<label for="username">Username</label><input type="text" placeholder="Enter Username" id="username" /> <span id="username-notify" class="label label-success"></span> <br />
|
<label for="email">Email Address</label><input type="email" name="email" placeholder="Enter Email Address" id="email" /> <span id="email-notify" class="label label-important"></span> <br />
|
||||||
<label for="password">Password</label><input type="password" placeholder="Enter Password" id="password" /><br />
|
<label for="username">Username</label><input type="text" name="username" placeholder="Enter Username" id="username" /> <span id="username-notify" class="label label-success"></span> <br />
|
||||||
<button class="btn btn-primary" id="register" type="submit">Register Now</button>
|
<label for="password">Password</label><input type="password" name="password" placeholder="Enter Password" id="password" /><br />
|
||||||
|
<button class="btn btn-primary" id="register" type="submit">Register Now</button>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
(function() {
|
(function() {
|
||||||
@@ -16,14 +18,6 @@
|
|||||||
username_notify = document.getElementById('username-notify'),
|
username_notify = document.getElementById('username-notify'),
|
||||||
email_notify = document.getElementById('email-notify');
|
email_notify = document.getElementById('email-notify');
|
||||||
|
|
||||||
register.onclick = function() {
|
|
||||||
socket.emit('user.create', {
|
|
||||||
username: username.value,
|
|
||||||
password: password.value,
|
|
||||||
email: emailEl.value
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
username.onkeyup = function() {
|
username.onkeyup = function() {
|
||||||
if (username.value.length > 2) socket.emit('user.exists', {username: username.value});
|
if (username.value.length > 2) socket.emit('user.exists', {username: username.value});
|
||||||
else {
|
else {
|
||||||
@@ -32,20 +26,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
emailEl.addEventListener('change', function() {
|
emailEl.addEventListener('change', function() {
|
||||||
console.log('checking email existance');
|
|
||||||
socket.emit('user.email.exists', { email: emailEl.value });
|
socket.emit('user.email.exists', { email: emailEl.value });
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
|
ajaxify.register_events(['user.exists', 'user.email.exists']);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ajaxify.register_events(['user.create', 'user.exists', 'user.email.exists']);
|
|
||||||
|
|
||||||
socket.on('user.create', function(data) {
|
|
||||||
//console.log('user create: ' + data.status);
|
|
||||||
ajaxify.go('/');
|
|
||||||
|
|
||||||
});
|
|
||||||
socket.on('user.exists', function(data) {
|
socket.on('user.exists', function(data) {
|
||||||
if (data.exists == true) {
|
if (data.exists == true) {
|
||||||
username_notify.innerHTML = 'Username exists';
|
username_notify.innerHTML = 'Username exists';
|
||||||
|
|||||||
24
src/user.js
24
src/user.js
@@ -133,15 +133,10 @@ var config = require('../config.js'),
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
User.create = function(username, password, email) {
|
User.create = function(username, password, email, callback) {
|
||||||
if (username == null || password == null) {
|
|
||||||
return; socket.emit('user.create', {'status': 0, 'message': 'Missing fields'});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
User.exists(username, function(exists) {
|
User.exists(username, function(exists) {
|
||||||
if (exists) {
|
if (exists) {
|
||||||
return;
|
return callback('user-exists', 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
RDB.incr('global:next_user_id', function(uid) {
|
RDB.incr('global:next_user_id', function(uid) {
|
||||||
@@ -158,14 +153,7 @@ var config = require('../config.js'),
|
|||||||
RDB.lpush('user:users', username);
|
RDB.lpush('user:users', username);
|
||||||
io.sockets.emit('user.latest', {username: username});
|
io.sockets.emit('user.latest', {username: username});
|
||||||
|
|
||||||
socket.emit('user.create', {'status': 1});
|
callback(null, uid);
|
||||||
|
|
||||||
socket.emit('event:alert', {
|
|
||||||
title: 'Thank you for registering',
|
|
||||||
message: 'You have successfully registered - welcome to nodebb!',
|
|
||||||
type: 'notify',
|
|
||||||
timeout: 2000
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -174,11 +162,9 @@ var config = require('../config.js'),
|
|||||||
User.exists = function(username, callback) {
|
User.exists = function(username, callback) {
|
||||||
User.get_uid_by_username(username, function(exists) {
|
User.get_uid_by_username(username, function(exists) {
|
||||||
exists = !!exists;
|
exists = !!exists;
|
||||||
socket.emit('user.exists', {exists: exists})
|
|
||||||
|
|
||||||
if (callback) {
|
if (callback) callback(exists);
|
||||||
callback(exists);
|
else socket.emit('user.exists', {exists: exists});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
User.count = function() {
|
User.count = function() {
|
||||||
|
|||||||
@@ -164,6 +164,20 @@ passport.deserializeUser(function(uid, done) {
|
|||||||
res.send(templates['header'] + templates['register'] + templates['footer']);
|
res.send(templates['header'] + templates['register'] + templates['footer']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post('/register', function(req, res) {
|
||||||
|
global.modules.user.create(req.body.username, req.body.password, req.body.email, function(err, uid) {
|
||||||
|
if (err === null) {
|
||||||
|
req.login({
|
||||||
|
uid: uid
|
||||||
|
}, function() {
|
||||||
|
res.redirect('/');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.redirect('/register');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
app.get('/account', function(req, res) {
|
app.get('/account', function(req, res) {
|
||||||
refreshTemplates();
|
refreshTemplates();
|
||||||
res.send(templates['header'] + templates['account_settings'] + templates['footer']);
|
res.send(templates['header'] + templates['account_settings'] + templates['footer']);
|
||||||
|
|||||||
@@ -53,10 +53,6 @@ var SocketIO = require('socket.io').listen(global.server),
|
|||||||
modules.user.get(uid, data.fields);
|
modules.user.get(uid, data.fields);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('user.create', function(data) {
|
|
||||||
modules.user.create(data.username, data.password, data.email);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('user.exists', function(data) {
|
socket.on('user.exists', function(data) {
|
||||||
modules.user.exists(data.username);
|
modules.user.exists(data.username);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user