-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CL-Sarthak/API-development
Chat Module
- Loading branch information
Showing
19 changed files
with
335 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.