Skip to content

Commit

Permalink
start work on shared types
Browse files Browse the repository at this point in the history
  • Loading branch information
js0mmer committed Oct 11, 2024
1 parent 4dbcab2 commit 20c6187
Show file tree
Hide file tree
Showing 13 changed files with 546 additions and 177 deletions.
10 changes: 6 additions & 4 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "peterportal-core-api",
"version": "1.0.0",
"main": "dist/app.js",
"main": "src/app.ts",
"type": "module",
"scripts": {
"start": "node dist/app.js",
"dev": "tsc-watch --onSuccess \"pnpm start\"",
"dev": "nodemon --exec tsx ./src/app.ts",
"build": "tsc",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0 && tsc --noEmit"
},
Expand All @@ -19,11 +19,13 @@
"express-session": "^1.18.0",
"mongoose": "^8.3.3",
"morgan": "^1.10.0",
"nodemon": "^3.1.7",
"passport": "^0.7.0",
"passport-google-oauth": "^2.0.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@peterportal/types": "workspace:*",
"@types/connect-mongodb-session": "^2.4.7",
"@types/cookie-parser": "^1.4.7",
"@types/dotenv-flow": "^3.3.3",
Expand All @@ -36,7 +38,7 @@
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"tsc-watch": "^6.2.0",
"tsx": "^4.19.1",
"typescript": "^5.4.2"
}
}
3 changes: 2 additions & 1 deletion api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import authRouter from './controllers/auth';
import { SESSION_LENGTH } from './config/constants';
import { createContext } from './helpers/trpc';
import { appRouter } from './controllers';
import passportInit from './config/passport';

// instantiate app
const app = express();
Expand Down Expand Up @@ -68,7 +69,7 @@ app.use(
if (process.env.GOOGLE_CLIENT && process.env.GOOGLE_SECRET) {
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport');
passportInit();
} else {
console.log('GOOGLE_CLIENT and/or GOOGLE_SECRET env var(s) not defined! Google login will not be available.');
}
Expand Down
66 changes: 34 additions & 32 deletions api/src/config/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,39 @@ import { User } from 'express-session';
import passport from 'passport';
import { OAuth2Strategy as GoogleStrategy } from 'passport-google-oauth';

passport.serializeUser(function (user, done) {
done(null, user);
});
export default function passportInit() {
passport.serializeUser(function (user, done) {
done(null, user);
});

passport.deserializeUser(function (user: false | User | null | undefined, done) {
done(null, user);
});
passport.deserializeUser(function (user: false | User | null | undefined, done) {
done(null, user);
});

/**
* Configuration for Google Strategy
*/
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: process.env.PRODUCTION_DOMAIN + '/api/users/auth/google/callback',
},
function (accessToken, refreshToken, profile, done) {
let email = '';
// get the first registered email
if (profile.emails && profile.emails.length! > 0) {
email = profile.emails[0].value;
}
const userData = {
id: profile.id,
email: email,
name: profile.displayName,
picture: profile._json.picture,
};
done(null, userData);
},
),
);
/**
* Configuration for Google Strategy
*/
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: process.env.PRODUCTION_DOMAIN + '/api/users/auth/google/callback',
},
function (accessToken, refreshToken, profile, done) {
let email = '';
// get the first registered email
if (profile.emails && profile.emails.length! > 0) {
email = profile.emails[0].value;
}
const userData = {
id: profile.id,
email: email,
name: profile.displayName,
picture: profile._json.picture,
};
done(null, userData);
},
),
);
}
2 changes: 1 addition & 1 deletion api/src/controllers/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Report from '../models/report';
import { adminProcedure, publicProcedure, router } from '../helpers/trpc';
import { z } from 'zod';
import { reportData } from '../types/schemas';
import { reportData } from '@peterportal/types';

const reportsRouter = router({
/**
Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Preference from '../models/preference';
import { publicProcedure, router, userProcedure } from '../helpers/trpc';
import { userPreferences, UserPreferences } from '../types/schemas';
import { userPreferences, UserPreferences } from '@peterportal/types';

const usersRouter = router({
/**
Expand Down
2 changes: 1 addition & 1 deletion api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"module": "es2020",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"concurrently": "^8.2.2",
"concurrently": "^9.0.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"husky": "^8.0.0",
Expand Down
Loading

0 comments on commit 20c6187

Please sign in to comment.