Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
yotta
/
pictogram
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
60
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
0a4a021e
authored
Dec 30, 2016
by
Arturo Montejo Ráez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
issue #724 closed
parent
df6f0dd0
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
69 additions
and
30 deletions
sails/src/api/controllers/StudentController.js
sails/src/api/responses/badRequest.js
sails/src/api/responses/notFound.js
sails/src/api/responses/serverError.js
sails/src/api/responses/unauthorized.js
sails/src/api/controllers/StudentController.js
View file @
0a4a021e
...
@@ -43,12 +43,18 @@ module.exports = {
...
@@ -43,12 +43,18 @@ module.exports = {
* token: '... asd90jkas ...',
* token: '... asd90jkas ...',
* server_time: 123912932312
* server_time: 123912932312
* }
* }
* @param {response} Errors:
* - 400 (Bad request) with error message "Missing parameters"
* - 401 (Unauthorized) with error message "Student has an invalid license"
* - 401 (Unauthorized) with error message "Invalid username/password"
* - 404 (Not found) with error message "Student not found"
* - 500 (Server error) with error message "Error when connecting to database"
*/
*/
login
:
function
(
req
,
res
)
{
login
:
function
(
req
,
res
)
{
var
bcrypt
=
require
(
'bcrypt-nodejs'
);
var
bcrypt
=
require
(
'bcrypt-nodejs'
);
Student
.
findOne
({
if
(
!
req
.
body
.
username
||
!
req
.
body
.
password
)
username
:
req
.
body
.
username
return
res
.
badRequest
(
"Missing parameters"
);
})
Student
.
findOne
({
username
:
req
.
body
.
username
})
.
populate
(
'license'
)
.
populate
(
'license'
)
.
then
(
function
(
student
)
{
.
then
(
function
(
student
)
{
if
(
student
)
{
if
(
student
)
{
...
@@ -56,7 +62,7 @@ module.exports = {
...
@@ -56,7 +62,7 @@ module.exports = {
student
.
isStudent
=
true
;
student
.
isStudent
=
true
;
if
(
!
student
.
license
||
!
student
.
license
[
0
]
||
student
.
license
[
0
].
hasExpired
())
{
if
(
!
student
.
license
||
!
student
.
license
[
0
]
||
student
.
license
[
0
].
hasExpired
())
{
sails
.
log
.
error
(
`Tried to login with non valid license
${
req
.
body
.
username
}
`
);
sails
.
log
.
error
(
`Tried to login with non valid license
${
req
.
body
.
username
}
`
);
return
res
.
badRequest
(
"Student has an invalid license"
);
return
res
.
unauthorized
(
"Student has an invalid license"
);
}
else
}
else
student
=
student
.
toObject
();
// to enable overwrite license field
student
=
student
.
toObject
();
// to enable overwrite license field
student
.
license
=
student
.
license
[
0
];
student
.
license
=
student
.
license
[
0
];
...
@@ -68,16 +74,16 @@ module.exports = {
...
@@ -68,16 +74,16 @@ module.exports = {
}
else
{
}
else
{
sails
.
log
.
error
(
`Invalid student login: user
${
student
.
username
}
, password\
sails
.
log
.
error
(
`Invalid student login: user
${
student
.
username
}
, password\
"
${
req
.
body
.
password
}
"`
);
"
${
req
.
body
.
password
}
"`
);
res
.
badRequest
(
);
res
.
unauthorized
(
"Invalid username/password"
);
}
}
}
else
{
}
else
{
sails
.
log
.
error
(
`Tried to login as non-existing student
${
req
.
body
.
username
}
`
);
sails
.
log
.
error
(
`Tried to login as non-existing student
${
req
.
body
.
username
}
`
);
res
.
badRequest
(
);
res
.
notFound
(
"Student not found"
);
}
}
})
})
.
catch
(
function
()
{
.
catch
(
function
()
{
sails
.
log
.
error
(
`Error getting student
${
req
.
body
.
username
}
for login`
);
sails
.
log
.
error
(
`Error getting student
${
req
.
body
.
username
}
for login`
);
res
.
serverError
(
);
res
.
serverError
(
"Error when connecting to database"
);
});
});
},
},
...
...
sails/src/api/responses/badRequest.js
View file @
0a4a021e
...
@@ -31,13 +31,6 @@ module.exports = function badRequest(data, options) {
...
@@ -31,13 +31,6 @@ module.exports = function badRequest(data, options) {
}
}
else
sails
.
log
.
verbose
(
'Sending 400 ("Bad Request") response'
);
else
sails
.
log
.
verbose
(
'Sending 400 ("Bad Request") response'
);
// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if
(
sails
.
config
.
environment
===
'production'
)
{
data
=
undefined
;
}
// If the user-agent wants JSON, always respond with JSON
// If the user-agent wants JSON, always respond with JSON
if
(
req
.
wantsJSON
)
{
if
(
req
.
wantsJSON
)
{
return
res
.
jsonx
(
data
);
return
res
.
jsonx
(
data
);
...
@@ -61,4 +54,3 @@ module.exports = function badRequest(data, options) {
...
@@ -61,4 +54,3 @@ module.exports = function badRequest(data, options) {
});
});
};
};
sails/src/api/responses/notFound.js
View file @
0a4a021e
...
@@ -33,13 +33,6 @@ module.exports = function notFound (data, options) {
...
@@ -33,13 +33,6 @@ module.exports = function notFound (data, options) {
}
}
else
sails
.
log
.
verbose
(
'Sending 404 ("Not Found") response'
);
else
sails
.
log
.
verbose
(
'Sending 404 ("Not Found") response'
);
// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if
(
sails
.
config
.
environment
===
'production'
)
{
data
=
undefined
;
}
// If the user-agent wants JSON, always respond with JSON
// If the user-agent wants JSON, always respond with JSON
if
(
req
.
wantsJSON
)
{
if
(
req
.
wantsJSON
)
{
return
res
.
jsonx
(
data
);
return
res
.
jsonx
(
data
);
...
@@ -79,4 +72,3 @@ module.exports = function notFound (data, options) {
...
@@ -79,4 +72,3 @@ module.exports = function notFound (data, options) {
});
});
};
};
sails/src/api/responses/serverError.js
View file @
0a4a021e
...
@@ -28,13 +28,6 @@ module.exports = function serverError (data, options) {
...
@@ -28,13 +28,6 @@ module.exports = function serverError (data, options) {
}
}
else
sails
.
log
.
error
(
'Sending empty 500 ("Server Error") response'
);
else
sails
.
log
.
error
(
'Sending empty 500 ("Server Error") response'
);
// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if
(
sails
.
config
.
environment
===
'production'
)
{
data
=
undefined
;
}
// If the user-agent wants JSON, always respond with JSON
// If the user-agent wants JSON, always respond with JSON
if
(
req
.
wantsJSON
)
{
if
(
req
.
wantsJSON
)
{
return
res
.
jsonx
(
data
);
return
res
.
jsonx
(
data
);
...
...
sails/src/api/responses/unauthorized.js
0 → 100644
View file @
0a4a021e
/**
* 401 (Unauthorized) Handler
*
* Usage:
* return res.unauthorized();
* return res.unauthorized(data);
* return res.unauthorized(data, 'some/specific/badRequest/view');
*
* e.g.:
* ```
* return res.unauthorized(
* 'Invalid username',
* 'trial/signup'
* );
* ```
*/
module
.
exports
=
function
unauthorized
(
data
,
options
)
{
// Get access to `req`, `res`, & `sails`
var
req
=
this
.
req
;
var
res
=
this
.
res
;
var
sails
=
req
.
_sails
;
// Set status code
res
.
status
(
401
);
// Log error to console
if
(
data
!==
undefined
)
{
sails
.
log
.
verbose
(
'Sending 401 ("Unauthorized") response: \n'
,
data
);
}
else
sails
.
log
.
verbose
(
'Sending 401 ("Unauthorized") response'
);
// If the user-agent wants JSON, always respond with JSON
if
(
req
.
wantsJSON
)
{
return
res
.
jsonx
(
data
);
}
// If second argument is a string, we take that to mean it refers to a view.
// If it was omitted, use an empty object (`{}`)
options
=
(
typeof
options
===
'string'
)
?
{
view
:
options
}
:
options
||
{};
// If a view was provided in options, serve it.
// Otherwise try to guess an appropriate view, or if that doesn't
// work, just send JSON.
if
(
options
.
view
)
{
return
res
.
view
(
options
.
view
,
{
data
:
data
});
}
// If no second argument provided, try to serve the implied view,
// but fall back to sending JSON(P) if no view can be inferred.
else
return
res
.
guessView
({
data
:
data
},
function
couldNotGuessView
()
{
return
res
.
jsonx
(
data
);
});
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment