Browse Source

Migration to the new database mostly works. I might have botched the git though.

pull/18/head
Zed A. Shaw 2 weeks ago
parent
commit
71d8766c8e
10 changed files with 66 additions and 45 deletions
  1. +9
    -18
      lib/data.js
  2. +26
    -2
      lib/models.js
  3. +4
    -4
      migrations/20200318222642_user_migration.js
  4. +2
    -1
      migrations/20200318224957_auths_table.js
  5. +5
    -3
      src/node_modules/data.js
  6. +1
    -1
      src/node_modules/security.js
  7. +1
    -1
      src/node_modules/utils.js
  8. +1
    -1
      src/routes/auth/verify.js
  9. +2
    -2
      src/routes/register/index.json.js
  10. +15
    -12
      src/routes/register/index.svelte

+ 9
- 18
lib/data.js View File

@@ -7,7 +7,7 @@ exports.find_token_with_email = async (email) => {
assert(email, `Valid email required: ${email}`);
try {
let res = await new Auth('email', email).fetch();
let res = await new Auth('email', email).fetch({require: false});
return res === null ? undefined : res.get('token');
} catch (error) {
log.error(error,"find_token_with_email");
@@ -16,25 +16,16 @@ exports.find_token_with_email = async (email) => {
}
exports.create_user = async (email, active) => {
exports.create_auth_token = async (email, ip_addr, registration) => {
assert(email, `Valid email required: ${email}`);
try {
let result = await new User({email, active}).save();
log.debug("Create user result", result);
return result;
} catch (error) {
log.error(error, "create_user");
return undefined;
}
}
exports.create_auth_token = async (email, ip_addr, registered) => {
assert(email, `Valid email required: ${email}`);
log.debug(`Adding auth token for ${email} with registered ${registered}`);
log.debug(`Adding auth token for ${email} with registration ${registration}`);
let token = uuid4();
try {
let res = await new Auth({ email, ip_addr, registered, token }).save();
let res = await new Auth({ email, ip_addr, registration, token }).save();
assert(res.get('token') === token, `Token ${token} just from the database does not match ${res.get('token')}`);
} catch (error) {
log.error(error, "create_auth_token");
}
@@ -59,11 +50,11 @@ exports.is_valid_user = async (email) => {
let user = await new User({email}).fetch({require: false});
// is the 2nd check necessary?
let is_valid = user && user.email === email;
let is_valid = user && user.get("email") === email;
log.debug("User validity check for", email, "returned", user, "with is_valid", is_valid);
return {user, is_valid};
return {user: user.attributes, is_valid};
} catch(error) {
log.error(error, "is_valid_user");
return {user: undefined, is_valid: false};

+ 26
- 2
lib/models.js View File

@@ -1,3 +1,6 @@
const assert = require('assert');
let knex = require('knex')({
client: 'sqlite3',
connection: {
@@ -9,13 +12,34 @@ let knex = require('knex')({
useNullAsDefault: true
});
function fix_booleans(me, attr) {
for(let a of attr) {
// TODO: make sure this is the same with postgres or knex to figure it out
me.on('fetched', (model, response, options) => {
assert(a in model.attributes, `No attribute ${a} in ${model}`);
model.attributes[a] = model.attributes[a] == '1';
});
me.on('saved', (model, response, options) => {
assert(a in model.attributes, `No attribute ${a} in ${model}`);
model.attributes[a] = model.attributes[a] == '1';
});
}
}
const bookshelf = require('bookshelf')(knex);
exports.User = bookshelf.model('User', {
tableName: 'users'
tableName: 'users',
initialize() {
fix_booleans(this, ['send_emails', 'tos_agree', 'verified', 'registered']);
}
});
exports.Auth = bookshelf.model('Auth', {
tableName: 'auths'
tableName: 'auths',
initialize() {
fix_booleans(this, ['registration']);
}
});

+ 4
- 4
migrations/20200318222642_user_migration.js View File

@@ -4,10 +4,10 @@ exports.up = function(knex) {
table.increments();
table.string('full_name').notNullable();
table.string('email').notNullable();
table.boolean('send_emails').defaultTo(false);
table.boolean('tos_agree').defaultTo(false);
table.boolean('verified').defaultTo(false);
table.boolean('registered').defaultTo(false);
table.boolean('send_emails').notNullable().defaultTo(false);
table.boolean('tos_agree').notNullable().defaultTo(false);
table.boolean('verified').notNullable().defaultTo(false);
table.boolean('registered').notNullable().defaultTo(false);
});
};


+ 2
- 1
migrations/20200318224957_auths_table.js View File

@@ -6,7 +6,8 @@ exports.up = function(knex) {
table.string('email').notNullable();
table.string('ip_addr').notNullable();
table.string('token').notNullable();
table.boolean('registered').defaultTo(false);
// TODO: rename this to registration_required something
table.boolean('registration').notNullable().defaultTo(true);
});
};


+ 5
- 3
src/node_modules/data.js View File

@@ -7,8 +7,8 @@ const { User, Auth } = require('../../../lib/models');
export async function get_auth_token(token) {
try {
let res = await new Auth({token}).fetch({require: false});
log.info(res, "Auth token result");
return res ? res.attributes : res;
return res !== null ? res.attributes : undefined;
} catch (error) {
log.error(error, 'get_auth_token');
}
@@ -23,6 +23,8 @@ export async function register_user(user, full_name, send_emails, tos_agree)
{
// at this point they have just done an auth but are not registered
// email should be validated already before getting here, so we just need to add them
log.debug("Registering user", {user, full_name, send_emails, tos_agree});

try {
let new_user = await new User({
email: user.email,
@@ -34,7 +36,7 @@ export async function register_user(user, full_name, send_emails, tos_agree)
subscribe_user(new_user.get('email'));
}

return new_user.attributes;
return new_user;
} catch (error) {
log.error(error);
throw error;

+ 1
- 1
src/node_modules/security.js View File

@@ -1,4 +1,4 @@

export function is_authenticated(req) {
return req.session.user && req.session.user.verified === true;
return req.session.user && req.session.user.verified;
}

+ 1
- 1
src/node_modules/utils.js View File

@@ -12,5 +12,5 @@ export function post(endpoint, data) {
}

export function valid_user(user) {
return user && user.verified === true;
return user && user.verified;
}

+ 1
- 1
src/routes/auth/verify.js View File

@@ -31,7 +31,7 @@ export async function post(req, res) {
&& expected_auth.email === user.email
&& expected_auth.ip_addr === ip_addr) {
log.debug("User authenticated with code", user.token);
user.registered = expected_auth.registration === false;
user.registered = !expected_auth.registration;
delete_authmail(user.email);

if(user.registered) {

+ 2
- 2
src/routes/register/index.json.js View File

@@ -14,9 +14,9 @@ export async function post(req, res) {
try {
const updated_user = await register_user(user, full_name, send_emails, tos_agree);
res.end(JSON.stringify({user: updated_user}));
res.end(JSON.stringify({user: updated_user.attributes}));
} catch(error) {
log.error(error, "resgister post");
log.error(error, "register post");
res.statusCode = 403;
res.end(JSON.stringify({message: 'Database error saving your registration. Please try again later.'}));
}

+ 15
- 12
src/routes/register/index.svelte View File

@@ -5,8 +5,6 @@ export async function preload(page, session) {
if (!user) {
return this.redirect(302, 'auth');
}

return { user };
}
</script>

@@ -21,21 +19,26 @@ let full_name = "";
let send_emails=false;
let tos_agree=false;
let error = '';
export let segment;

const user_email = $session.user.email;


$: form_valid = (tos_agree === true && full_name);
$: form_valid = (tos_agree && full_name);

const register = async (event) => {
let user = $session.user;
let res = await post('/register.json', { user, full_name, send_emails, tos_agree });
if(res.user) {
console.log('New user is', res.user);
$session.user = res.user;
goto('live');
} else {
error = res.message;
}
console.log("Registration called");
let user = $session.user;
let res = await post('/register.json', { user, full_name, send_emails, tos_agree });
console.log("Registration returned", res);

if(res.user) {
console.log('New user is', res.user);
$session.user = res.user;
goto('live');
} else {
error = res.message;
}
}

</script>

Loading…
Cancel
Save