-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd226d5
commit 02ffcf8
Showing
10 changed files
with
25,815 additions
and
5,790 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import mongoose, { ObjectId, Schema, model } from 'mongoose'; | ||
import ITokens from './tokens.types'; | ||
import toJSON from '../plugins/toJSON'; | ||
|
||
interface IToken { | ||
token: string; | ||
user: ObjectId; | ||
type: string; | ||
expires: Date; | ||
blacklisted: boolean; | ||
} | ||
|
||
const tokenSchema = new Schema<IToken>( | ||
{ | ||
token: { | ||
type: String, | ||
required: true, | ||
index: true, | ||
}, | ||
user: { | ||
type: mongoose.SchemaTypes.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
}, | ||
type: { | ||
type: String, | ||
enum: [ITokens.REFRESH, ITokens.RESET_PASSWORD, ITokens.VERIFY_EMAIL], | ||
required: true, | ||
}, | ||
expires: { | ||
type: Date, | ||
required: true, | ||
}, | ||
blacklisted: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
// add plugin that converts mongoose to json | ||
tokenSchema.plugin(toJSON); | ||
|
||
const Token = model<IToken>('Token', tokenSchema); | ||
|
||
export default Token; |
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Schema, model, Model } from 'mongoose'; | ||
import validator from 'validator'; | ||
import bcrypt from 'bcryptjs'; | ||
import toJSON from '../plugins/toJSON'; | ||
import paginate from '../plugins/paginate'; | ||
import { roles } from '../config/roles'; | ||
|
||
interface IUser { | ||
name: string; | ||
email: string; | ||
password: string; | ||
role?: string; | ||
isEmailVerified?: boolean; | ||
} | ||
|
||
interface IStatics extends Model<IUser> { | ||
isEmailTaken(): Promise<boolean>; | ||
isPasswordMatch(): Promise<boolean>; | ||
} | ||
|
||
const userSchema = new Schema<IUser, IStatics>( | ||
{ | ||
name: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
trim: true, | ||
lowercase: true, | ||
validate(value) { | ||
if (!validator.isEmail(value)) { | ||
throw new Error('Invalid email'); | ||
} | ||
}, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
minlength: 8, | ||
validate(value) { | ||
if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) { | ||
throw new Error('Password must contain at least one letter and one number'); | ||
} | ||
}, | ||
private: true, // used by the toJSON plugin | ||
}, | ||
role: { | ||
type: String, | ||
enum: roles, | ||
default: 'user', | ||
}, | ||
isEmailVerified: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
// add plugin that converts mongoose to json | ||
userSchema.plugin(toJSON); | ||
userSchema.plugin(paginate); | ||
|
||
/** | ||
* Check if email is taken | ||
* @param {string} email - The user's email | ||
* @param {ObjectId} [excludeUserId] - The id of the user to be excluded | ||
* @returns {Promise<boolean>} | ||
*/ | ||
userSchema.statics.isEmailTaken = async function (email, excludeUserId) { | ||
const user = await this.findOne({ email, _id: { $ne: excludeUserId } }); | ||
return !!user; | ||
}; | ||
|
||
/** | ||
* Check if password matches the user's password | ||
* @param {string} password | ||
* @returns {Promise<boolean>} | ||
*/ | ||
userSchema.methods.isPasswordMatch = async function (password) { | ||
const user = this; | ||
return bcrypt.compare(password, user.password); | ||
}; | ||
|
||
userSchema.pre('save', async function (next) { | ||
const user = this; | ||
if (user.isModified('password')) { | ||
user.password = await bcrypt.hash(user.password, 8); | ||
} | ||
next(); | ||
}); | ||
|
||
const User = model<IUser>('User', userSchema); | ||
|
||
export default User; |
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
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
Oops, something went wrong.