aboutsummaryrefslogtreecommitdiffstats
path: root/models/User.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/User.js')
-rw-r--r--models/User.js55
1 files changed, 27 insertions, 28 deletions
diff --git a/models/User.js b/models/User.js
index 44d9619..bcf2523 100644
--- a/models/User.js
+++ b/models/User.js
@@ -1,6 +1,6 @@
-const mongoose = require("mongoose")
+const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')
-const {nanoid} = require('nanoid')
+const { nanoid } = require('nanoid')
const userResponse = u => ({
_id: u._id,
@@ -20,7 +20,7 @@ const userSchema = new mongoose.Schema({
required: true,
minlength: 6,
maxlength: 255,
- validate(value) {
+ validate (value) {
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
throw new Error('Wrong email address')
}
@@ -30,29 +30,29 @@ const userSchema = new mongoose.Schema({
type: String,
required: true,
minlength: 6,
- maxlength: 1024,
+ maxlength: 1024
},
isVerified: {
type: Boolean,
- default: false,
+ default: false
},
verificationKey: {
type: String,
- default: () => nanoid(6),
+ default: () => nanoid(6)
},
noteList: {
type: mongoose.Schema.Types.ObjectId,
- ref: "NoteList",
- required: true,
+ ref: 'NoteList',
+ required: true
},
- theme: {type: String, default: () => 'green'},
- language: {type: String, default: () => 'en'},
- created_at: {type: Date, default: Date.now},
- updated_at: {type: Date, default: Date.now}
+ theme: { type: String, default: () => 'green' },
+ language: { type: String, default: () => 'en' },
+ created_at: { type: Date, default: Date.now },
+ updated_at: { type: Date, default: Date.now }
})
-userSchema.statics.findByCredentials = async function (email, password){
- const user = await User.findOne({email})
+userSchema.statics.findByCredentials = async function (email, password) {
+ const user = await User.findOne({ email })
if (!user) {
throw new Error('Unable to login')
@@ -67,8 +67,8 @@ userSchema.statics.findByCredentials = async function (email, password){
return userResponse(user)
}
-userSchema.statics.getVerificationKey = async function (email){
- const {verificationKey: key} = await User.findOne({email})
+userSchema.statics.getVerificationKey = async function (email) {
+ const { verificationKey: key } = await User.findOne({ email })
if (!key) {
throw new Error('Could not verify user')
@@ -77,8 +77,8 @@ userSchema.statics.getVerificationKey = async function (email){
return key
}
-userSchema.statics.verifyUser = async function (_id, key){
- const user = await User.findOneAndUpdate({_id, verificationKey: key}, {isVerified: true, verificationKey: nanoid(10)}, {new: true})
+userSchema.statics.verifyUser = async function (_id, key) {
+ const user = await User.findOneAndUpdate({ _id, verificationKey: key }, { isVerified: true, verificationKey: nanoid(10) }, { new: true })
if (!user) {
throw new Error('Could not verify user')
@@ -87,8 +87,8 @@ userSchema.statics.verifyUser = async function (_id, key){
return userResponse(user)
}
-userSchema.statics.saveSettings = async function ({_id, theme, language}){
- const user = await User.findOneAndUpdate({_id}, {theme, language}, {new: true})
+userSchema.statics.saveSettings = async function ({ _id, theme, language }) {
+ const user = await User.findOneAndUpdate({ _id }, { theme, language }, { new: true })
if (!user) {
throw new Error('Could not save settings')
@@ -97,24 +97,23 @@ userSchema.statics.saveSettings = async function ({_id, theme, language}){
return userResponse(user)
}
-userSchema.statics.state = async function (_id){
- const user = await User.findOne({_id})
+userSchema.statics.state = async function (_id) {
+ const user = await User.findOne({ _id })
if (!user) {
- return {isLoggedIn: false}
+ return { isLoggedIn: false }
} else if (!user.isVerified) {
- return {isVerified: false}
+ return { isVerified: false }
}
return {}
}
-
-userSchema.pre('save', async function(next){
- const user = this;
+userSchema.pre('save', async function (next) {
+ const user = this
if (user.isModified('password')) {
- user.password = await bcrypt.hash(user.password, 8);
+ user.password = await bcrypt.hash(user.password, 8)
}
user.updated_at = Date.now()