-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
signup with password hashing implemented
- Loading branch information
1 parent
d5aacf0
commit f9e1709
Showing
4 changed files
with
595 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
const mongoose = require('mongoose'); | ||
const bcrypt = require('bcrypt'); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: [true, 'Email is required'], | ||
required: true, | ||
unique: true, | ||
trim: true, | ||
trim: true | ||
|
||
}, | ||
password: { | ||
type: String, | ||
required: [true, 'Password is required'], | ||
minlength: [6, 'Password must be at least 6 characters'] | ||
required: true | ||
} | ||
}, { timestamps: true }); | ||
|
||
//static signup method | ||
userSchema.statics.signup = async function(email, password) { | ||
const exists = await this.findOne({ email }); | ||
|
||
if (exists) { | ||
throw new Error('Email already in use'); | ||
} | ||
|
||
const salt = await bcrypt.genSalt(10); | ||
const hashedPassword = await bcrypt.hash(password, salt); | ||
|
||
const user = await this.create({ email, password: hashedPassword }); | ||
|
||
return user; | ||
}; | ||
|
||
module.exports = mongoose.model('User', userSchema); |
Oops, something went wrong.