finished user store in typscript

This commit is contained in:
canove
2020-09-14 18:54:36 -03:00
parent eba3553a2d
commit 7f33e33078
29 changed files with 260 additions and 133 deletions

View File

@@ -0,0 +1,48 @@
import { sign } from "jsonwebtoken";
import User from "../models/User";
import AppError from "../errors/AppError";
import authConfig from "../config/auth";
interface Request {
email: string;
password: string;
}
interface Response {
user: User;
token: string;
}
const AuthUserService = async ({
email,
password
}: Request): Promise<Response> => {
const user = await User.findOne({
where: { email }
});
if (!user) {
throw new AppError("Incorrect user/password combination.", 401);
}
if (!(await user.checkPassword(password))) {
throw new AppError("Incorrect user/password combination.", 401);
}
const { secret, expiresIn } = authConfig;
const token = sign(
{ usarname: user.name, profile: user.profile, id: user.id },
secret,
{
expiresIn
}
);
return {
user,
token
};
};
export default AuthUserService;

View File

@@ -10,44 +10,59 @@ interface Request {
profile?: string;
}
interface Response {
email: string;
name: string;
id: number;
profile: string;
}
const CreateUserService = async ({
email,
password,
name,
profile = "admin"
}: Request): Promise<User> => {
// const schema = Yup.object().shape({
// name: Yup.string().required().min(2),
// email: Yup.string()
// .email()
// .required()
// .test(
// "Check-email",
// "An user with this email already exists.",
// async value => {
// const emailExists = await User.findOne({
// where: { email: value }
// });
// return !Boolean(emailExists);
// }
// ),
// password: Yup.string().required().min(5)
// });
}: Request): Promise<Response> => {
const schema = Yup.object().shape({
name: Yup.string().required().min(2),
email: Yup.string()
.email()
.required()
.test(
"Check-email",
"An user with this email already exists.",
async value => {
if (value) {
const emailExists = await User.findOne({
where: { email: value }
});
return !emailExists;
}
return false;
}
),
password: Yup.string().required().min(5)
});
// try {
// await schema.validate({ email, password, name });
// } catch (err) {
// throw new AppError(err.message);
// }
try {
await schema.validate({ email, password, name });
} catch (err) {
throw new AppError(err.message);
}
const user = User.create({
const user = await User.create({
email,
password,
name,
profile
});
return user;
return {
id: user.id,
name: user.name,
email: user.email,
profile: user.profile
};
};
export default CreateUserService;

View File

@@ -1,21 +1,17 @@
import { getRepository, Raw } from "typeorm";
import User from "../models/User";
import AppError from "../errors/AppError";
const FindUserService = async (id: string): Promise<User | undefined> => {
const usersRepository = getRepository(User);
const user = await User.findOne({
where: { id },
attributes: ["name", "id", "email", "profile"]
});
const user = await usersRepository.findOne({
where: { id },
select: ["name", "id", "email", "profile"],
});
if (!user) {
throw new AppError("No user found with this ID.", 404);
}
if (!user) {
throw new AppError("No user found with this ID.", 404);
}
return user;
return user;
};
export default FindUserService;