-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaea83e
commit a6fec39
Showing
8 changed files
with
139 additions
and
14 deletions.
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
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,55 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { ChatService } from '../chat/chat.service'; | ||
|
||
@Injectable() | ||
export class ChatGuard implements CanActivate { | ||
constructor( | ||
private readonly chatService: ChatService, // Inject ChatService to fetch chat details | ||
private readonly jwtService: JwtService, // JWT Service to verify tokens | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const gqlContext = GqlExecutionContext.create(context); | ||
const request = gqlContext.getContext().req; | ||
|
||
// Extract the authorization header | ||
const authHeader = request.headers.authorization; | ||
if (!authHeader || !authHeader.startsWith('Bearer ')) { | ||
throw new UnauthorizedException('Authorization token is missing'); | ||
} | ||
|
||
// Decode the token to get user information | ||
const token = authHeader.split(' ')[1]; | ||
let user: any; | ||
try { | ||
user = this.jwtService.verify(token); | ||
} catch (error) { | ||
throw new UnauthorizedException('Invalid token'); | ||
} | ||
|
||
// Extract chatId from the request arguments | ||
const args = gqlContext.getArgs(); | ||
const { chatId } = args; | ||
|
||
// check if the user is part of the chat | ||
const chat = await this.chatService.getChatDetails(chatId); | ||
if (!chat) { | ||
throw new UnauthorizedException('Chat not found'); | ||
} | ||
|
||
if (chat.user.id !== user.userId) { | ||
throw new UnauthorizedException( | ||
'User is not authorized to access this chat', | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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