Skip to content

Commit

Permalink
fix session and update home page projects
Browse files Browse the repository at this point in the history
  • Loading branch information
younes200 committed Oct 3, 2023
1 parent 5054de5 commit 8547714
Show file tree
Hide file tree
Showing 29 changed files with 392 additions and 268 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions apps/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"start": "node --use-strict dist/index.js"
},
"dependencies": {
"@adminjs/express": "6.0.0",
"@adminjs/express": "6.0.1",
"@adminjs/import-export": "^3.0.0",
"@adminjs/prisma": "5.0.1",
"@adminjs/themes": "^1.0.1",
Expand All @@ -27,7 +27,7 @@
"express": "^4.18.2",
"express-formidable": "^1.2.0",
"express-session": "^1.17.3",
"redis": "^4.6.7"
"redis": "^4.6.10"
},
"devDependencies": {
"@celluloid/config": "*",
Expand Down
1 change: 1 addition & 0 deletions apps/admin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"jsx": "react",
"module": "NodeNext",
Expand Down
8 changes: 4 additions & 4 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
"dependencies": {
"@celluloid/prisma": "*",
"@celluloid/trpc": "*",
"@trpc/server": "^10.38.4",
"@trpc/server": "^10.38.5",
"bcryptjs": "^2.4.3",
"change-case": "^4.1.2",
"connect-redis": "^7.1.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.17.1",
"express": "^4.18.2",
"express-session": "^1.17.3",
"lodash": "^4.17.21",
"redis": "^4.6.9",
"redis": "^4.6.10",
"swagger-ui-express": "^5.0.0",
"trpc-openapi": "^1.2.0",
"uuid": "^9.0.1",
Expand All @@ -32,7 +32,7 @@
"@types/express": "^4.17.17",
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^9.0.4",
"esbuild": "^0.19.3",
"esbuild": "^0.19.4",
"start-server-and-test": "^2.0.0",
"tsx": "^3.12.10",
"wait-port": "^1.0.4"
Expand Down
46 changes: 5 additions & 41 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { prisma } from "@celluloid/prisma"
import { appRouter, createContext } from '@celluloid/trpc';
import "./passport";

import { appRouter, createRPCContext } from '@celluloid/trpc';
import * as trpcExpress from '@trpc/server/adapters/express';
import bcrypt from 'bcryptjs';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import express from 'express';
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
import swaggerUi from 'swagger-ui-express';
import { createOpenApiExpressMiddleware } from 'trpc-openapi';

import { openApiDocument } from './openapi';
import { createSession } from './session';


const trpcApiEndpoint = '/trpc'


Expand All @@ -37,40 +35,6 @@ async function main() {
app.use(createSession());
app.use(passport.authenticate("session"));


passport.serializeUser((user: any, done) => {
done(null, user.id)
});

passport.deserializeUser(async (id: string, done) => {
const user = await prisma.user.findUnique({ where: { id } })
if (user) {
return done(null, user);
} else {
console.error(
`Deserialize user failed: user with id` + ` ${id} does not exist`
);
return done(new Error("InvalidUser"));
}
});

passport.use(
new LocalStrategy(async (username: string, password: string, done) => {
const user = await prisma.user.findUnique({ where: { username: username } })
if (!user) {
return done(new Error("InvalidUser"));
}
if (!bcrypt.compareSync(password, user.password)) {
return done(new Error("InvalidUser"));
}
if (!user.confirmed && user.role !== "Student") {
return done(new Error("UserNotConfirmed"));
}
return done(null, user);

}),
);

app.use((req, _res, next) => {
// request logger
console.log('⬅️ ', req.method, req.path, req.body ?? req.query);
Expand All @@ -81,12 +45,12 @@ async function main() {
trpcApiEndpoint,
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext,
createContext: createRPCContext,
}),
);

// Handle incoming OpenAPI requests
app.use('/api', createOpenApiExpressMiddleware({ router: appRouter, createContext }));
app.use('/api', createOpenApiExpressMiddleware({ router: appRouter, createContext: createRPCContext }));


// Serve Swagger UI with our OpenAPI schema
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { generateOpenApiDocument } from 'trpc-openapi';

// Generate OpenAPI schema document
export const openApiDocument = generateOpenApiDocument(appRouter, {
title: 'Celluloid CRUD API',
title: 'Celluloid REST API',
description: 'OpenAPI compliant REST API built using tRPC with Express',
version: '1.0.0',
baseUrl: 'http://localhost:2021/api',
docsUrl: 'https://github.com/jlalmes/trpc-openapi',
docsUrl: 'https://github.com/celluloid-camp/celluloid',
tags: ['auth', 'users', 'projects'],
});
75 changes: 75 additions & 0 deletions apps/backend/src/passport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

import { prisma } from "@celluloid/prisma"
import bcrypt from 'bcryptjs';
import passport from 'passport';
import {
Strategy as LocalStrategy,
} from "passport-local";

export enum SigninStrategy {
LOGIN = "login",
TEACHER_SIGNUP = "teacher-signup",
STUDENT_SIGNUP = "student-signup",
}


passport.serializeUser((user: any, done) => {
done(null, user.id)
});

passport.deserializeUser(async (id: string, done) => {
const user = await prisma.user.findUnique({ where: { id } })
if (user) {
return done(null, user);
} else {
console.error(
`Deserialize user failed: user with id` + ` ${id} does not exist`
);
return done(new Error("InvalidUser"));
}
});

passport.use(
new LocalStrategy(async (username: string, password: string, done) => {
const user = await prisma.user.findUnique({ where: { username: username } })
if (!user) {
return done(new Error("InvalidUser"));
}
if (!bcrypt.compareSync(password, user.password)) {
return done(new Error("InvalidUser"));
}
if (!user.confirmed && user.role !== "Student") {
return done(new Error("UserNotConfirmed"));
}
return done(null, user);

}),
);


const loginStrategy = new LocalStrategy(
{ usernameField: "login" },
async (login, password, done) => {

const user = await prisma.user.findUnique({
where: {
OR: [{ email: login }, { username: login, }]
}
});

if (!user) {
return Promise.resolve(done(new Error("InvalidUser")));
}
if (!bcrypt.compareSync(password, user.password)) {
console.error(`Login failed for user ${user.username}: incorrect password`);
return Promise.resolve(done(new Error("InvalidUser")));
}
if (!user.confirmed && user.role !== "Student") {
console.error(`Login failed: ${user.username} is not confirmed`);
return Promise.resolve(done(new Error("UserNotConfirmed")));
}
return Promise.resolve(done(null, user));
}
);

passport.use(SigninStrategy.LOGIN, loginStrategy);
5 changes: 3 additions & 2 deletions apps/backend/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import session from "express-session"
import { createClient } from "redis"




export function createSession() {

// Initialize client.
Expand All @@ -20,6 +18,9 @@ export function createSession() {

return session({
store: redisStore,
name: process.env.CELLULOID_COOKIE_NAME
? process.env.CELLULOID_COOKIE_NAME
: undefined,
cookie: {
domain: process.env.CELLULOID_COOKIE_DOMAIN
? process.env.CELLULOID_COOKIE_DOMAIN
Expand Down
9 changes: 9 additions & 0 deletions apps/backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "esnext",
"module": "esnext",
Expand All @@ -13,5 +14,13 @@
],
"exclude": [
"node_modules"
],
"references": [
{
"path": "../../packages/trpc"
},
{
"path": "../../packages/prisma"
}
]
}
10 changes: 5 additions & 5 deletions apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"private": true,
"scripts": {
"dev": "vite --open",
"dev": "vite --open ",
"build": "vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
Expand All @@ -25,10 +25,10 @@
"@mui/material": "^5.12.0",
"@mui/styles": "^5.12.0",
"@reduxjs/toolkit": "^1.8.6",
"@tanstack/react-query": "^4.35.3",
"@trpc/client": "^10.38.4",
"@trpc/react-query": "^10.38.4",
"@trpc/server": "^10.38.4",
"@tanstack/react-query": "^4.35.7",
"@trpc/client": "^10.38.5",
"@trpc/react-query": "^10.38.5",
"@trpc/server": "^10.38.5",
"@types/linkify-urls": "^3.1.1",
"autosuggest-highlight": "^3.1.1",
"axios": "^1.3.4",
Expand Down
18 changes: 12 additions & 6 deletions apps/frontend/src/components/home/ProjectGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useDidUpdate } from "rooks";
import { StyledTitle } from "~components/typography";
import { useMe, useProjects } from "~hooks/use-user";
import { isAdmin, isMember, isOwner } from "~utils/ProjectUtils";
import { ProjectList, trpc } from "~utils/trpc";

import ProjectThumbnail from "./ProjectThumbnail";

Expand All @@ -42,8 +43,10 @@ export const ProjectGrid: React.FC = () => {
}
}, 1000);

const { data: user } = useMe();
const { data: projects = [], error, isLoading } = useProjects(searchTerm);
const { data: user } = trpc.user.me.useQuery();
const { data, isFetching, error } = trpc.project.list.useQuery({
term: searchTerm,
});

const { t } = useTranslation();

Expand All @@ -53,12 +56,15 @@ export const ProjectGrid: React.FC = () => {

const sort = R.sortWith([R.descend(R.prop("publishedAt"))]);

const sorted = sort(projects) as ProjectGraphRecord[];
console.log(data);

const sorted = sort(data?.items || []) as ProjectList;

const userProjects = R.filter(
(project: ProjectGraphRecord) =>
!!user &&
(isOwner(project, user) || isMember(project, user) || isAdmin(user))
// (isOwner(project, user) || isMember(project, user) || isAdmin(user))
(isOwner(project, user) || isAdmin(user))
)(sorted);

const publicProjects = R.difference(sorted, userProjects);
Expand Down Expand Up @@ -109,7 +115,7 @@ export const ProjectGrid: React.FC = () => {
) : null}
</Paper>

{isLoading ? (
{isFetching ? (
<Box
mx={2}
my={10}
Expand Down Expand Up @@ -178,7 +184,7 @@ export const ProjectGrid: React.FC = () => {
</>
)}

{!isLoading && noProjects && (
{!isFetching && noProjects && (
<Fade in={noProjects} appear={true}>
<Typography
variant="h3"
Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/src/utils/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function isTRPCClientError(

type RouterOutput = inferRouterOutputs<AppRouter>;


export type UserMe = RouterOutput['user']['me'];

export type ProjectById = RouterOutput['project']['byId'];

export type ProjectList = RouterOutput['project']['list'];
8 changes: 6 additions & 2 deletions apps/frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,18 @@
},
"include": [
"src/**/*",
"src/**/*.json"
"src/**/*.json",
"vite.config.ts"
],
"exclude": [
"node_modules"
],
"references": [
{
"path": "./tsconfig.node.json"
}
},
{
"path": "../../packages/trpc"
},
]
}
1 change: 1 addition & 0 deletions apps/frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
],
server: {
port: 3000,
host: "localhost",
proxy: {
"/api": "http://localhost:3001",
"/trpc": "http://localhost:2021",
Expand Down
Loading

0 comments on commit 8547714

Please sign in to comment.