const jwt = require('jsonwebtoken');

module.exports = {


    friendlyName: 'remove token',


    description: '',


    inputs: {
        token: {
            type: 'string',
            required:true
        },
        id: {
            type: 'number',
            required:true
        }
    },


    exits: {
        success: {
            description: 'Success',

        },
        forbidden:{
           description: 'forbidden'
        }
    },


    fn: async function (inputs, exits) {

        let token = await Token.find({token: inputs.token,
            owner:inputs.id}).limit(1).then(t => t.length > 0 ? t[0] : null)
            .intercept(err=>{throw err})
        if(!token){throw 'forbidden'}
        await Token.destroy(token.id)
        sails.log(`token : ${token.id} record destroyed !`);
        return exits.success()
    }
};

