"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const typeorm_1 = require("typeorm");
const entities_1 = require("../entities");
async function signup(attrs) {
    const repository = typeorm_1.getRepository(entities_1.User);
    const old = await repository.findOne({ email: attrs.email });
    if (old) {
        throw new Error('user duplicated.');
    }
    const newattrs = Object.assign({ userType: 'user' }, attrs, { password: entities_1.User.encode(attrs.password) });
    const user = await repository.save(Object.assign({}, newattrs));
    return await signin({
        email: attrs.email,
        password: attrs.password
    });
}
exports.signup = signup;
async function signin(attrs) {
    const repository = typeorm_1.getRepository(entities_1.User);
    const user = await repository.findOne({ email: attrs.email });
    if (!user) {
        throw new Error('user notfound.');
    }
    if (!user.veryfy(attrs.password)) {
        throw new Error('password not match.');
    }
    return await user.sign();
}
exports.signin = signin;
async function authcheck(email) {
    const repository = typeorm_1.getRepository(entities_1.User);
    const user = await repository.findOne({ email });
    if (!user) {
        throw new Error('user notfound.');
    }
    return await user.sign();
}
exports.authcheck = authcheck;
//# sourceMappingURL=auth.js.map