Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware based auth for pages, robots.txt, sitemap.xml #164

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

aryasaatvik
Copy link
Contributor

@aryasaatvik aryasaatvik commented Jul 25, 2024

Middleware-Based Authentication for Pages, Robots.txt, and Sitemap.xml

Overview

This pull request introduces middleware-based authentication for various pages within the application, including the onboarding flow, sign-in, and dashboard pages. It also includes updates to the robots.txt and sitemap.xml files, ensuring proper handling of user authentication states.

Changes

  • New Features: None

  • Refactoring:

    • Implemented middleware-based authentication for pages, ensuring users are redirected to the appropriate pages based on their authentication status.
    • Updated redirect paths for successful sign-in and onboarding completion to use the /app/home route instead of the previous /home route.
    • Refactored the SignIn component to remove unused searchParams parameter and simplify the logic.
    • Updated the Navbar component in the landing page to use the /app/home route for the "Home" link.
    • Adjusted the Page functions in the memory-related pages to handle cases where user data is not available, redirecting to the /app/home route.
  • Other Changes:

    • Consolidated redirect logic across various pages to maintain consistency throughout the application.
    • Updated the routing and redirect logic to ensure a seamless user experience for both authenticated and unauthenticated states.

✨ Generated with love by Kaizen ❤️

Original Description None

