Files
NodeJS-for-Beginners/step6/stores/user.js
T
Ulises Gascón 9dd233c371 WIP
2024-03-25 00:59:06 +01:00

24 lines
532 B
JavaScript

import {
User
} from '../database.js'
const create = async (username, password, email) => {
const user = new User({ username, password, email })
await user.save()
return user
}
const getUserByCredentials = async (username, password) => {
const user = await User.findOne({ username })
if (!user) {
throw new Error('User not found')
}
const isMatch = await user.comparePassword(password)
if (!isMatch) {
throw new Error('Password is incorrect')
}
return user
}
export { create, getUserByCredentials }