Signup endpoints almost done

This commit is contained in:
Ben
2018-08-31 17:08:04 +01:00
parent b330329441
commit c33f7bdd7b
12 changed files with 163 additions and 22 deletions

10
src/models/api/API.js Normal file
View File

@@ -0,0 +1,10 @@
import {BaseAPI} from './baseAPI';
export class API extends BaseAPI {
constructor() {
super();
}
}
API.errors = require('./APIErrors').APIErrors;
API.user = require('./userResponses').User;

View File

@@ -0,0 +1,39 @@
import {API} from './API';
export class APIErrors extends API {
// get errors() {return this.errors}
// set errors(err) {this.errors = err}
constructor(res) {
super()
this.res = res;
this.errors = {
status: {
error: true,
code: undefined,
type: undefined,
message: undefined
},
error: {
errors: []
}
}
}
addError(statusCode, message, verbose) {
this.errors.error.errors.push({status: statusCode, title: message, detail: verbose});
this.errors.status.code = statusCode;
this.errors.status.type = message;
this.errors.status.message = verbose;
}
count() {
return this.errors.error.errors.length;
}
endpoint() {
this.res.setHeader('Content-type', 'application/json');
this.res.status(this.errors.status.code);
this.res.end(JSON.stringify(this.errors, false, 4));
}
}

View File

@@ -0,0 +1,4 @@
export class BaseAPI {
constructor() { }
}

View File

@@ -0,0 +1,34 @@
import {API} from './API';
export class User extends API {
constructor(res, id, username, email, updated, token) {
super();
this.res = res;
this.response = {
status: {
error: false,
code: 200,
type: 'success',
message: 'Success'
},
data: [
{
status: 'Authenticated',
user: {
id: id,
username: username,
email: email,
updated: updated
},
token: token
}
]
}
}
endpoint() {
this.res.setHeader('Content-type', 'application/json');
this.res.status(200);
this.res.end(JSON.stringify(this.response, false, 4));
}
}

View File

@@ -2,7 +2,6 @@ import Sequelize from 'sequelize';
import {BaseDatabase} from './baseDatabase';
import {Logger} from '../logger';
import {Config} from '../../config/config';
export class UserTools extends BaseDatabase {
static async listAll() {
@@ -36,7 +35,7 @@ export class UserTools extends BaseDatabase {
let User = BaseDatabase.User;
try {
let user = await User.destroy({where: {id: id}});
await User.destroy({where: {id: id}});
return 1;
} catch (e) {
Logger.error(`An error occured while deleting user id ${id}: ${e}`);
@@ -64,26 +63,26 @@ export class UserTools extends BaseDatabase {
if (column == 'id') {
return search;
} else if (column == 'username') {
let user = await User.findOne({where: {username: serch}});
let user = await User.findOne({where: {username: search}});
if (user == null) return -1;
return user;
} else if (column == 'password') {
let user = await User.findOne({where: {password: serch}});
let user = await User.findOne({where: {password: search}});
if (user == null) return -1;
return user;
} else if (column == 'ip') {
let user = await User.findOne({where: {ip: serch}});
let user = await User.findOne({where: {ip: search}});
if (user == null) return -1;
return user;
} else if (column == 'authcode') {
let user = await User.findOne({where: {authcode: serch}});
let user = await User.findOne({where: {authcode: search}});
if (user == null) return -1;
return user;
} else {
return -1
}
} catch (e) {
Logger.error(`An error occured while querying the id of a user where ${term} is ${searchTerm}: ${e}`);
Logger.error(`An error occured while querying the id of a user where ${column} is ${search}: ${e}`);
return -1;
}
}
@@ -92,7 +91,7 @@ export class UserTools extends BaseDatabase {
let User = BaseDatabase.User;
try {
let user = await User.update({ip: newIP}, {where: {id: id}});
await User.update({ip: newIP}, {where: {id: id}});
return 1;
} catch (e) {
Logger.error(`An error occured while updating user id ${id}'s ip: ${e}`);

View File

@@ -32,6 +32,12 @@ export class Logger {
+ colours.cyan('DEBUG') + '] ' + message);
}
static ready() {
let d = new Date();
console.log('[' + d.toLocaleString() + '] ['
+ colours.rainbow('READY') + ']');
}
static info(message) {
if (LogLevel > 2) return;
let d = new Date();

View File

@@ -1,15 +1,12 @@
import {Logger} from '../logger';
export class BaseUser {
constructor(id, username, password, email, ip, lastupdated, verified, authcode, timeauthed) {
constructor(id, username, password, email, ip, authcode) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.ip = ip;
this.lastupdated = lastupdated;
this.verified = verified;
this.authcode = authcode;
this.timeauthed = timeauthed;
}
}

View File

@@ -3,13 +3,13 @@ import {BaseUser} from './baseUser';
import {Database} from '../database/database';
export class User extends BaseUser {
constructor(id, username, password, email, ip, lastupdated, verified, authcode, timeauthed) {
super(id, username, password, email, ip, lastupdated, verified, authcode, timeauthed);
constructor(id, username, password, email, ip, authcode) {
super(id, username, password, email, ip, authcode);
}
async insert() {
this._instance = await Database.users.newUser(this.id, this.username, this.password, this.email, this.ip, this.authcode)
if (this._instance == -1) throw new Error('Failed to insert');
if (this._instance == -1) return -1;
Logger.debug(`New user [${this.id}] ${this.username}`);
}