Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
5 changes: 5 additions & 0 deletions .idea/.gitignore

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

49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
# VideoStramApp
# VideoStreamApp

Full-stack [app tutorial](https://blog.logrocket.com/full-stack-app-tutorial-nestjs-react/) with NestJS and React

[![MIT License](https://img.shields.io/apm/l/atomic-design-ui.svg?)](https://github.com/tterb/atomic-design-ui/blob/master/LICENSEs)

## Features

- NestJS + Mongoose
- MongoDB
- React|Vue as frontend

## Run Locally

Clone the project

```bash
git clone https://github.com/zolotarev/VideoStreamApp.git
```

Go to the project directory

```bash
cd VideoStreamApp
```

Install dependencies

```bash
(cd backend && yarn install) & (cd frontend && yarn install)
```

Start for development

```bash
(cd backend && yarn && yarn dev) & (cd frontend && yarn && yarn dev)
```

Start for production

```bash
(cd frontend && yarn build)
(cd backend && yarn build && yarn start)
```

## License

[MIT](https://choosealicense.com/licenses/mit/)
2 changes: 2 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=3002
MONGO=mongodb://localhost:27017/Stream
3 changes: 2 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/extensions.json
/public/
45 changes: 23 additions & 22 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,45 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/common": "^8.2.6",
"@nestjs/core": "^8.2.6",
"@nestjs/platform-express": "^8.2.6",
"@nestjs/serve-static": "^2.2.2",
"bcrypt": "^5.0.1",
"dotenv": "^16.0.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0"
"rxjs": "^7.5.3"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
"@nestjs/cli": "^8.2.0",
"@nestjs/jwt": "^8.0.0",
"@nestjs/mongoose": "^9.0.2",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@nestjs/schematics": "^8.0.5",
"@nestjs/testing": "^8.2.6",
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.13",
"@types/jest": "27.0.2",
"@types/jest": "27.4.0",
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0",
"@types/node": "^17.0.16",
"@types/supertest": "^2.0.11",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.2.5",
"mongoose": "^6.1.7",
"jest": "^27.5.1",
"mongoose": "^6.2.1",
"passport-jwt": "^4.0.0",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
"prettier": "^2.5.1",
"source-map-support": "^0.5.21",
"supertest": "^6.2.2",
"ts-jest": "^27.1.3",
"ts-loader": "^9.2.6",
"ts-node": "^10.5.0",
"tsconfig-paths": "^3.12.0",
"typescript": "^4.5.5"
},
"jest": {
"moduleFileExtensions": [
Expand Down
22 changes: 0 additions & 22 deletions backend/src/app.controller.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions backend/src/app.controller.ts

This file was deleted.

62 changes: 33 additions & 29 deletions backend/src/app.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@

import { JwtService } from '@nestjs/jwt';
import { Injectable, NestMiddleware, HttpException, HttpStatus } from '@nestjs/common';
import {
Injectable,
NestMiddleware,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { UserService } from './service/user.service';
interface UserRequest extends Request {
user: any
user: any;
}
@Injectable()
export class isAuthenticated implements NestMiddleware {
constructor(private readonly jwt: JwtService, private readonly userService: UserService) { }
async use(req: UserRequest, res: Response, next: NextFunction) {
try{

if (
req.headers.authorization &&
req.headers.authorization.startsWith('Bearer')
) {
const token = req.headers.authorization.split(' ')[1];
const decoded = await this.jwt.verify(token);
const user = await this.userService.getOne(decoded.email)
if (user) {
req.user = user
next()
} else {
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED)

}
} else {
throw new HttpException('No token found', HttpStatus.NOT_FOUND)

}
}catch {
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED)
}
constructor(
private readonly jwt: JwtService,
private readonly userService: UserService,
) {}
async use(req: UserRequest, res: Response, next: NextFunction) {
try {
if (
req.headers.authorization &&
req.headers.authorization.startsWith('Bearer')
) {
const token = req.headers.authorization.split(' ')[1];
const decoded = await this.jwt.verify(token);
const user = await this.userService.getOne(decoded.email);
if (user) {
req.user = user;
next();
} else {
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
}
} else {
throw new HttpException('No token found', HttpStatus.NOT_FOUND);
}
} catch {
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
}
}
}
}
38 changes: 18 additions & 20 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Module, RequestMethod, MiddlewareConsumer } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongooseModule } from '@nestjs/mongoose';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
Expand All @@ -16,42 +14,42 @@ import { UserController } from './controller/user.controller';
import { Video, VideoSchema } from './model/video.schema';
import { User, UserSchema } from './model/user.schema';
import { isAuthenticated } from './app.middleware';
import dotenv from 'dotenv';
dotenv.config();

@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost:27017/Stream'),
MongooseModule.forRoot(process.env.MONGO),
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]),
MongooseModule.forFeature([{ name: Video.name, schema: VideoSchema }]),

MulterModule.register({
storage: diskStorage({
destination: './public',
filename: (req, file, cb) => {
const ext = file.mimetype.split('/')[1];
cb(null, `${uuidv4()}-${Date.now()}.${ext}`);
},
})
}),
JwtModule.register({
MulterModule.register({
storage: diskStorage({
destination: './public',
filename: (req, file, cb) => {
const ext = file.mimetype.split('/')[1];
cb(null, `${uuidv4()}-${Date.now()}.${ext}`);
},
}),
}),
JwtModule.register({
secret,
signOptions: { expiresIn: '2h' },
}),
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'public'),
exclude: ['/api/v1*'],
}),
],

controllers: [AppController, VideoController, UserController],
providers: [AppService, VideoService, UserService],
})

controllers: [VideoController, UserController],
providers: [VideoService, UserService],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(isAuthenticated)
.exclude(
{ path: 'api/v1/video/:id', method: RequestMethod.GET }
)
.exclude({ path: 'api/v1/video/:id', method: RequestMethod.GET })
.forRoutes(VideoController);
}
}
8 changes: 0 additions & 8 deletions backend/src/app.service.ts

This file was deleted.

44 changes: 22 additions & 22 deletions backend/src/controller/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { Body, Controller, Delete, Get, HttpStatus, Param, Post, UploadedFiles, Put, Req, Res } from "@nestjs/common";
import { User } from "../model/user.schema";
import { UserService } from "../service/user.service";
import { Body, Controller, HttpStatus, Post, Res } from '@nestjs/common';
import { User } from '../model/user.schema';
import { UserService } from '../service/user.service';
import { JwtService } from '@nestjs/jwt';


@Controller('/api/v1/user')
@Controller('/user')
export class UserController {
constructor(private readonly userServerice: UserService,
private jwtService: JwtService
) { }
constructor(
private readonly userService: UserService,
private jwtService: JwtService,
) {}

@Post('/signup')
async Signup(@Res() response, @Body() user: User) {
console.log(user)
const newUSer = await this.userServerice.signup(user);
return response.status(HttpStatus.CREATED).json({
newUSer
})
}
@Post('/signup')
async SignUp(@Res() response, @Body() user: User) {
console.log(user);
const newUSer = await this.userService.signUp(user);
return response.status(HttpStatus.CREATED).json({
newUSer,
});
}

@Post('/signin')
async SignIn(@Res() response, @Body() user: User) {
const token = await this.userServerice.signin(user, this.jwtService);
return response.status(HttpStatus.OK).json(token)
}
}
@Post('/signin')
async SignIn(@Res() response, @Body() user: User) {
const token = await this.userService.signIn(user, this.jwtService);
return response.status(HttpStatus.OK).json(token);
}
}
Loading