-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
990 additions
and
1,652 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,3 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-env"], | ||
"plugins": ["@babel/transform-runtime"] | ||
} |
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,43 @@ | ||
version: "2" # required to adjust maintainability checks | ||
|
||
plugins: | ||
duplication: | ||
enabled: true | ||
eslint: | ||
enabled: true | ||
checks: | ||
argument-count: | ||
config: | ||
threshold: 4 | ||
complex-logic: | ||
config: | ||
threshold: 4 | ||
file-lines: | ||
config: | ||
threshold: 250 | ||
method-complexity: | ||
config: | ||
threshold: 5 | ||
method-count: | ||
config: | ||
threshold: 20 | ||
method-lines: | ||
config: | ||
threshold: 25 | ||
nested-control-flow: | ||
config: | ||
threshold: 4 | ||
return-statements: | ||
config: | ||
threshold: 4 | ||
similar-code: | ||
config: | ||
threshold: # language-specific defaults. an override will affect all languages. | ||
identical-code: | ||
config: | ||
threshold: # language-specific defaults. an override will affect all languages. | ||
exclude_patterns: | ||
- node_modules/ | ||
- test/ | ||
- public/ | ||
- dist/ |
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
This file was deleted.
Oops, something went wrong.
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,46 +1,95 @@ | ||
import users from '../utils/sample.users'; | ||
import bcrypt from 'bcrypt'; | ||
|
||
import Model from '../models/Model'; | ||
import { InternalServerError, CriticalError } from '../utils/errorHandlers'; | ||
import { dev_logger } from '../utils/loggers'; | ||
|
||
// import { dev_logger, test_logger } from '../utils/loggers'; | ||
|
||
const users_model = new Model('users'); | ||
|
||
const AuthController = { | ||
signup: (req, res) => { | ||
|
||
if (req.method === 'GET') { | ||
res.render('authentication', { title: 'Sign Up' }); | ||
} | ||
else { | ||
const { email, password } = req.body; | ||
|
||
res.status(200).json({ | ||
status: 201, | ||
data: { | ||
id: 2, | ||
email, | ||
password, | ||
first_name: '', | ||
last_name: '', | ||
address: { | ||
home: '', | ||
office: '', | ||
}, | ||
status: 'unverified', | ||
isAdmin: false, | ||
token: req.token, | ||
const { email, password } = req.body; | ||
const hashedPassword = bcrypt.hashSync(password, 8); | ||
dev_logger(`token: ${req.token}`); | ||
|
||
// check if user exists | ||
const check_user_existence = async (email) => { | ||
try { | ||
const { rows } = await users_model.select( | ||
'id, email', `email='${email}'`); | ||
const [ user, ] = rows; | ||
if (user) return user; | ||
} | ||
catch (e) { return InternalServerError(req, res, e);} | ||
}; | ||
|
||
// add user to db | ||
const create_user = async (email, password) => { | ||
try { | ||
await users_model.insert('(email, password)', | ||
`'${email}', '${password}'`); | ||
} | ||
catch (e) { return InternalServerError(req, res, e);} | ||
}; | ||
|
||
const get_new_user = async (email) => { | ||
try { | ||
const { rows } = await users_model.select( | ||
`id, email, password, firstname, | ||
lastname, phone, status, address`, | ||
`email='${email}'`); | ||
|
||
if (rows.length === 0) { | ||
// user was not found. | ||
// This is highly unlikely but should be handled | ||
return CriticalError( | ||
res, 'A supposedly created user is missing'); | ||
} | ||
}); | ||
} | ||
return res | ||
.status(201) | ||
.json({ data: { ...rows[0], token: req.token } }); | ||
} | ||
catch (e) { return InternalServerError(req, res, e);} | ||
}; | ||
|
||
(async () => { | ||
const user = await check_user_existence(email); | ||
dev_logger(`users******** ${user}`); | ||
if (user) { | ||
return res | ||
.status(404) | ||
.json({ error: `User with email ${email} already exists` }); | ||
} | ||
await create_user(email, hashedPassword); | ||
return get_new_user(email); | ||
})(); | ||
}, | ||
|
||
signin: (req, res) => { | ||
if (req.method === 'GET') { | ||
res.render( 'authentication', { title: 'Sign In' }); | ||
} | ||
else { | ||
const { email } = req.body; | ||
const user = users.find(user => user.email === email); | ||
const data = { ... user, token: req.token }; | ||
res.status(200).json({ data }); | ||
} | ||
const { email } = req.body; | ||
(async () => { | ||
try { | ||
const { rows } = await users_model.select( | ||
`id, email, password, firstname, | ||
lastname, phone, status, address`, | ||
`email='${email}'` | ||
); | ||
const [ user, ] = rows; | ||
if (!user) { | ||
return res | ||
.status(404) | ||
.json({ error: `User with email ${email} not found` }); | ||
} | ||
// token verification takes care of identifying the user | ||
return res | ||
.status(200) | ||
.json({ data: { ...rows[0], token: req.token } }); | ||
} | ||
catch (e) { return InternalServerError(req, res, e); } | ||
})(); | ||
}, | ||
}; | ||
|
||
export default AuthController; | ||
|
Oops, something went wrong.