Skip to content

Comments

feat(add server manage session): misc fixes#2

Open
tukuna30 wants to merge 1 commit intomainfrom
add_node_server_manage_session
Open

feat(add server manage session): misc fixes#2
tukuna30 wants to merge 1 commit intomainfrom
add_node_server_manage_session

Conversation

@tukuna30
Copy link

@tukuna30 tukuna30 commented Jun 5, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a Node.js server using Express for user authentication with JWT tokens.
    • Added new scripts and dependencies for server setup in package.json.
  • Enhancements

    • Updated the application title and references from "Slash Admin" to "SkillRazr Admin" across multiple files.
    • Added expiresIn field to user token handling for better token management.
    • Updated API endpoint paths to relative paths for consistency.
  • Bug Fixes

    • Improved error handling in the registration form.
  • Configuration

    • Updated development API base URL to http://localhost:5000.
    • Disabled ESLint rule 'consistent-return'.
    • Set default Firebase project to "skillrazr-admin".
    • Updated VSCode settings to display language as English.
    • Configured Firebase hosting to serve content from dist directory.
  • Refactor

    • Removed unused imports and components in the login form.
    • Simplified state declaration in the Calendar component.
  • Documentation

    • Renamed the admin dashboard template in README.md to "SkillRazr Admin".

@coderabbitai
Copy link

coderabbitai bot commented Jun 5, 2024

Walkthrough

The recent updates encompass a variety of functional and configuration changes, including API endpoint adjustments, ESLint rule modifications, and environment variable updates. Significant changes include renaming the admin dashboard from "Slash Admin" to "SkillRazr Admin," adding new dependencies and scripts in package.json, and introducing a Node.js server for authentication. Additionally, Firebase configuration files and i18n settings have been updated, along with various UI text changes and the addition of new user token handling logic.

Changes

Files/Paths Change Summary
.env.development Updated VITE_APP_BASE_API value from /api to http://localhost:5000.
.eslintrc.cjs Disabled the ESLint rule 'consistent-return'.
.firebaserc, firebase.json Introduced Firebase configuration for project and hosting setup.
.vscode/settings.json Updated i18n-ally.displayLanguage from "zh_CN" to "en_US".
README.md, index.html, .../notice.tsx, .../nav.tsx, .../banner-card.tsx Renamed "Slash Admin" to "SkillRazr Admin".
package.json Added new scripts and dependencies for server setup and JWT handling.
src/App.tsx Updated the application title from "Slash Admin" to "SkillRazr Admin".
src/_mock/assets.js Added new exported constants: adminPermissions and testPermissions.
src/_mock/handlers/_user.js Updated user sign-in logic to reflect the use of a Node server.
src/api/apiClient.ts Added console logs for API base URL and Axios response.
src/api/services/userService.ts Changed endpoint paths in UserApi enum from absolute to relative paths.
src/pages/sys/login/Login.tsx Refined token handling logic and introduced accessToken and expiresIn variables.
src/pages/sys/login/LoginForm.tsx Removed references to TEST_USER, @/_mock/assets, and ProTag.
src/pages/sys/login/RegisterForm.tsx Added try-catch block for error handling in onFinish function.
src/pages/sys/others/calendar/index.tsx Modified setEvents function to only declare events state without setting it.
src/router/components/auth-guard.tsx Added expiresIn handling to check for token expiration and redirect if expired.
src/server.ts Introduced Node.js server using Express for handling user authentication with JWT tokens.
src/store/userStore.ts Added expiresIn field to the response object from login API call.
tsconfig.json Added ts-node configuration with esm and experimentalSpecifierResolution.
types/entity.ts Added expiresIn property to UserToken interface.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant ClientApp
    participant AuthServer

    User ->> ClientApp: Enters credentials
    ClientApp ->> AuthServer: POST /signin with credentials
    AuthServer ->> ClientApp: Returns accessToken, expiresIn
    ClientApp ->> ClientApp: Store accessToken, expiresIn
    ClientApp ->> User: Redirect to dashboard

    User ->> ClientApp: Navigates to protected route
    ClientApp ->> ClientApp: Check accessToken, expiresIn
    alt Token valid
        ClientApp ->> User: Grant access
    else Token expired
        ClientApp ->> User: Redirect to /login
    end
Loading

Poem

In the land of code, where changes bloom,
"SkillRazr Admin" now takes the room.
With tokens fresh and servers bright,
APIs dance in the morning light.
Firebase sings a hosting tune,
While ESLint rests, a lazy afternoon.
CodeRabbit smiles, the work well done,
A new era for SkillRazr's just begun. 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 10

Outside diff range and nitpick comments (5)
src/pages/sys/login/LoginForm.tsx (1)

Line range hint 64-64: Specify the button type explicitly to avoid unintended form submissions.

- <button className="!underline">{t('sys.login.forgetPassword')}</button>
+ <button type="button" className="!underline">{t('sys.login.forgetPassword')}</button>
src/layouts/dashboard/nav.tsx (2)

Line range hint 57-57: Remove unnecessary dependency from useEffect.

- }, [pathname, matches, collapsed, themeLayout]);
+ }, [pathname, matches, themeLayout]);

Line range hint 141-150: Add explicit button type to prevent form submission.

- <button onClick={toggleCollapsed} className="absolute right-0 top-7 z-50 hidden h-6 w-6 translate-x-1/2 cursor-pointer select-none rounded-full text-center !text-gray md:block" style={{ color: colorTextBase, borderColor: colorTextBase, fontSize: 16 }}>
+ <button type="button" onClick={toggleCollapsed} className="absolute right-0 top-7 z-50 hidden h-6 w-6 translate-x-1/2 cursor-pointer select-none rounded-full text-center !text-gray md:block" style={{ color: colorTextBase, borderColor: colorTextBase, fontSize: 16 }}>
src/pages/dashboard/workbench/banner-card.tsx (2)

Line range hint 75-76: Add a title element or aria-label to SVG for accessibility.

<svg viewBox="0 0 480 360" className="h-full w-full" xmlns="http://www.w3.org/2000/svg">
+ <title>SkillRazr Admin Banner</title>
  <defs>

Line range hint 50-55: Specify the button type to prevent unintended form submissions.

<button
+ type="button"
  className="font-mediumtext-black m-auto flex items-center justify-center rounded-md px-2 py-1 shadow-none md:m-0"
  style={{ backgroundColor: themeToken.colorPrimary, color: '#fff' }}
  onClick={() => window.open('https://discord.gg/fXemAXVNDa')}
>
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aa65f61 and 7e9c787.

Files ignored due to path filters (2)
  • package-lock.json is excluded by !**/package-lock.json
  • src/assets/images/skillrazr.jpg is excluded by !**/*.jpg
Files selected for processing (25)
  • .env.development (1 hunks)
  • .eslintrc.cjs (1 hunks)
  • .firebaserc (1 hunks)
  • .vscode/settings.json (1 hunks)
  • README.md (1 hunks)
  • firebase.json (1 hunks)
  • index.html (1 hunks)
  • package.json (3 hunks)
  • src/App.tsx (1 hunks)
  • src/_mock/assets.js (1 hunks)
  • src/_mock/handlers/_user.js (1 hunks)
  • src/api/apiClient.ts (3 hunks)
  • src/api/services/userService.ts (1 hunks)
  • src/layouts/_common/notice.tsx (1 hunks)
  • src/layouts/dashboard/nav.tsx (1 hunks)
  • src/pages/dashboard/workbench/banner-card.tsx (1 hunks)
  • src/pages/sys/login/Login.tsx (2 hunks)
  • src/pages/sys/login/LoginForm.tsx (2 hunks)
  • src/pages/sys/login/RegisterForm.tsx (1 hunks)
  • src/pages/sys/others/calendar/index.tsx (1 hunks)
  • src/router/components/auth-guard.tsx (1 hunks)
  • src/server.ts (1 hunks)
  • src/store/userStore.ts (1 hunks)
  • tsconfig.json (1 hunks)
  • types/entity.ts (1 hunks)
Files skipped from review due to trivial changes (8)
  • .eslintrc.cjs
  • .firebaserc
  • firebase.json
  • index.html
  • src/App.tsx
  • src/api/services/userService.ts
  • src/layouts/_common/notice.tsx
  • tsconfig.json
Additional context used
Biome
.vscode/settings.json

[error] 26-27: Expected a property but instead found '}'. (parse)

Expected a property here.

src/pages/sys/login/LoginForm.tsx

[error] 64-64: Provide an explicit type prop for the button element. (lint/a11y/useButtonType)

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

src/layouts/dashboard/nav.tsx

[error] 57-57: This hook specifies more dependencies than necessary: collapsed (lint/correctness/useExhaustiveDependencies)

This dependency can be removed from the list.


[error] 141-150: Provide an explicit type prop for the button element. (lint/a11y/useButtonType)

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

src/pages/dashboard/workbench/banner-card.tsx

[error] 75-76: Alternative text title element cannot be empty (lint/a11y/noSvgWithoutTitle)

For accessibility purposes, SVGs should have an alternative text, provided via title element. If the svg element has role="img", you should add the aria-label or aria-labelledby attribute.


[error] 50-55: Provide an explicit type prop for the button element. (lint/a11y/useButtonType)

The default type of a button is submit, which causes the submission of a form when placed inside a form element. This is likely not the behaviour that you want inside a React application.
Allowed button types are: submit, button or reset

LanguageTool
README.md

[duplication] ~34-~34: Possible typo: you repeated a word (ENGLISH_WORD_REPEAT_RULE)
Context: ... - Data fetching using React-Query. ## Document Document ...

Markdownlint
README.md

20-20: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


21-21: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


22-22: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


23-23: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


24-24: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


25-25: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


26-26: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


27-27: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


28-28: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


29-29: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


30-30: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


31-31: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


32-32: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


105-105: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


106-106: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


107-107: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


108-108: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


109-109: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


110-110: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


111-111: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


112-112: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


113-113: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


114-114: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


115-115: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


116-116: Expected: plus; Actual: dash (MD004, ul-style)
Unordered list style


1-1: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


2-2: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


80-80: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


73-73: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


100-100: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


11-11: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


34-34: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


74-74: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


75-75: Expected: 1; Actual: 0; Above (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


75-75: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


82-82: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


91-91: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


77-77: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


79-79: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


84-84: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


86-86: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


93-93: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


95-95: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


12-12: null (MD032, blanks-around-lists)
Lists should be surrounded by blank lines


12-12: null (MD034, no-bare-urls)
Bare URL used


89-89: null (MD034, no-bare-urls)
Bare URL used


98-98: null (MD034, no-bare-urls)
Bare URL used


77-77: null (MD040, fenced-code-language)
Fenced code blocks should have a language specified


84-84: null (MD040, fenced-code-language)
Fenced code blocks should have a language specified


93-93: null (MD040, fenced-code-language)
Fenced code blocks should have a language specified


4-4: null (MD045, no-alt-text)
Images should have alternate text (alt text)


15-15: Expected: leading_only; Actual: leading_and_trailing; Unexpected trailing pipe (MD055, table-pipe-style)
Table pipe style

Additional comments not posted (13)
.env.development (1)

1-1: Ensure VITE_APP_BASE_API is appropriately configured for production environments.

.vscode/settings.json (1)

26-26: Update the display language setting to align with rebranding efforts.

Tools
Biome

[error] 26-27: Expected a property but instead found '}'. (parse)

Expected a property here.

src/router/components/auth-guard.tsx (1)

20-23: Token expiration logic added to enhance security.

types/entity.ts (1)

6-6: Add expiresIn to UserToken to support new token expiration features.

src/_mock/handlers/_user.js (1)

8-8: Clarification comment added to indicate the shift from mock to Node server.

src/pages/sys/login/Login.tsx (2)

23-28: Token expiration checks added to enhance security.


44-45: Rebranding text updated to "SkillRazr Admin".

Verification successful

The rebranding to "SkillRazr Admin" appears to be consistent across multiple files in the repository. The search results show that the new brand name is present in various key files, including index.html, README.md, src/App.tsx, and several components and layout files.

  • index.html
  • README.md
  • src/App.tsx
  • src/pages/sys/login/Login.tsx
  • src/pages/dashboard/workbench/banner-card.tsx
  • src/layouts/dashboard/nav.tsx
  • src/layouts/_common/notice.tsx

No instances of the old brand name "Slash Admin" were found.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the rebranding is consistent across all text files.

# Test: Search for old and new brand names across all text files.
rg --type txt 'Slash Admin' || rg --type txt 'SkillRazr Admin'

Length of output: 361



Script:

#!/bin/bash
# List all files in the repository to verify the presence of text files.
fd

# Search for old and new brand names across all file types.
rg 'Slash Admin' || rg 'SkillRazr Admin'

Length of output: 14643

src/api/apiClient.ts (1)

37-39: Ensure that errors are handled gracefully to avoid unhandled promise rejections.

README.md (1)

5-7: Rebranding changes are correctly implemented.

package.json (1)

12-13: New scripts and dependencies are correctly added for the server setup.

Also applies to: 35-46, 78-83

src/server.ts (1)

83-96: Consider using middleware for token verification.
[REFACTOR_SUGGESTion]

- app.get('/protected', (req: Request, res: Response) => {
-   const token = req.headers.authorization;
-   if (!token) {
-     return res.status(401).json({ message: 'You are not authorized to access this' });
-   }
-   jwt.verify(token, JWT_SECRET, (err, decoded) => {
-     if (err) {
-       return res.status(401).json({ message: 'Invalid token' });
-     }
-     res.json({ message: 'Protected route accessed', user: decoded, status: 0 });
-   });
- });
+ app.get('/protected', tokenCheckMiddleware, protectedRouteHandler);
src/pages/sys/others/calendar/index.tsx (1)

31-31: Consider initializing state with actual data if available.

src/pages/dashboard/workbench/banner-card.tsx (1)

36-37: Rebranding text updated correctly to reflect "SkillRazr Admin".

import { ResultEnum } from '#/enum';

// 创建 axios 实例
console.log('api base url', import.meta.env.VITE_APP_BASE_API);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing or replacing console.log statements with a more robust logging mechanism for production.

Also applies to: 32-32

Comment on lines +26 to 27
"i18n-ally.displayLanguage": "en_US",
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the syntax error in the JSON file.

-  "i18n-ally.displayLanguage": "en_US",
+  "i18n-ally.displayLanguage": "en_US"
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"i18n-ally.displayLanguage": "en_US",
}
"i18n-ally.displayLanguage": "en_US"
Tools
Biome

[error] 26-27: Expected a property but instead found '}'. (parse)

Expected a property here.

Comment on lines +14 to +17
const { accessToken, expiresIn } = useUserToken();
console.log('accessToken', accessToken, expiresIn);
const isTokenExpired = Date.now() > new Date(expiresIn!).getTime();
console.log('token expired', isTokenExpired);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure robust handling of potentially undefined expiresIn values to prevent runtime errors.

-  const isTokenExpired = Date.now() > new Date(expiresIn!).getTime();
+  const isTokenExpired = expiresIn ? Date.now() > new Date(expiresIn).getTime() : true;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const { accessToken, expiresIn } = useUserToken();
console.log('accessToken', accessToken, expiresIn);
const isTokenExpired = Date.now() > new Date(expiresIn!).getTime();
console.log('token expired', isTokenExpired);
const { accessToken, expiresIn } = useUserToken();
console.log('accessToken', accessToken, expiresIn);
const isTokenExpired = expiresIn ? Date.now() > new Date(expiresIn).getTime() : true;
console.log('token expired', isTokenExpired);

export interface UserToken {
accessToken?: string;
refreshToken?: string;
expiresIn?: string
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a missing semicolon for consistency in TypeScript syntax.

-  expiresIn?: string
+  expiresIn?: string;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
expiresIn?: string
expiresIn?: string;

Comment on lines +63 to +66
console.log('res from login api', res);

const { user, accessToken, refreshToken, expiresIn } = res;
setUserToken({ accessToken, refreshToken, expiresIn });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid logging sensitive information such as user tokens to ensure security.

Comment on lines +19 to +25
try {
const resp = await signUpMutation.mutateAsync(values);
console.log('resp signup from server', resp);
backToLogin();
} catch (e) {
console.log('e', e);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider providing user feedback in the catch block instead of just logging the error.

Comment on lines +31 to +51
app.post('/signup', async (req: Request, res: Response): Promise<any> => {
const { username, password } = req.body;

console.log('signup, userName', 'password', username, password);

// Check if the user already exists
const userExists = users.find((user) => user.username === username);
if (userExists) {
return res.status(400).json({ message: 'User already exists' });
}

// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);

// Save the new user
users.push({ username, password: hashedPassword, permissions: adminPermissions });

console.log('hashed password', hashedPassword, 'users database', users);

return res.status(201).json({ message: 'User created successfully', status: 0, data: [] });
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the signup endpoint to separate concerns.

- app.post('/signup', async (req: Request, res: Response): Promise<any> => {
-   const { username, password } = req.body;
-   const userExists = users.find((user) => user.username === username);
-   if (userExists) {
-     return res.status(400).json({ message: 'User already exists' });
-   }
-   const hashedPassword = await bcrypt.hash(password, 10);
-   users.push({ username, password: hashedPassword, permissions: adminPermissions });
-   return res.status(201).json({ message: 'User created successfully', status: 0, data: [] });
- });
+ app.post('/signup', signupHandler);

Committable suggestion was skipped due to low confidence.

Comment on lines +54 to +80
app.post('/signin', async (req: Request, res: Response) => {
const { username, password } = req.body;

const now = Date.now();
const expiresIn = new Date(now + 1 * 60 * 1000).toISOString();

console.log('sign in, userName', 'password', username, password, expiresIn);

// Find the user
const user = users.find((user) => user.username === username);

console.log('user found ', user);
if (!user) {
return res.status(400).json({ message: 'Invalid credentials' });
}

// Check the password
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid credentials' });
}

// Generate a JWT token
const token = jwt.sign({ username: user.username }, JWT_SECRET, { expiresIn: '1h' });

res.json({ status: 0, data: { user, accessToken: token, expiresIn } });
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize the signin endpoint by abstracting repetitive logic.

- app.post('/signin', async (req: Request, res: Response) => {
-   const { username, password } = req.body;
-   const user = users.find((user) => user.username === username);
-   if (!user) {
-     return res.status(400).json({ message: 'Invalid credentials' });
-   }
-   const isMatch = await bcrypt.compare(password, user.password);
-   if (!isMatch) {
-     return res.status(400).json({ message: 'Invalid credentials' });
-   }
-   const token = jwt.sign({ username: user.username }, JWT_SECRET, { expiresIn: '1h' });
-   res.json({ status: 0, data: { user, accessToken: token, expiresIn } });
- });
+ app.post('/signin', signinHandler);

Committable suggestion was skipped due to low confidence.

Comment on lines +16 to +17
app.use(bodyParser.json());
app.use(cors());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The search results indicate that there are no try-catch blocks or specific error-handling middleware associated with the bodyParser and cors middleware in src/server.ts. This confirms the concern about the lack of proper error handling for these middleware functions.

  • Ensure proper error handling for bodyParser and cors middleware in src/server.ts.
Analysis chain

Ensure proper error handling for middleware.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify proper error handling for bodyParser and cors middleware.

# Test: Search for error handling related to bodyParser and cors.
rg --type python $'app.use(bodyParser.json()); app.use(cors());'

Length of output: 96



Script:

#!/bin/bash
# Description: Verify proper error handling for bodyParser and cors middleware.

# Test: Search for error handling related to bodyParser and cors in TypeScript files.
rg --type typescript 'app.use(bodyParser.json()); app.use(cors());'

Length of output: 104



Script:

#!/bin/bash
# Description: Verify proper error handling for bodyParser and cors middleware.

# Test: Search for error handling related to bodyParser and cors in JavaScript files.
rg --type js 'app.use(bodyParser.json()); app.use(cors());'

Length of output: 61



Script:

#!/bin/bash
# Description: Verify proper error handling in src/server.ts.

# Test: Search for try-catch blocks and error-handling middleware in src/server.ts.
rg 'try|catch|app.use' src/server.ts

Length of output: 83

Comment on lines +534 to +535
export const adminPermissions = ADMIN_ROLE.permission;
export const testPermmissions = TEST_ROLE.permission;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo in variable name.

- export const testPermmissions = TEST_ROLE.permission;
+ export const testPermissions = TEST_ROLE.permission;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const adminPermissions = ADMIN_ROLE.permission;
export const testPermmissions = TEST_ROLE.permission;
export const adminPermissions = ADMIN_ROLE.permission;
export const testPermissions = TEST_ROLE.permission;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant