ethan0905 / ft_transcendence

Wiki with step-by-step explained strategy on how to: create app (front+back+infra), 42auth, 2FA and more..
2 stars 0 forks source link

Protection against SQL injection #34

Closed ethan0905 closed 1 year ago

ethan0905 commented 1 year ago

Working on securing the app from SQL injection.

Here is a quick example : We handle the security inside the backend/src/prisma/prisma.service.ts :

import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService {
  prisma: PrismaClient;

  constructor() {
    this.prisma = new PrismaClient();
  }

  async onModuleDestroy() {
    await this.prisma.$disconnect();
  }
}

And we call the @Injectable decorator before any class we export:

import { Injectable } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { User } from '@prisma/client';

@Injectable() // here you must add your @Injectable decorator
export class UserService {
  constructor(private prisma: PrismaService) {}

  async getUserById(id: number): Promise<User> {
  [...]
  }

  async createUser(name: string, email: string, password: string): Promise<User> {
   [...]
  }
}