-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updating instructions * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * database connection setup Signed-off-by: ridhishjain <[email protected]> * Update README.md * Update README.md * sql file Signed-off-by: ridhishjain <[email protected]> * test api success Signed-off-by: ridhishjain <[email protected]> * setting up eslint Signed-off-by: ridhishjain <[email protected]> * testing (#3) * testing * testing * signup api setup (#4) Signed-off-by: ridhishjain <[email protected]>
- Loading branch information
1 parent
c442ee4
commit 557980f
Showing
13 changed files
with
252 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,39 @@ | ||
{ | ||
"extends": ["eslint:recommended", "prettier"], | ||
"env": { | ||
"node": true, | ||
"browser": false, | ||
"commonjs": true, | ||
"es6": true | ||
}, | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"plugins": ["prettier"], | ||
"rules": { | ||
"semi": [2, "never"], | ||
"linebreak-style": ["error", "unix"], | ||
"prettier/prettier": ["error", { | ||
"extends": [ | ||
"eslint:recommended", | ||
"prettier" | ||
], | ||
"env": { | ||
"node": true, | ||
"browser": false, | ||
"commonjs": true, | ||
"es6": true | ||
}, | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"plugins": [ | ||
"prettier" | ||
], | ||
"rules": { | ||
"semi": [ | ||
2, | ||
"never" | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"prettier/prettier": [ | ||
"error", | ||
{ | ||
"singleQuote": true, | ||
"semi": false, | ||
"tabWidth": 4 | ||
} | ||
] | ||
} | ||
} | ||
"semi": false | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
const { pool } = require('../database') | ||
|
||
function getDetails() { | ||
return new Promise((resolve, reject) => { | ||
pool.query(`SELECT * FROM user`, (error, results) => { | ||
if (error) { | ||
return reject(error) | ||
} | ||
return resolve(results) | ||
}) | ||
return new Promise((resolve, reject) => { | ||
pool.query(`SELECT * FROM user`, (error, results) => { | ||
if (error) { | ||
return reject(error) | ||
} | ||
return resolve(results) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = getDetails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
const getDetails = require('./getDetails') | ||
const signup = require('./signup') | ||
|
||
module.exports = { getDetails } | ||
module.exports = { | ||
signup, | ||
getDetails | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-disable no-async-promise-executor */ | ||
const bcrypt = require('bcryptjs') | ||
const { pool } = require('../database') | ||
|
||
/** | ||
* | ||
* @param {*} param0 | ||
* @param {String} param0.username | ||
* @param {String} param0.password | ||
* @param {String} param0.full_name | ||
* @param {String} param0.admission_number | ||
* @param {String} param0.email | ||
* @param {String} param0.mobile | ||
* @return {Promise} | ||
* | ||
*/ | ||
|
||
function signup({ | ||
username, | ||
password, | ||
full_name: fullName, | ||
admission_number: admissionNumber, | ||
email, | ||
mobile | ||
}) { | ||
return new Promise(async (resolve, reject) => { | ||
bcrypt.genSalt(parseInt(process.env.SALT_ROUNDS), (error, salt) => { | ||
if (error) { | ||
return reject(error) | ||
} | ||
bcrypt.hash(password, salt, (error, hash) => { | ||
if (error) { | ||
return reject(error) | ||
} | ||
pool.query( | ||
`INSERT INTO user (username,secret,full_name,admission_number,email,mobile) VALUES(?,?,?,?,?,?)`, | ||
[username, hash, fullName, admissionNumber, email, mobile], | ||
error => { | ||
if (error) { | ||
return reject(error) | ||
} | ||
return resolve( | ||
'Account created. Please activate your account using the OTP sent to your email address.' | ||
) | ||
} | ||
) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = signup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
const getDetails = require('./getDetails') | ||
const getDetailsRouter = require('./getDetails') | ||
const signupRouter = require('./signup') | ||
|
||
module.exports = getDetails | ||
module.exports = { | ||
getDetailsRouter, | ||
signupRouter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const auth = require('../../models/auth') | ||
const ajv = require('../../schema') | ||
const { signupSchema } = require('../../schema/auth') | ||
|
||
/** | ||
* | ||
* @param {Array} errArray | ||
* @return {String} | ||
*/ | ||
function sumErrors(errArray) { | ||
const cb = (a, b) => a + b.message + ', ' | ||
return errArray.reduce(cb, '') | ||
} | ||
|
||
router.post('/signup', async (request, response) => { | ||
const validate = ajv.compile(signupSchema) | ||
const isValid = validate(request.body) | ||
if (!isValid) { | ||
return response.status(400).json({ | ||
success: false, | ||
error: sumErrors(validate.errors), | ||
results: null | ||
}) | ||
} | ||
auth | ||
.signup(request.body) | ||
.then(results => { | ||
return response.status(200).json({ | ||
success: true, | ||
error: null, | ||
results | ||
}) | ||
}) | ||
.catch(error => { | ||
return response.status(400).json({ | ||
success: false, | ||
error, | ||
results: null | ||
}) | ||
}) | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const ajv = require('../index') | ||
const signupSchema = require('./signup') | ||
|
||
ajv.addFormat('password', data => { | ||
return data.length >= 8 | ||
}) | ||
|
||
module.exports = { | ||
signupSchema, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const schema = { | ||
required: ['username', 'password', 'full_name', 'admission_number', 'email', 'mobile'], | ||
properties: { | ||
username: { type: 'string', minLength: 4 }, | ||
password: { type: 'string', format: 'password' }, | ||
full_name: { type: 'string' }, | ||
admission_number: { | ||
type: 'string', | ||
minLength: 8, | ||
maxLength: 8, | ||
pattern: '^[0-9]{2}[a-z]{2}[0-9]{4}$' | ||
}, | ||
email: { | ||
type: 'string', | ||
pattern: | ||
'^[a-z]+\\.[0-9]{2}[a-z]{2}[0-9]{4}@([a-z].\\.)?iitism\\.ac\\.in$' | ||
}, | ||
mobile: { | ||
type: 'string', | ||
minLength: 10, | ||
maxlength: 10, | ||
pattern: '^[0-9]{10}$' | ||
} | ||
}, | ||
errorMessage: { | ||
required: { | ||
username: 'Username requried', | ||
password: 'Password required', | ||
full_name: 'Name required', | ||
admission_number: 'Admission Number required', | ||
email: 'Email required', | ||
mobile: 'Mobile Number required', | ||
}, | ||
properties: { | ||
username: 'Invalid username', | ||
password: 'Invalid password', | ||
full_name: 'Invalid name', | ||
admission_number: 'Invalid admission number', | ||
email: 'Invlalid email', | ||
mobile: 'Invalid mobile number' | ||
}, | ||
_: 'Invalid data' | ||
} | ||
}; | ||
|
||
module.exports = schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const Ajv = require('ajv') | ||
|
||
const ajv = new Ajv({ | ||
allErrors: true, | ||
coerceTypes: true, | ||
useDefaults: true, | ||
jsonPointers: true | ||
}); | ||
|
||
require('ajv-errors')(ajv) | ||
|
||
module.exports = ajv |