Permalink
Browse files

Completed BasicAuth middleware and added Hash provider to providers list

  • Loading branch information...
thetutlage committed Jan 17, 2016
1 parent 070f6c2 commit 5e18dc4a170532ceabbb743378705069e17770f9
Showing with 51 additions and 2 deletions.
  1. +46 −0 app/Http/Middleware/BasicAuth.js
  2. +4 −2 bootstrap/app.js
  3. +1 −0 package.json
@@ -0,0 +1,46 @@
'use strict'

const auth = use('basic-auth')
const User = use('App/Model/User')
const Hash = use('Hash')

class BasicAuth {

* handle (request, response, next) {
const credentials = auth(request.request)

if (!credentials) {
response.status(401).send({
error: 'Please enter your account credentials'
})
return
}

// CHECKING FOR USER INSIDE USERS TABLE
const user = yield User
.where('username', credentials.name)
.first()
.fetch()

if (!user.size()) {
response.status(401).send({
error: 'Unable to find user with given username'
})
return
}

const userPassword = user.get('password')
const result = yield Hash.verify(credentials.pass, userPassword)

if (!result) {
response.status(401).send({
error: 'Password mismatch'
})
return
}
yield next
}

}

module.exports = BasicAuth
@@ -25,7 +25,8 @@ const providers = [
'adonis-framework/providers/SessionProvider',
'adonis-lucid/providers/DatabaseProvider',
'adonis-lucid/providers/LucidProvider',
'adonis-ace/providers/AnsiProvider'
'adonis-ace/providers/AnsiProvider',
'adonis-framework/providers/HashProvider'
]

/*
@@ -68,7 +69,8 @@ const aliases = {
Lucid: 'Adonis/Src/Lucid',
Runner: 'Adonis/Src/Runner',
Schema: 'Adonis/Src/Schema',
Ansi: 'Adonis/Src/Ansi'
Ansi: 'Adonis/Src/Ansi',
Hash : 'Adonis/Src/Hash'
}

/*
@@ -14,6 +14,7 @@
"adonis-fold": "^2.0.0",
"adonis-framework": "^2.0.5",
"adonis-lucid": "^2.0.2",
"basic-auth": "^1.0.3",
"formidable": "^1.0.17",
"ouch": "^0.2.0-beta",
"raw-body": "^2.1.5"

0 comments on commit 5e18dc4

Please sign in to comment.