Skip to content

Instantly share code, notes, and snippets.

@ea2305
Created May 14, 2019 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ea2305/7354b0b9c3fe8fc859da47a471f5f123 to your computer and use it in GitHub Desktop.
Save ea2305/7354b0b9c3fe8fc859da47a471f5f123 to your computer and use it in GitHub Desktop.
'use strict'
const Env = use('Env')
const Logger = use('Logger')
class AuthController {
/**
* Login user controller
* @param {Object} request : HTTP Request
* @param {Object} response : HTTP Request
*/
async login ({ request, response, auth }) {
// get user request information
const filter = ['email', 'password']
const { email, password } = request.only(filter)
try {
// Attemp login
const token = await auth.attempt(email, password)
// return token to client
return response.ok({ token: token.token, type: token.type })
} catch (error) {
if (Env.get('Logger') == 'true')
Logger.error(error)
return response.unauthorized({ error: 'bad credentials' })
}
}
}
module.exports = AuthController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment