diff --git a/server/src/model/user.ts b/server/src/model/user.ts index d6fb2f2..1085d81 100644 --- a/server/src/model/user.ts +++ b/server/src/model/user.ts @@ -1,8 +1,11 @@ -import { Table, Column, Model, CreatedAt, DataType, Scopes} from 'sequelize-typescript'; +import { Table, Column, Model, CreatedAt, DataType, Scopes, DefaultScope} from 'sequelize-typescript'; +@DefaultScope(() => ({ + attributes:{ exclude: ['password']} +})) @Scopes(() => ({ - withoutPassword: { - attributes: { exclude: ['password'] } + withPassword: { + attributes: {include: ['password']} } })) @Table diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts index ec50640..df9ef2f 100644 --- a/server/src/routes/auth.ts +++ b/server/src/routes/auth.ts @@ -2,6 +2,7 @@ import { compare } from 'bcrypt'; import express from 'express'; import { logger } from '../util/logging'; import User from '../model/user'; +import { Scope } from '../util/db'; import { getJWT, requireAuth } from '../util/auth'; import { LoginRequest } from '../messages/Login'; import { GenericMessage as Msg } from '../messages/Message'; @@ -11,7 +12,7 @@ const router = express.Router(); router.post('/login', async (req, res) => { try { const data : LoginRequest = req.body; - const user = await User.findOne({where: { userID: data.username}}); + const user = await User.scope(Scope.withPassword).findOne({where: { userID: data.username}}); if (!user) return res.status(401).json(new Msg('Invalid credentials')); const isMatch = await compare(data.password, user.password); if (!isMatch) return res.status(401).json(new Msg('Invalid credentials')); diff --git a/server/src/util/db.ts b/server/src/util/db.ts index 0df2c06..ac904f1 100644 --- a/server/src/util/db.ts +++ b/server/src/util/db.ts @@ -3,6 +3,11 @@ import { logger } from './logging'; import User from '../model/user'; import Transaction from '../model/transaction'; +enum Scope{ + // for User + withPassword = 'withPassword', +} + // Initialize Sequelize const db = new Sequelize({ dialect: 'postgres', @@ -28,4 +33,4 @@ async function testConnection() { } // Export Sequelize instance and models -export { logger, db, testConnection }; +export { logger, db, testConnection, Scope };