import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { check } from 'meteor/check';




Meteor.methods({
    'changePass' (username, oldPassword, newPassword) {
        debugger;
        this.unblock();
        check(username, String);
        check(oldPassword, String);
        check(newPassword, String);

        const user = Accounts.findUserByUsername(username);

        if (user == null) {
            throw new Meteor.Error("NotFoundUser");
        }

        // const digestOldPassword = sha256(oldPassword);

        // const password = { digest: digestOldPassword, algorithm: 'sha-256' };

        const result = Accounts._checkPassword(user, oldPassword);

        if (result.error != null) {
            throw new Meteor.Error("CheckPassError");
        }
        Accounts.setPassword(user._id, newPassword);
    },
});