Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test alfredo gonzalez #18

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

TYPE= service_account
PROJECT_ID= your_project_id
PRIVATE_KEY_ID= your_private_key_id
PRIVATE_KEY= your_private_key
CLIENT_EMAIL= your_client_email
CLIENT_ID= your_client_id
AUTH_URI= https://accounts.google.com/o/oauth2/auth
TOKEN_URI= https://oauth2.googleapis.com/token
AUTH_PROVIDER_X509_CERT_URL= https://www.googleapis.com/oauth2/v1/certs
CLIENT_X509_CERT_URL= your_client_cert_url
UNIVERSAL_DOMAIN= googleapis.com
FIREBASE_STORAGE_BUCKET= your_project_id.appspot.com

##Mock data for auth

Authorization Bearer mock_token
Content-Type multipart/form-data
Example of query link
localhost:3000/image?tenant=example_tenant&userId=123456&folderName=uploads&page=1&limit=10

const mockedPayload: JwtPayload = {
user_id: '123456',
role: 'admin',
tenant: 'example_tenant',
};
Binary file modified .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ You should use the following tech stack during this project:

3. **.env variables**
- You should update this point to include env names needed.
- The env names needed are added in the .env.template.

4. **Start the Service**
```bash
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@
"@nestjs/common": "^10.4.4",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.4.4",
"@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.4.4",
"@nestjs/swagger": "^7.4.2",
"@nestjs/typeorm": "^10.0.2",
"@types/multer": "^1.4.12",
"axios": "^1.7.7",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"firebase-admin": "^12.6.0",
"multer": "^1.4.5-lts.1",
"pg": "^8.13.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"sharp": "^0.33.5",
"typeorm": "^0.3.20",
"uuid": "^10.0.0"
},
Expand All @@ -42,6 +47,7 @@
"@types/express": "^4.17.21",
"@types/jest": "^29.5.13",
"@types/node": "^20.16.11",
"@types/sharp": "^0.32.0",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
Expand Down
22 changes: 0 additions & 22 deletions src/app.controller.spec.ts

This file was deleted.

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

This file was deleted.

17 changes: 12 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { ImageModule } from './image/image.module';
import { FirebaseModule } from './firebase/firebase.module';
import { AuthModule } from './auth/auth.module';

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
imports: [
ConfigModule.forRoot({ cache: true, envFilePath: '.env' }),
ImageModule,
FirebaseModule,
AuthModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
8 changes: 0 additions & 8 deletions src/app.service.ts

This file was deleted.

34 changes: 34 additions & 0 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { AuthService } from './auth.service';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post()
create(@Body() createAuthDto: CreateAuthDto) {
return this.authService.create(createAuthDto);
}

@Get()
findAll() {
return this.authService.findAll();
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.authService.findOne(+id);
}

@Patch(':id')
update(@Param('id') id: string, @Body() updateAuthDto: UpdateAuthDto) {
return this.authService.update(+id, updateAuthDto);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.authService.remove(+id);
}
}
11 changes: 11 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UserAuthGuard } from './guards/user-auth.guard';

@Module({
controllers: [AuthController],
providers: [AuthService, UserAuthGuard],
exports: [UserAuthGuard],
})
export class AuthModule {}
26 changes: 26 additions & 0 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';

@Injectable()
export class AuthService {
create(createAuthDto: CreateAuthDto) {
return 'This action adds a new auth';
}

findAll() {
return `This action returns all auth`;
}

findOne(id: number) {
return `This action returns a #${id} auth`;
}

update(id: number, updateAuthDto: UpdateAuthDto) {
return `This action updates a #${id} auth`;
}

remove(id: number) {
return `This action removes a #${id} auth`;
}
}
1 change: 1 addition & 0 deletions src/auth/dto/create-auth.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateAuthDto {}
4 changes: 4 additions & 0 deletions src/auth/dto/update-auth.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateAuthDto } from './create-auth.dto';

export class UpdateAuthDto extends PartialType(CreateAuthDto) {}
1 change: 1 addition & 0 deletions src/auth/entities/auth.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class Auth {}
51 changes: 51 additions & 0 deletions src/auth/guards/user-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
Injectable,
CanActivate,
ExecutionContext,
UnauthorizedException,
} from '@nestjs/common';
import { Observable } from 'rxjs';

export interface JwtPayload {
user_id: string;
role: string;
tenant: string;
}

@Injectable()
export class UserAuthGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest();
const token = this.extractTokenFromHeader(request);

