Skip to content

Commit

Permalink
"Issue in redirecting"
Browse files Browse the repository at this point in the history
  • Loading branch information
harshtiwariexe committed Oct 5, 2024
1 parent 3ffd826 commit c72bb8b
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 29 deletions.
Binary file modified bun.lockb
Binary file not shown.
7 changes: 6 additions & 1 deletion convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {
FilterApi,
FunctionReference,
} from "convex/server";
import type * as auth from "../auth.js";
import type * as http from "../http.js";

/**
* A utility for referencing Convex functions in your app's API.
Expand All @@ -24,7 +26,10 @@ import type {
* const myFunctionReference = api.myModule.myFunction;
* ```
*/
declare const fullApi: ApiFromModules<{}>;
declare const fullApi: ApiFromModules<{
auth: typeof auth;
http: typeof http;
}>;
export declare const api: FilterApi<
typeof fullApi,
FunctionReference<any, "public">
Expand Down
34 changes: 18 additions & 16 deletions convex/_generated/dataModel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@
* @module
*/

import { AnyDataModel } from "convex/server";
import type {
DataModelFromSchemaDefinition,
DocumentByName,
TableNamesInDataModel,
SystemTableNames,
} from "convex/server";
import type { GenericId } from "convex/values";

/**
* No `schema.ts` file found!
*
* This generated code has permissive types like `Doc = any` because
* Convex doesn't know your schema. If you'd like more type safety, see
* https://docs.convex.dev/using/schemas for instructions on how to add a
* schema file.
*
* After you change a schema, rerun codegen with `npx convex dev`.
*/
import schema from "../schema.js";

/**
* The names of all of your Convex tables.
*/
export type TableNames = string;
export type TableNames = TableNamesInDataModel<DataModel>;

/**
* The type of a document stored in Convex.
*
* @typeParam TableName - A string literal type of the table name (like "users").
*/
export type Doc = any;
export type Doc<TableName extends TableNames> = DocumentByName<
DataModel,
TableName
>;

/**
* An identifier for a document in Convex.
Expand All @@ -44,8 +44,10 @@ export type Doc = any;
*
* IDs are just strings at runtime, but this type can be used to distinguish them from other
* strings when type checking.
*
* @typeParam TableName - A string literal type of the table name (like "users").
*/
export type Id<TableName extends TableNames = TableNames> =
export type Id<TableName extends TableNames | SystemTableNames> =
GenericId<TableName>;

/**
Expand All @@ -57,6 +59,6 @@ export type Id<TableName extends TableNames = TableNames> =
* This type is used to parameterize methods like `queryGeneric` and
* `mutationGeneric` to make them type-safe.
*/
export type DataModel = AnyDataModel;
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;

/* prettier-ignore-end */
8 changes: 8 additions & 0 deletions convex/auth.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
providers: [
{
domain: process.env.CONVEX_SITE_URL,
applicationID: "convex",
},
],
};
5 changes: 5 additions & 0 deletions convex/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { convexAuth } from "@convex-dev/auth/server";

export const { auth, signIn, signOut, store } = convexAuth({
providers: [],
});
8 changes: 8 additions & 0 deletions convex/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { httpRouter } from "convex/server";
import { auth } from "./auth";

const http = httpRouter();

auth.addHttpRoutes(http);

export default http;
8 changes: 8 additions & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineSchema } from "convex/server";
import { authTables } from "@convex-dev/auth/server";

const schema = defineSchema({
...authTables,
});

export default schema;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"lint": "next lint"
},
"dependencies": {
"@auth/core": "^0.35.3",
"@convex-dev/auth": "^0.0.71",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
Expand Down
33 changes: 23 additions & 10 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Metadata } from "next";
import localFont from "next/font/local";
import "./globals.css";
import { ConvexClientProvider } from "./../components/ConvexClientProvider";
import { ConvexClientProvider } from "../components/ConvexClientProvider";
import { ConvexAuthNextjsServerProvider } from "@convex-dev/auth/nextjs/server";
import Head from "next/head";

const geistSans = localFont({
src: "./fonts/GeistVF.woff",
Expand All @@ -21,16 +23,27 @@ export const metadata: Metadata = {

export default function RootLayout({
children,
}: Readonly<{
}: {
children: React.ReactNode;
}>) {
}) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<ConvexClientProvider>{children}</ConvexClientProvider>
</body>
</html>
<ConvexAuthNextjsServerProvider>
<html lang="en">
<Head>
<meta charSet="UTF-8" />
<meta name="description" content="Team-Chat" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Team-Chat</title>
</Head>
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<ConvexClientProvider>{children}</ConvexClientProvider>
</body>
</html>
</ConvexAuthNextjsServerProvider>
);
}
7 changes: 7 additions & 0 deletions src/app/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

function SignInPage() {
return <div>SignInPage</div>;
}

export default SignInPage;
7 changes: 7 additions & 0 deletions src/app/test/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

function page() {
return <div>page</div>;
}

export default page;
9 changes: 7 additions & 2 deletions src/components/ConvexClientProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"use client";

import { ConvexProvider, ConvexReactClient } from "convex/react";
import { ConvexAuthNextjsProvider } from "@convex-dev/auth/nextjs";
import { ConvexReactClient } from "convex/react";
import { ReactNode } from "react";

const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL!);

export function ConvexClientProvider({ children }: { children: ReactNode }) {
return <ConvexProvider client={convex}>{children}</ConvexProvider>;
return (
<ConvexAuthNextjsProvider client={convex}>
{children}
</ConvexAuthNextjsProvider>
);
}
35 changes: 35 additions & 0 deletions src/midlleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
isAuthenticatedNextjs,
nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server";

const isSignInPage = createRouteMatcher(["/signin"]);

export default convexAuthNextjsMiddleware((request) => {
const method = request.method;
console.log(request);

const isAuthenticated = isAuthenticatedNextjs();
console.log("Is Authenticated: ", isAuthenticated);
console.log("Is Sign In Page: ", isSignInPage(request));

// Allow GET requests without side-effects
if (method === "GET") {
console.log("Allowing GET request without redirect");
return;
}

// Redirect if not authenticated and the request isn't a GET or sign-in request
if (!isSignInPage(request) && !isAuthenticated) {
console.log("Redirecting to /signin");
return nextjsMiddlewareRedirect(request, "/signin");
}
});

convexAuthNextjsMiddleware();
export const config = {
// The following matcher runs middleware on all routes except static assets
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

0 comments on commit c72bb8b

Please sign in to comment.