Skip to content

Commit

Permalink
Added branch and department to user table, API updated (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: ridhisjain <[email protected]>
  • Loading branch information
ridhishjain authored Oct 16, 2020
1 parent 306b128 commit 9f7c544
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
5 changes: 4 additions & 1 deletion server/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ CREATE TABLE `user` (
`otp` varchar(45) DEFAULT NULL,
`otp_valid_upto` varchar(45) DEFAULT NULL,
`verified` tinyint DEFAULT NULL,
`department` int NOT NULL,
`branch` int NOT NULL,
`bio` varchar(45) DEFAULT NULL,
PRIMARY KEY (`username`),
UNIQUE KEY `username_UNIQUE` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Expand All @@ -50,4 +53,4 @@ CREATE TABLE `user` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-09-28 16:32:52
-- Dump completed on 2020-10-16 6:01:34
6 changes: 5 additions & 1 deletion server/models/auth/getUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ const { pool } = require('../database')
function getUser(username) {
return new Promise((resolve, reject) => {
pool.query(
`SELECT username,full_name,email,admission_number,mobile FROM user WHERE username=?`,
`SELECT username,full_name,email,admission_number,mobile,department,branch,bio,logged_in FROM user WHERE username=?`,
[username],
(error, results) => {
if (error || !results.length) {
return reject('User not found')
}
const isLoggedIn = results[0].logged_in
if (!isLoggedIn) {
return reject('User not logged in, please login first!')
}
return resolve(results[0])
}
)
Expand Down
10 changes: 10 additions & 0 deletions server/models/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-async-promise-executor */
const fs = require('fs')
const jwt = require('jsonwebtoken')
const { pool } = require('../database')
const isCorrect = require('./isCorrect')

/**
Expand Down Expand Up @@ -33,6 +34,15 @@ function login({ username, password }) {
if (error) {
return reject(`error = ${error}`)
}
pool.query(
`UPDATE user SET logged_in=? WHERE username=?`,
[1, username],
(error) => {
if (error) {
return reject(error)
}
}
)
return resolve({ username, access_token: accessToken })
}
)
Expand Down
20 changes: 17 additions & 3 deletions server/models/auth/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const otplib = require('otplib')
* @param {String} param0.admission_number
* @param {String} param0.email
* @param {String} param0.mobile
* @param {Number} param0.department
* @param {Number} param0.branch
* @return {Promise}
*
*/
Expand All @@ -24,6 +26,8 @@ function signup({
admission_number: admissionNumber,
email,
mobile,
department,
branch,
}) {
return new Promise(async (resolve, reject) => {
bcrypt.genSalt(parseInt(process.env.SALT_ROUNDS), (error, salt) => {
Expand All @@ -39,9 +43,19 @@ function signup({
const otp = otplib.authenticator.generate(secretOtp)

pool.query(
`INSERT INTO user (username,secret,full_name,admission_number,email,mobile,otp,otp_valid_upto)
VALUES(?,?,?,?,?,?,?,NOW()+INTERVAL 1 DAY)`,
[username, hash, fullName, admissionNumber, email, mobile, otp],
`INSERT INTO user (username,secret,full_name,admission_number,email,mobile,department,branch,otp,otp_valid_upto)
VALUES(?,?,?,?,?,?,?,?,?,NOW()+INTERVAL 1 DAY)`,
[
username,
hash,
fullName,
admissionNumber,
email,
mobile,
department,
branch,
otp,
],
(error) => {
if (error) {
return reject(error)
Expand Down
20 changes: 19 additions & 1 deletion server/models/auth/updateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const otplib = require('otplib')
* @param {String} param0.email
* @param {String} param0.full_name
* @param {String} param0.admission_number
* @param {Number} param0.mobile
* @param {String} param0.mobile
* @param {Number} param0.department
* @param {Number} param0.branch
* @param {String} param0.bio
* @return {Promise}
*
*/
Expand All @@ -20,6 +23,9 @@ function updateUser({
full_name: fullName,
admission_number: admissionNumber,
mobile,
department,
branch,
bio,
}) {
return new Promise((resolve, reject) => {
const secret = otplib.authenticator.generateSecret()
Expand All @@ -39,6 +45,18 @@ function updateUser({
query += `mobile=?,`
arr.push(mobile)
}
if (Number.isInteger(department)) {
query += `department=?,`
arr.push(department)
}
if (Number.isInteger(branch)) {
query += `branch=?,`
arr.push(branch)
}
if (bio) {
query += `bio=?,`
arr.push(bio)
}
if (email) {
query += `email=?,verified=?,`
arr.push(email)
Expand Down
16 changes: 16 additions & 0 deletions server/schema/auth/signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const schema = {
'admission_number',
'email',
'mobile',
'department',
'branch',
],
properties: {
username: { type: 'string', minLength: 4 },
Expand All @@ -27,6 +29,16 @@ const schema = {
maxlength: 10,
pattern: '^[0-9]{10}$',
},
department: {
type: 'number',
minimum: 0,
maximum: 14,
},
branch: {
type: 'number',
minimum: 0,
maximum: 3,
},
},
errorMessage: {
required: {
Expand All @@ -36,6 +48,8 @@ const schema = {
admission_number: 'Admission Number required',
email: 'Email required',
mobile: 'Mobile Number required',
department: 'Department required',
branch: 'Branch required',
},
properties: {
username: 'Invalid username',
Expand All @@ -44,6 +58,8 @@ const schema = {
admission_number: 'Invalid admission number',
email: 'Invlalid email',
mobile: 'Invalid mobile number',
department: 'Invalid department',
branch: 'Invalid branch',
},
_: 'Invalid data',
},
Expand Down
17 changes: 17 additions & 0 deletions server/schema/auth/updateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ const schema = {
maxLength: 10,
pattern: '^[0-9]{10}$',
},
department: {
type: 'number',
minimum: 0,
maximum: 14,
},
branch: {
type: 'number',
minimum: 0,
maximum: 3,
},
bio: {
type: 'string',
maxLength: 100,
},
},
errorMessage: {
required: {
Expand All @@ -30,6 +44,9 @@ const schema = {
full_name: 'Invalid name',
admission_number: 'Invalid Admission number',
mobile: 'Invalid mobile number',
department: 'Invalid department',
branch: 'Invalid branch',
bio: 'Invalid bio',
},
_: 'Invalid data',
},
Expand Down

0 comments on commit 9f7c544

Please sign in to comment.