summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2023-08-10 18:42:20 +0200
committerGravatar piotrruss <mail@pruss.it> 2023-08-10 22:47:33 +0200
commitbdb0762696ccf028c30b2957b93a01da7bf67571 (patch)
treeffb3e9483eb5e0fcfa1aed4a6cbecc3b28a9e09a /model
downloadauth-service-bdb0762696ccf028c30b2957b93a01da7bf67571.tar.gz
auth-service-bdb0762696ccf028c30b2957b93a01da7bf67571.tar.bz2
auth-service-bdb0762696ccf028c30b2957b93a01da7bf67571.zip
init commit
Diffstat (limited to 'model')
-rw-r--r--model/User.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/model/User.js b/model/User.js
new file mode 100644
index 0000000..cbd1e60
--- /dev/null
+++ b/model/User.js
@@ -0,0 +1,108 @@
+const mongoose = require('mongoose')
+const bcrypt = require('bcryptjs')
+const { randomBytes } = require('crypto')
+
+const userSchema = new mongoose.Schema({
+ email: {
+ type: String,
+ trim: true,
+ lowercase: true,
+ unique: true,
+ required: true,
+ min: 4,
+ max: 255,
+ validate: {
+ validator: v => /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v),
+ message: "Please enter a valid email"
+ },
+ },
+ password: {
+ type: String,
+ required: true,
+ max: 1024,
+ min: 6,
+ },
+ refresh: {
+ type: String,
+ required: true,
+ max: 1024,
+ min: 6,
+ },
+ verify: {
+ type: String,
+ default: randomBytes(3).toString('hex'),
+ },
+ role: {
+ type: String,
+ max: 16,
+ min: 3,
+ default: 'user',
+ },
+ date: {
+ type: Date,
+ default: Date.now,
+ },
+})
+
+userSchema.statics.checkRefreshToken = async (email, refresh) => {
+ const user = await User.findOne({ email })
+
+ if (!user) {
+ throw new Error('User not found')
+ }
+
+ const isMatch = await bcrypt.compare(refresh, user.refresh)
+
+ if (!isMatch) {
+ throw new Error('Wrong refresh token')
+ }
+
+ return user
+}
+
+userSchema.statics.findByCredentials = async (email, password) => {
+ const user = await User.findOne({ email })
+
+ if (!user) {
+ throw new Error('Unable to login')
+ }
+
+ const isMatch = await bcrypt.compare(password, user.password)
+
+ if (!isMatch) {
+ throw new Error('Unable to login')
+ }
+
+ return user
+}
+
+userSchema.statics.newRefreshToken = async (user) => {
+ const newRefreshToken = randomBytes(32).toString('hex')
+ user.refresh = newRefreshToken
+
+ try {
+ await user.save()
+ return newRefreshToken
+ } catch(err) {
+ return ''
+ }
+}
+
+
+userSchema.pre('save', async function(next){
+ const user = this
+
+ if (user.isModified('password')) {
+ user.password = await bcrypt.hash(user.password, 8)
+ }
+
+ if (user.isModified('refresh')) {
+ user.refresh = await bcrypt.hash(user.refresh, 8)
+ }
+
+ next()
+})
+
+const User = mongoose.model('User', userSchema)
+
+module.exports = User