const { Controller } = require('libflitter') class AuthController extends Controller { static get services() { return [...super.services, 'models', 'auth']; } async get_user_info(req, res, next) { const User = this.models.get('auth:User') if ( !req.query.uid ) { return res.status(400).api() } const user = await User.findOne({ uid: req.query.uid, provider: 'flitter', }) if ( user ) { return res.api({ exists: true, uid: user.uid, id: user.id, }) } return res.api({ exists: false }) } async attempt_registration(req, res, next) { const flitter = this.auth.get_provider('flitter') const form_data = { username: req.body.uid, password: req.body.password, } const errors = await flitter.validate_registration(form_data) if ( !req.body.fullName ) { errors.push('Full name is required.') } if ( req.body.password !== req.body.passwordConfirmation ) { errors.push('Password does not match confirmation.') } if ( errors.length ) { return res.api({ success: false, message: errors[0] }) } const [username, attrs] = await flitter.get_registration_args(form_data) attrs.full_name = req.body.fullName const user = await flitter.register(username, attrs, undefined) if ( !user ) { return res.api({ success: false, message: 'An unknown error has occurred' }) } await flitter.session(req, user) return res.api({ success: true }) } async attempt_authentication(req, res, next) { const flitter = this.auth.get_provider('flitter') if ( !req.body.uid ) { return res.status(400) .message('Missing required field: uid') .api() } if ( !req.body.password ) { return res.status(400) .message('Missing required field: password') .api() } const user = await flitter.login(req.body.uid, req.body.password) if ( !user ) { return res.api({ success: false, message: 'Invalid username or password' }) } await flitter.session(req, user) return res.api({ success: true }) } async end_session(req, res, next) { const flitter = this.auth.get_provider('flitter') await flitter.logout(req); return res.api(); } } module.exports = exports = AuthController