Created
January 30, 2019 13:18
-
-
Save hypeJunction/7c5307b383df47af6f18fe103ae9b869 to your computer and use it in GitHub Desktop.
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
import moment from 'moment'; | |
import Response from '../app/Response'; | |
export default class AccountController { | |
static async login (request, response, next) { | |
const services = request.app.get('services'); | |
const { email, password, remember } = request.body; | |
const time = moment(); | |
const expiresAt = remember ? time.add(30, 'days') : time.add(10, 'minutes'); | |
try { | |
const user = await services.auth.authenticate(email, password); | |
const token = await services.auth.createToken(user, expiresAt.utc().unix(), remember); | |
user.last_login = moment().toDate(); | |
user.save(); | |
services.session.setUser(user); | |
next(Response.ok({ | |
user: await user.toJSON(), | |
token, | |
})); | |
} catch (err) { | |
next(Response.error(err)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment