Skip to content

Commit

Permalink
Merge pull request #1 from CL-Sarthak/API-development
Browse files Browse the repository at this point in the history
Chat Module
  • Loading branch information
Aakash-bhardwaj-cirruslabs authored Jun 15, 2024
2 parents ae82c32 + 15b7b5f commit 8702d56
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ Enhance your Nx experience by installing [Nx Console](https://nx.dev/nx-console)
provides an interactive UI to view your projects, run tasks, generate code, and more! Available for VSCode, IntelliJ and
comes with a LSP for Vim users.

# Install libraries

step 1
In terminal run:

npm install
nmp outdated

step 2
check and compare for the current version and wanted version
install the different ones manually. e.g.
npm install @nrwl/react@19.2.3
(19.2.3 is the wanted version)

## Start the application

Run `npx nx serve humanoid-ai-backend` to start the development server. Happy coding!
Expand Down
6 changes: 5 additions & 1 deletion apps/humanoid-ai-backend/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Module } from '@nestjs/common';
import { AuthModule } from '../auth/auth.module';
import { UsersModule } from '../users/users.module';
import { ChatModule } from '../chat/chat.module';


import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [],
imports: [AuthModule, UsersModule, ChatModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
28 changes: 28 additions & 0 deletions apps/humanoid-ai-backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// src/auth/auth.controller.ts
import { Controller, Post, Body, UseGuards, Request, Get } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LocalAuthGuard } from './local-auth.guard';
import { LoginDto } from './dto/login.dto';
import { User } from './user.decorator';

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

@UseGuards(LocalAuthGuard)
@Post('login')
async login(@Body() loginDto: LoginDto, @Request() req) {
return req.user;
}

@Post('logout')
async logout(@Request() req) {
req.logout();
return { message: 'Logged out successfully' };
}

@Get('profile')
async getProfile(@User() user) {
return user;
}
}
14 changes: 14 additions & 0 deletions apps/humanoid-ai-backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// src/auth/auth.module.ts
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LocalStrategy } from './local.strategy';
import { PassportModule } from '@nestjs/passport';
import { UsersModule } from '../users/users.module';
import { AuthController } from './auth.controller';

@Module({
imports: [UsersModule, PassportModule],
providers: [AuthService, LocalStrategy],
controllers: [AuthController],
})
export class AuthModule {}
17 changes: 17 additions & 0 deletions apps/humanoid-ai-backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// src/auth/auth.service.ts
import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';

@Injectable()
export class AuthService {
constructor(private usersService: UsersService) {}

async validateUser(username: string, password: string): Promise<any> {
const user = await this.usersService.findOne(username);
if (user && user.password === password) {
const { password, ...result } = user;
return result;
}
return null;
}
}
10 changes: 10 additions & 0 deletions apps/humanoid-ai-backend/src/auth/dto/login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// src/auth/dto/login.dto.ts
import { IsString } from 'class-validator';

export class LoginDto {
@IsString()
readonly username: string;

@IsString()
readonly password: string;
}
13 changes: 13 additions & 0 deletions apps/humanoid-ai-backend/src/auth/local-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// src/auth/local-auth.guard.ts
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class LocalAuthGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest();
return request.isAuthenticated();
}
}
20 changes: 20 additions & 0 deletions apps/humanoid-ai-backend/src/auth/local.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// src/auth/local.strategy.ts
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}

async validate(username: string, password: string): Promise<any> {
const user = await this.authService.validateUser(username, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
9 changes: 9 additions & 0 deletions apps/humanoid-ai-backend/src/auth/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// src/auth/user.decorator.ts
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const User = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return request.user;
},
);
19 changes: 19 additions & 0 deletions apps/humanoid-ai-backend/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// src/chat/chat.controller.ts
import { Controller, Post, Body, Get } from '@nestjs/common';
import { ChatService } from './chat.service';
import { SendMessageDto } from './dto/send-message.dto';

@Controller('chat')
export class ChatController {
constructor(private readonly chatService: ChatService) {}

@Post('send')
async sendMessage(@Body() sendMessageDto: SendMessageDto): Promise<string> {
return this.chatService.sendMessage(sendMessageDto.message);
}

@Get('receive')
async receiveMessage(): Promise<string> {
return this.chatService.receiveMessage();
}
}
10 changes: 10 additions & 0 deletions apps/humanoid-ai-backend/src/chat/chat.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// src/chat/chat.module.ts
import { Module } from '@nestjs/common';
import { ChatService } from './chat.service';
import { ChatController } from './chat.controller';

@Module({
controllers: [ChatController],
providers: [ChatService],
})
export class ChatModule {}
15 changes: 15 additions & 0 deletions apps/humanoid-ai-backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// src/chat/chat.service.ts
import { Injectable } from '@nestjs/common';

@Injectable()
export class ChatService {
async sendMessage(message: string): Promise<string> {
// Add logic to process the message
return `Echo: ${message}`;
}

async receiveMessage(): Promise<string> {
// Add logic to receive the message
return 'Received your message';
}
}
7 changes: 7 additions & 0 deletions apps/humanoid-ai-backend/src/chat/dto/send-message.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// src/chat/dto/send-message.dto.ts
import { IsString } from 'class-validator';

export class SendMessageDto {
@IsString()
readonly message: string;
}
11 changes: 11 additions & 0 deletions apps/humanoid-ai-backend/src/users/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// src/users/dto/create-user.dto.ts
import { IsString } from 'class-validator';

export class CreateUserDto {
@IsString()
readonly username: string;

@IsString()
readonly password: string;
}

19 changes: 19 additions & 0 deletions apps/humanoid-ai-backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// src/users/users.controller.ts
import { Controller, Post, Body, Get, Param } from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';

@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}

@Post('create')
async createUser(@Body() createUserDto: CreateUserDto): Promise<any> {
return this.usersService.createUser(createUserDto.username, createUserDto.password);
}

@Get(':username')
async findOne(@Param('username') username: string): Promise<any> {
return this.usersService.findOne(username);
}
}
11 changes: 11 additions & 0 deletions apps/humanoid-ai-backend/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// src/users/users.module.ts
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';

@Module({
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
20 changes: 20 additions & 0 deletions apps/humanoid-ai-backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// src/users/users.service.ts
import { Injectable } from '@nestjs/common';

@Injectable()
export class UsersService {
private readonly users = [
{ id: 1, username: 'john', password: 'changeme' },
{ id: 2, username: 'maria', password: 'guess' },
];

async findOne(username: string): Promise<any | undefined> {
return this.users.find(user => user.username === username);
}

async createUser(username: string, password: string): Promise<any> {
const newUser = { id: this.users.length + 1, username, password };
this.users.push(newUser);
return newUser;
}
}
Loading

0 comments on commit 8702d56

Please sign in to comment.