if (!token) {
throw new UnauthorizedException('Missing JWT token');
}

try {
const payload = this.decodeToken(token);
request.user = payload;
return true;
} catch (error) {
throw new UnauthorizedException('Invalid JWT token');
}
}

private extractTokenFromHeader(request: any): string | undefined {
const [type, token] = request.headers.authorization?.split(' ') ?? [];
return type === 'Bearer' ? token : undefined;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
private decodeToken(token: string): JwtPayload {
const mockedPayload: JwtPayload = {
user_id: '123456',
role: 'admin',
tenant: 'example_tenant',
};

return mockedPayload;
}
}
9 changes: 9 additions & 0 deletions src/auth/types/express.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { JwtPayload } from '../guards/user-auth.guard';

declare global {
namespace Express {
interface Request {
user?: JwtPayload;
}
}
}
1 change: 1 addition & 0 deletions src/firebase/dto/create-firebase.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateFirebaseDto {}
4 changes: 4 additions & 0 deletions src/firebase/dto/update-firebase.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateFirebaseDto } from './create-firebase.dto';

export class UpdateFirebaseDto extends PartialType(CreateFirebaseDto) {}
1 change: 1 addition & 0 deletions src/firebase/entities/firebase.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class Firebase {}
34 changes: 34 additions & 0 deletions src/firebase/firebase.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { FirebaseService } from './firebase.service';
import { CreateFirebaseDto } from './dto/create-firebase.dto';
import { UpdateFirebaseDto } from './dto/update-firebase.dto';

@Controller('firebase')
export class FirebaseController {
constructor(private readonly firebaseService: FirebaseService) {}

@Post()
create(@Body() createFirebaseDto: CreateFirebaseDto) {
return this.firebaseService.create(createFirebaseDto);
}

@Get()
findAll() {
return this.firebaseService.findAll();
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.firebaseService.findOne(+id);
}

@Patch(':id')
update(@Param('id') id: string, @Body() updateFirebaseDto: UpdateFirebaseDto) {
return this.firebaseService.update(+id, updateFirebaseDto);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.firebaseService.remove(+id);
}
}
50 changes: 50 additions & 0 deletions src/firebase/firebase.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { FirebaseService } from './firebase.service';
import { FirebaseController } from './firebase.controller';
import * as admin from 'firebase-admin';

@Module({
imports: [ConfigModule],
controllers: [FirebaseController],
providers: [
FirebaseService,
{
provide: 'FIREBASE_APP',
useFactory: (configService: ConfigService) => {
const firebaseConfig = {
type: configService.get('TYPE'),
projectId: configService.get('PROJECT_ID'),
privateKeyId: configService.get('PRIVATE_KEY_ID'),
privateKey: configService.get('PRIVATE_KEY')?.replace(/\\n/g, '\n'),
clientEmail: configService.get('CLIENT_EMAIL'),
clientId: configService.get('CLIENT_ID'),
authUri: configService.get('AUTH_URI'),
tokenUri: configService.get('TOKEN_URI'),
authProviderX509CertUrl: configService.get(
'AUTH_PROVIDER_X509_CERT_URL',
),
clientX509CertUrl: configService.get('CLIENT_X509_CERT_URL'),
};

const storageBucket = configService.get('FIREBASE_STORAGE_BUCKET');

if (!storageBucket) {
throw new Error(
'FIREBASE_STORAGE_BUCKET is not defined in the environment variables',
);
}

return admin.initializeApp({
credential: admin.credential.cert(
firebaseConfig as admin.ServiceAccount,
),
storageBucket: storageBucket,
});
},
inject: [ConfigService],
},
],
exports: [FirebaseService, 'FIREBASE_APP'],
})
export class FirebaseModule {}
26 changes: 26 additions & 0 deletions src/firebase/firebase.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { CreateFirebaseDto } from './dto/create-firebase.dto';
import { UpdateFirebaseDto } from './dto/update-firebase.dto';

@Injectable()
export class FirebaseService {
create(createFirebaseDto: CreateFirebaseDto) {
return 'This action adds a new firebase';
}

findAll() {
return `This action returns all firebase`;
}

findOne(id: number) {
return `This action returns a #${id} firebase`;
}

update(id: number, updateFirebaseDto: UpdateFirebaseDto) {
return `This action updates a #${id} firebase`;
}

remove(id: number) {
return `This action removes a #${id} firebase`;
}
}
Loading