user.js 2.68 KB
Newer Older
abergavenny's avatar
abergavenny committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import mongoose from 'mongoose'

import { User } from '../index.js'
import { matchEncryptedString } from '../../utils/crypto.js'

export const userSchema = new mongoose.Schema({
  email: {
    type: String,
    immutable: true,
    lowercase: true
  },
  username: {
    type: String,
    immutable: true
  },
  encryptedPassword: {
    type: String,
    minlength: [8, 'TOO_FEW_CHARACTERS'],
    required: [true, 'PASSWORD_REQUIRED']
  },
  role: {
    type: String,
    required: true
  },
  linkedTo: mongoose.SchemaTypes.ObjectId,
  apartments: [mongoose.SchemaTypes.ObjectId],
  sharingAllowed: {
    type: Boolean,
    default: false
  },
  lastLogin: {
    type: Date,
    default: null
  },
  confirmationToken: {
    type: String,
    default: null
  },
  confirmationExpireAt: {
    type: Date,
    default: null
  },
  emailConfirmed: {
    type: Boolean,
    default: false
  },
  resetToken: {
    type: String,
    default: null
  },
  resetExpireAt: {
    type: Date,
    default: null
  },
  lastResetRequest: {
    type: Date,
    default: null
  },
  createdBy: mongoose.SchemaTypes.ObjectId,
  createdAt: {
    type: Date,
    immutable: true,
    default: () => Date.now()
  },
  updatedAt: {
    type: Date,
    default: () => Date.now()
  }
})

userSchema.methods.comparePassword = async function (password) {
  return matchEncryptedString(password, this.password) || false
}

userSchema.statics.verifyPasswordResetAndUpdate = async function (token, data) {
  const user = await this.findOne({ resetToken: token })

  if (user) {
    return await this.findOneAndUpdate({ _id: user._id }, data, {
      new: true,
      runValidators: true
    })
  }

  return null
}

userSchema.statics.createBuildingUser = async function (accessor, data) {
  const isOwner = await this.findOne({ _id: accessor })

  if (isOwner) {
    const exists = await User.findOne({ username: data.username })

    if (exists) return null

    return await this.create(data)
  }

  return null
}

userSchema.statics.createNewAccount = async function (email, data) {
  const exists = await User.findOne({ email })

  if (exists) return null

  const user = await User.create(data)

  return user
}

userSchema.statics.findUserAndVerify = async function (emailOrUsername, password) {
  const byUsername = await this.findOne({ username: emailOrUsername }).exec()
  const byEmail = await this.findOne({ email: emailOrUsername }).exec()

  const user = byUsername || byEmail

  if (user && matchEncryptedString(password, user.encryptedPassword)) {
    return this.findOne({
      $or: [{ username: user.username }, { email: user.email || '2ad98ac316a093cd32f2b05296d98e3b' }]
    })
  }

  return null
}

export default mongoose.model('User', userSchema)