apps/web/app/(auth)/onboarding/page.tsx Outdated Show resolved Hide resolved
apps/web/app/(auth)/signin/page.tsx Outdated Show resolved Hide resolved
apps/web/app/(landing)/page.tsx Outdated Show resolved Hide resolved
apps/web/app/actions/doers.ts Outdated Show resolved Hide resolved
apps/web/app/app/(dash)/(memories)/memories/page.tsx Outdated Show resolved Hide resolved
apps/web/app/app/(dash)/chat/chatWindow.tsx Outdated Show resolved Hide resolved
apps/web/app/app/(dash)/chat/route.ts Outdated Show resolved Hide resolved
apps/web/app/app/(dash)/header/autoBreadCrumbs.tsx Outdated Show resolved Hide resolved
@aryasaatvik aryasaatvik marked this pull request as draft July 25, 2024 21:29
Comment on lines +4 to +11
return [
{
url: 'https://supermemory.ai/',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
}
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: The sitemap.xml generation logic is missing URLs for authenticated pages.

Solution: Add URLs for authenticated pages to the sitemap.xml generation logic.

Reason For Comment: The current implementation only includes the root URL, which is insufficient for SEO purposes. Authenticated pages should be included in the sitemap to improve discoverability by search engines.

Suggested change
return [
{
url: 'https://supermemory.ai/',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 1,
}
]
import{type MetadataRoute}from 'next'
export default function sitemap(): MetadataRoute.Sitemap{
return[
{url: 'https://supermemory.ai/', lastModified: new Date(), changeFrequency: 'yearly', priority: 1},
{url: 'https://supermemory.ai/app/home', lastModified: new Date(), changeFrequency: 'daily', priority: 0.8},
{url: 'https://supermemory.ai/memories', lastModified: new Date(), changeFrequency: 'daily', priority: 0.8},
{url: 'https://supermemory.ai/space', lastModified: new Date(), changeFrequency: 'daily', priority: 0.8},
{url: 'https://supermemory.ai/chat', lastModified: new Date(), changeFrequency: 'daily', priority: 0.8},
{url: 'https://supermemory.ai/note', lastModified: new Date(), changeFrequency: 'daily', priority: 0.8},
{url: 'https://supermemory.ai/canvas', lastModified: new Date(), changeFrequency: 'daily', priority: 0.8},
]
}
```

@aryasaatvik aryasaatvik marked this pull request as ready for review July 25, 2024 23:34
@Dhravya
Copy link
Collaborator

Dhravya commented Jul 30, 2024

Hi! let's get this merged, can you resolve the conflicts please?

Copy link
Contributor

kaizen-bot bot commented Jul 30, 2024

Code Review

Attention Required: This PR has potential issues. 🚨

Authentication Logic

Redirect logic in Signin function should handle more cases.

Potential Solution:

Add error handling to manage failed authentication attempts and provide user feedback.

apps/web/app/(auth)/signin/page.tsx | 23 - 23

reason_for_request: The current implementation only checks if a user exists but does not handle cases where the authentication fails or if the user is not authorized.

level: [critical] , severity: [9]

Error Handling

The redirect logic in the Page function does not handle errors properly.

Potential Solution:

Change the condition to explicitly check for both success and data.

apps/web/app/(dash)/(memories)/space/[spaceid]/page.tsx | 13 - 13

reason_for_request: Using a nullish coalescing operator (??) in the condition might lead to unexpected behavior. It should explicitly check for both success and data.

level: [critical] , severity: [8]

Middleware Authentication Logic

The middleware function should handle both authenticated and unauthenticated routes correctly.

Potential Solution:

Ensure that the authentication check is comprehensive and covers all necessary routes.

apps/web/middleware.ts | 23 - 33

reason_for_request: If the authentication logic fails or is misconfigured, users may gain unauthorized access or be incorrectly redirected.

level: [critical] , severity: [8]

✨ Generated with love by Kaizen ❤️


Useful Commands
  • Feedback: Reply with !feedback [your message]

  • Ask PR: Reply with !ask-pr [your question]

  • Review: Reply with !review

if (user) {
redirect("/home");
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: Redirect logic in Signin function should handle more cases.

Solution: Add error handling to manage failed authentication attempts and provide user feedback.

Reason For Comment: The current implementation only checks if a user exists but does not handle cases where the authentication fails or if the user is not authorized.

Suggested change
async function Signin(){const user = await auth(); if (!user){return <ErrorComponent message='Authentication failed' />;}redirect('/home');}


async function Page({ params: { spaceid } }: { params: { spaceid: number } }) {
const user = await auth();

const { success, data } = await getMemoriesInsideSpace(spaceid);
if (!success ?? !data) return redirect("/home");
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: The redirect logic in the Page function does not handle errors properly.

Solution: Change the condition to explicitly check for both success and data.

Reason For Comment: Using a nullish coalescing operator (??) in the condition might lead to unexpected behavior. It should explicitly check for both success and data.

Suggested change
if (!success ?? !data) return redirect("/home");
if (!success || !data) return redirect('/home');

Comment on lines +23 to +33
const info = await auth();
if (routeTypes.authed.some((route) => request.nextUrl.pathname.startsWith(route))) {
if (!info) {
return NextResponse.redirect(new URL("/signin", request.nextUrl));
}
} else if (routeTypes.unAuthedOnly.some((route) => request.nextUrl.pathname.endsWith(route))) {
if (info) {
return NextResponse.redirect(new URL("/home", request.nextUrl));
}
}
return NextResponse.next();
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: The middleware function should handle both authenticated and unauthenticated routes correctly.

Solution: Ensure that the authentication check is comprehensive and covers all necessary routes.

Reason For Comment: If the authentication logic fails or is misconfigured, users may gain unauthorized access or be incorrectly redirected.

Suggested change
const info = await auth();
if (routeTypes.authed.some((route) => request.nextUrl.pathname.startsWith(route))) {
if (!info) {
return NextResponse.redirect(new URL("/signin", request.nextUrl));
}
} else if (routeTypes.unAuthedOnly.some((route) => request.nextUrl.pathname.endsWith(route))) {
if (info) {
return NextResponse.redirect(new URL("/home", request.nextUrl));
}
}
return NextResponse.next();
if (routeTypes.authed.some((route) => request.nextUrl.pathname.startsWith(route))){/* existing logic */}else if (routeTypes.unAuthedOnly.some((route) => request.nextUrl.pathname.endsWith(route))){/* existing logic */}

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.

2 participants