Skip to content

Commit

Permalink
Risolto problema errore login ed errore nel caricamento dei file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard1984 committed Nov 10, 2018
1 parent d9e708a commit 4245b50
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "archivio",
"version": "0.0.4",
"version": "0.0.5",
"description": "Archivio Digitale - ITIS Enrico Fermi",
"main": "server/server.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const login = new LocalStrategy({
},
async (username, password, done) => {
try {
let user = await User.findByCredentials(username, password)
const user = await User.findByCredentials(username, password)
if (!user) {
return done(null, false)
}
done(null, user)
} catch (e) {
done(e)
Expand Down
19 changes: 18 additions & 1 deletion server/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,24 @@ const signin = (req, res, next) => passport.authenticate('login', {
failureFlash: true
}, (err, user, info) => {
if (err) {
next(new Error('Si è verificato un errore durante il login.'))
const { name } = err
switch (name) {
case 'bad_email':
err.code = 404
next(err)
break
case 'account_disabled':
err.code = 401
next(err)
break
case 'bad_password':
err.code = 401
next(err)
break
default:
next(new Error('Si è verificato un errore durante il login.'))
break
}
} else if (!user) {
return res.status(401).json({
message: info
Expand Down
2 changes: 1 addition & 1 deletion server/middleware/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const error = (err, req, res, next) => {
default:
message = err.message
}
res.status(500).send({
res.status(err.code || 500).send({
messages: [message]
})
next(err)
Expand Down
12 changes: 9 additions & 3 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,23 @@ UserSchema.statics.findByCredentials = async function (email, password) {
})

if (!user) {
return Promise.reject(new Error('Nessun utente registrato con l\'email inserita.'))
const e = new Error('Nessun utente registrato con l\'email inserita.')
e.name = 'bad_email'
return Promise.reject(e)
}

if (user.state !== 'active') {
return Promise.reject(new Error('Il tuo account è stato disabilitato.'))
const e = new Error('Il tuo account è stato disabilitato.')
e.name = 'account_disabled'
return Promise.reject(e)
}

if (await bcrypt.compare(password, user.password)) {
return Promise.resolve(user)
} else {
return Promise.reject(new Error('Password errata'))
const e = new Error('La password inserita non è corretta.')
e.name = 'bad_password'
return Promise.reject(e)
}
} catch (e) {
return Promise.reject(e)
Expand Down
2 changes: 1 addition & 1 deletion server/routes/api/v1/routes/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ router.delete('/:id', authenticate, editDocument, asyncMiddleware(async (req, re
}
})

fs.unlink(path.join(__dirname, '..', 'public', 'public', 'documents', document.directory), (err) => {
fs.unlink(path.join(__dirname, '..', '..', '..', '..', 'public', 'public', 'documents', document.directory), (err) => {
if (err) {
return res.status(500).json({
messages: ['Impossibile eliminare il documento.']
Expand Down
7 changes: 1 addition & 6 deletions server/routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ const {
*/
router.post('/', signin, asyncMiddleware(async (req, res) => {
const {user} = req
let token
try {
token = await user.generateAuthToken()
} catch (e) {
throw e
}
const token = await user.generateAuthToken()
res.status(200).json({
token
})
Expand Down

0 comments on commit 4245b50

Please sign in to comment.