mirror of
https://github.com/sasjs/server.git
synced 2026-01-01 20:40:06 +00:00
chore: refactored, added instance + static methods to models
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { Security, Route, Tags, Example, Post, Body, Query, Hidden } from 'tsoa'
|
import { Security, Route, Tags, Example, Post, Body, Query, Hidden } from 'tsoa'
|
||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
import bcrypt from 'bcryptjs'
|
|
||||||
import User from '../model/User'
|
import User from '../model/User'
|
||||||
import { InfoJWT } from '../types'
|
import { InfoJWT } from '../types'
|
||||||
import { removeTokensInDB, saveTokensInDB } from '../utils'
|
import { removeTokensInDB, saveTokensInDB } from '../utils'
|
||||||
@@ -80,7 +79,7 @@ const authorize = async (data: any): Promise<AuthorizeResponse> => {
|
|||||||
const user = await User.findOne({ username })
|
const user = await User.findOne({ username })
|
||||||
if (!user) throw new Error('Username is not found.')
|
if (!user) throw new Error('Username is not found.')
|
||||||
|
|
||||||
const validPass = await bcrypt.compare(password, user.password)
|
const validPass = user.comparePassword(password)
|
||||||
if (!validPass) throw new Error('Invalid password.')
|
if (!validPass) throw new Error('Invalid password.')
|
||||||
|
|
||||||
// generate authorization code against clientId
|
// generate authorization code against clientId
|
||||||
|
|||||||
@@ -163,7 +163,10 @@ const createGroup = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getGroup = async (groupId: number): Promise<GroupDetailsResponse> => {
|
const getGroup = async (groupId: number): Promise<GroupDetailsResponse> => {
|
||||||
const group = (await Group.findOne({ groupId }).populate(
|
const group = (await Group.findOne(
|
||||||
|
{ groupId },
|
||||||
|
'groupId name description isActive users -_id'
|
||||||
|
).populate(
|
||||||
'users',
|
'users',
|
||||||
'id username displayName -_id'
|
'id username displayName -_id'
|
||||||
)) as unknown as GroupDetailsResponse
|
)) as unknown as GroupDetailsResponse
|
||||||
@@ -193,7 +196,13 @@ const addUserToGroup = async (
|
|||||||
)) as unknown as GroupDetailsResponse
|
)) as unknown as GroupDetailsResponse
|
||||||
if (!updatedGroup) throw new Error('Unable to update group')
|
if (!updatedGroup) throw new Error('Unable to update group')
|
||||||
|
|
||||||
return updatedGroup
|
return {
|
||||||
|
groupId: updatedGroup.groupId,
|
||||||
|
name: updatedGroup.name,
|
||||||
|
description: updatedGroup.description,
|
||||||
|
isActive: updatedGroup.isActive,
|
||||||
|
users: updatedGroup.users
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeUserFromGroup = async (
|
const removeUserFromGroup = async (
|
||||||
@@ -211,5 +220,11 @@ const removeUserFromGroup = async (
|
|||||||
)) as unknown as GroupDetailsResponse
|
)) as unknown as GroupDetailsResponse
|
||||||
if (!updatedGroup) throw new Error('Unable to update group')
|
if (!updatedGroup) throw new Error('Unable to update group')
|
||||||
|
|
||||||
return updatedGroup
|
return {
|
||||||
|
groupId: updatedGroup.groupId,
|
||||||
|
name: updatedGroup.name,
|
||||||
|
description: updatedGroup.description,
|
||||||
|
isActive: updatedGroup.isActive,
|
||||||
|
users: updatedGroup.users
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import {
|
|||||||
Body,
|
Body,
|
||||||
Hidden
|
Hidden
|
||||||
} from 'tsoa'
|
} from 'tsoa'
|
||||||
import bcrypt from 'bcryptjs'
|
|
||||||
|
|
||||||
import User, { UserPayload } from '../model/User'
|
import User, { UserPayload } from '../model/User'
|
||||||
|
|
||||||
@@ -131,8 +130,7 @@ const createUser = async (data: UserPayload): Promise<UserDetailsResponse> => {
|
|||||||
if (usernameExist) throw new Error('Username already exists.')
|
if (usernameExist) throw new Error('Username already exists.')
|
||||||
|
|
||||||
// Hash passwords
|
// Hash passwords
|
||||||
const salt = await bcrypt.genSalt(10)
|
const hashPassword = User.hashPassword(password)
|
||||||
const hashPassword = await bcrypt.hash(password, salt)
|
|
||||||
|
|
||||||
// Create a new user
|
// Create a new user
|
||||||
const user = new User({
|
const user = new User({
|
||||||
@@ -180,8 +178,7 @@ const updateUser = async (
|
|||||||
|
|
||||||
if (password) {
|
if (password) {
|
||||||
// Hash passwords
|
// Hash passwords
|
||||||
const salt = await bcrypt.genSalt(10)
|
params.password = User.hashPassword(password)
|
||||||
params.password = await bcrypt.hash(password, salt)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedUser = await User.findOneAndUpdate({ id }, params, { new: true })
|
const updatedUser = await User.findOneAndUpdate({ id }, params, { new: true })
|
||||||
@@ -208,7 +205,7 @@ const deleteUser = async (
|
|||||||
if (!user) throw new Error('User is not found.')
|
if (!user) throw new Error('User is not found.')
|
||||||
|
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
const validPass = await bcrypt.compare(password!, user.password)
|
const validPass = user.comparePassword(password!)
|
||||||
if (!validPass) throw new Error('Invalid password.')
|
if (!validPass) throw new Error('Invalid password.')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ interface IGroup extends IGroupDocument {
|
|||||||
}
|
}
|
||||||
interface IGroupModel extends Model<IGroup> {}
|
interface IGroupModel extends Model<IGroup> {}
|
||||||
|
|
||||||
const groupSchema = new Schema({
|
const groupSchema = new Schema<IGroupDocument>({
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true
|
required: true
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import mongoose, { Schema, model } from 'mongoose'
|
import mongoose, { Schema, model, Document, Model } from 'mongoose'
|
||||||
const AutoIncrement = require('mongoose-sequence')(mongoose)
|
const AutoIncrement = require('mongoose-sequence')(mongoose)
|
||||||
|
import bcrypt from 'bcryptjs'
|
||||||
|
|
||||||
export interface UserPayload {
|
export interface UserPayload {
|
||||||
/**
|
/**
|
||||||
@@ -28,7 +29,7 @@ export interface UserPayload {
|
|||||||
isActive?: boolean
|
isActive?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface User extends UserPayload {
|
interface IUserDocument extends UserPayload, Document {
|
||||||
id: number
|
id: number
|
||||||
isAdmin: boolean
|
isAdmin: boolean
|
||||||
isActive: boolean
|
isActive: boolean
|
||||||
@@ -36,7 +37,14 @@ interface User extends UserPayload {
|
|||||||
tokens: [{ [key: string]: string }]
|
tokens: [{ [key: string]: string }]
|
||||||
}
|
}
|
||||||
|
|
||||||
const UserSchema = new Schema<User>({
|
interface IUser extends IUserDocument {
|
||||||
|
comparePassword(password: string): boolean
|
||||||
|
}
|
||||||
|
interface IUserModel extends Model<IUser> {
|
||||||
|
hashPassword(password: string): string
|
||||||
|
}
|
||||||
|
|
||||||
|
const userSchema = new Schema<IUserDocument>({
|
||||||
displayName: {
|
displayName: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true
|
required: true
|
||||||
@@ -76,6 +84,20 @@ const UserSchema = new Schema<User>({
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
UserSchema.plugin(AutoIncrement, { inc_field: 'id' })
|
userSchema.plugin(AutoIncrement, { inc_field: 'id' })
|
||||||
|
|
||||||
export default model('User', UserSchema)
|
// Static Methods
|
||||||
|
userSchema.static('hashPassword', (password: string): string => {
|
||||||
|
const salt = bcrypt.genSaltSync(10)
|
||||||
|
return bcrypt.hashSync(password, salt)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Instance Methods
|
||||||
|
userSchema.method('comparePassword', function (password: string): boolean {
|
||||||
|
if (bcrypt.compareSync(password, this.password)) return true
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
export const User: IUserModel = model<IUser, IUserModel>('User', userSchema)
|
||||||
|
|
||||||
|
export default User
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import mongoose from 'mongoose'
|
import mongoose from 'mongoose'
|
||||||
import jwt from 'jsonwebtoken'
|
|
||||||
|
|
||||||
import AuthController from '../../controllers/auth'
|
import AuthController from '../../controllers/auth'
|
||||||
import Client from '../../model/Client'
|
import Client from '../../model/Client'
|
||||||
@@ -10,12 +9,7 @@ import {
|
|||||||
authenticateRefreshToken
|
authenticateRefreshToken
|
||||||
} from '../../middlewares'
|
} from '../../middlewares'
|
||||||
|
|
||||||
import {
|
import { authorizeValidation, tokenValidation } from '../../utils'
|
||||||
authorizeValidation,
|
|
||||||
removeTokensInDB,
|
|
||||||
saveTokensInDB,
|
|
||||||
tokenValidation
|
|
||||||
} from '../../utils'
|
|
||||||
import { InfoJWT } from '../../types'
|
import { InfoJWT } from '../../types'
|
||||||
|
|
||||||
const authRouter = express.Router()
|
const authRouter = express.Router()
|
||||||
|
|||||||
Reference in New Issue
Block a user