Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node server/server.js",
"start": "cd server && npm run start",
"develop": "concurrently \"cd server && npm run watch\" \"cd client && npm run dev\"",
"install": "cd server && npm i && cd ../client && npm i",
"build": "concurrently \"cd server && npm run build\" \"cd client && npm run build\"",
Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"start": "node dist/server.js",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon index.ts"
"dev": "nodemon index.ts",
"watch": "nodemon dist/server.js"
},
"keywords": [],
"author": "",
Expand Down
19 changes: 11 additions & 8 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import express, { Request, Response } from 'express';
import cors from 'cors';
import express, { type Request, type Response } from 'express';

import dotenv from 'dotenv';
import { OpenAI } from 'openai';
// import fs from 'fs';
// import path from 'path';
import { ApolloServer } from 'apollo-server-express';

import path from 'path';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import typeDefs from './schemas/typeDefs';
import resolvers from './schemas/resolvers';
Expand Down Expand Up @@ -33,16 +33,19 @@ dotenv.config();
resolvers,
});

await server.start();
const startApolloServer = async () => {
await server.start();
await db;

app.use(express.static('public')); // serve generated mp3 file
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static('public')); // serve generated mp3 file

app.use('/graphql', expressMiddleware(server as any,
{
context: authenticateToken as any
}
));

// if we're in production, serve client/build as static assets
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(dirname, '../client/build')));
Expand Down
8 changes: 8 additions & 0 deletions server/src/types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare namespace Express {
interface Request {
user: {
_id: unknown;
username: string;
};
}
}
29 changes: 17 additions & 12 deletions server/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface JwtPayload {
}

export const authMiddleware = ({ req }: { req: any }) => {
const authHeader = req.headers.authorization;
const authHeader = req.headers.get('authorization');

if (authHeader) {
const token = authHeader.split(' ')[1];
Expand Down Expand Up @@ -42,20 +42,25 @@ export const authMiddleware = ({ req }: { req: any }) => {
return { user: null };
};

export const authenticateToken = (token: string) => {
const secretKey = process.env.JWT_SECRET_KEY || '';
export const authenticateToken = async ({ req }: { req: Request }) => {
const authHeader = req.headers.authorization;
let user = null;
console.log('AUTH HEADER', authHeader);

if (!token) {
throw new Error('No token provided');
}
if (authHeader) {
const token = authHeader.split(' ')[1];
console.log('TOKEN', token);
const secretKey = process.env.JWT_SECRET_KEY || '';

try {
const user = jwt.verify(token, secretKey) as JwtPayload;
return user;
} catch (err) {
console.error('Invalid token:', err);
throw new Error('Invalid or expired token');
try {
user = jwt.verify(token, secretKey) as JwtPayload;
console.log('USER', user);
} catch (err) {
console.error(err);
}
}

return { user };
};

export const signToken = (username: string, email: string, _id: unknown) => {
Expand Down
2 changes: 1 addition & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"@/*": ["src/*"] // Map the '@' alias to the 'src' directory
}
},
"include": ["src/**/*"],
"include": ["src/**/*", "src/server.ts"],
"exclude": ["node_modules"]
}
Loading