From a2a9bd9020063b4fa844db31c3916cb13934734e Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 13 Oct 2021 12:12:29 +0200 Subject: [PATCH] chore: Add static current user temporarily --- packages/backend/src/app.ts | 4 +++- packages/backend/src/helpers/authentication.ts | 18 ++++++++++++++++++ packages/backend/src/types/user.d.ts | 9 +++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 packages/backend/src/helpers/authentication.ts create mode 100644 packages/backend/src/types/user.d.ts diff --git a/packages/backend/src/app.ts b/packages/backend/src/app.ts index 134545b4..915a95e4 100644 --- a/packages/backend/src/app.ts +++ b/packages/backend/src/app.ts @@ -8,6 +8,7 @@ import logger from './helpers/logger'; import morgan from './helpers/morgan'; import errorHandler from './helpers/error-handler'; import './config/database'; +import authentication from './helpers/authentication'; const app = express(); const port = appConfig.port; @@ -15,7 +16,8 @@ const port = appConfig.port; app.use(morgan); app.use(express.json()); app.use(express.urlencoded({ extended: false })); -app.use(cors(corsOptions)) +app.use(cors(corsOptions)); +app.use(authentication); app.use('/graphql', graphQLInstance); // catch 404 and forward to error handler diff --git a/packages/backend/src/helpers/authentication.ts b/packages/backend/src/helpers/authentication.ts new file mode 100644 index 00000000..535c7dfc --- /dev/null +++ b/packages/backend/src/helpers/authentication.ts @@ -0,0 +1,18 @@ +import { Request, Response, NextFunction } from 'express'; +import User from '../models/user'; +import UserType from '../types/user'; + +interface RequestWithCurrentUser extends Request { + currentUser: UserType +} + +const authentication = async (req: RequestWithCurrentUser, _res: Response, next: NextFunction) => { + // We set authentication to use the sample user we created temporarily. + req.currentUser = await User.query().findOne({ + email: 'user@automatisch.com' + }) + + next() +} + +export default authentication; diff --git a/packages/backend/src/types/user.d.ts b/packages/backend/src/types/user.d.ts new file mode 100644 index 00000000..8bbc5bd8 --- /dev/null +++ b/packages/backend/src/types/user.d.ts @@ -0,0 +1,9 @@ +type User = { + id: number, + email: string, + password: string, + createdAt: string, + updatedAt: string +} + +export default User;