-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/added GraphQl Quries in server to verify User Role and Its au…
…thorization So that it can be helpFul for client side. (#3094) * added graphql Queries named VerifyRole Response * modified verifyRole.ts * Update src/resolvers/Query/verifyRole.ts line-30 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update src/typeDefs/types.ts line -86 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update schema.graphql line - 1618 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update src/resolvers/Query/verifyRole.ts line - 37 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update src/resolvers/Query/verifyRole.ts line-44 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update src/resolvers/Query/verifyRole.ts line-52 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * suggestion from ai and fixed the failing test * fixed build issue * added test cases for verifyRole Query * added test cases for falling line * fixed failed test cases --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
8ab6bdd
commit 595feaa
Showing
8 changed files
with
406 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,94 @@ | ||
import type { QueryResolvers } from "../../types/generatedGraphQLTypes"; | ||
import jwt from "jsonwebtoken"; | ||
import type { InterfaceAppUserProfile } from "../../models/AppUserProfile"; | ||
import { AppUserProfile } from "../../models/AppUserProfile"; | ||
import type { Request } from "express"; | ||
import type { InterfaceJwtTokenPayload } from "../../utilities"; | ||
/** | ||
* This query verifies the user's role based on the provided JWT token. | ||
* @param _ - Unused parent parameter (as this is a root-level query). | ||
* @param __ - Unused arguments parameter (as this query does not require input arguments). | ||
* @param context - Contains the Express `Request` object, which includes the Authorization header. | ||
* @returns An object containing: | ||
* - `role`: The user's role, either "admin" or "user". | ||
* - `isAuthorized`: A boolean indicating whether the token is valid. | ||
* | ||
* @remarks | ||
* - Extracts the token from the `Authorization` header. | ||
* - Decodes and verifies the token using `jwt.verify()`. | ||
* - Fetches the user profile from the database using `userId` from the decoded token. | ||
* - Determines the role (`admin` if `isSuperAdmin` is `true`, otherwise `user`). | ||
* - Returns the role and authorization status. | ||
*/ | ||
|
||
export const verifyRole: QueryResolvers["verifyRole"] = async ( | ||
_: unknown, | ||
args: unknown, | ||
{ req }: { req: Request }, | ||
) => { | ||
try { | ||
// Extract token from the Authorization header | ||
const authHeader = req.headers.authorization; | ||
if (!authHeader) { | ||
return { role: "", isAuthorized: false }; | ||
} | ||
const token = authHeader.startsWith("Bearer ") | ||
? authHeader.split(" ")[1] | ||
: authHeader; | ||
if (!token) { | ||
return { role: "", isAuthorized: false }; | ||
} | ||
// Verify token | ||
if (!process.env.ACCESS_TOKEN_SECRET) { | ||
throw new Error("ACCESS_TOKEN_SECRET is not defined"); | ||
} | ||
const decoded = jwt.verify( | ||
token, | ||
process.env.ACCESS_TOKEN_SECRET as string, | ||
); | ||
const decodedToken = decoded as InterfaceJwtTokenPayload; | ||
if (!decodedToken.userId) { | ||
throw new Error("Invalid token: userId is missing"); | ||
} | ||
const appUserProfile: InterfaceAppUserProfile | null = | ||
await AppUserProfile.findOne({ | ||
userId: decodedToken.userId, | ||
appLanguageCode: process.env.DEFAULT_LANGUAGE_CODE || "en", | ||
tokenVersion: process.env.TOKEN_VERSION | ||
? parseInt(process.env.TOKEN_VERSION) | ||
: 0, | ||
}); | ||
if (appUserProfile == null || appUserProfile == undefined) { | ||
throw new Error("User profile not found"); | ||
} | ||
|
||
let role = "user"; // Default role | ||
if (appUserProfile) { | ||
if (appUserProfile.isSuperAdmin) { | ||
role = "superAdmin"; | ||
} else if ( | ||
appUserProfile.adminFor && | ||
appUserProfile.adminFor.length > 0 | ||
) { | ||
role = "admin"; | ||
} | ||
} | ||
return { | ||
role: role, | ||
isAuthorized: true, | ||
}; | ||
} catch (error) { | ||
// Log sanitized error for debugging | ||
console.error( | ||
"Token verification failed:", | ||
error instanceof Error ? error.message : "Unknown error", | ||
); | ||
// Return specific error status | ||
const isJwtError = error instanceof jwt.JsonWebTokenError; | ||
return { | ||
role: "", | ||
isAuthorized: false, | ||
error: isJwtError ? "Invalid token" : "Authentication failed", | ||
}; | ||
} | ||
}; |
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
Oops, something went wrong.