Skip to content

Commit e39f49a

Browse files
authored
Merge pull request #2 from cornell-dti/annie/setup-auth
set up convex auth with Google OAuth
2 parents 05bcb28 + 94a3c32 commit e39f49a

11 files changed

Lines changed: 134 additions & 47 deletions

File tree

bun.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

convex/_generated/api.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
* @module
99
*/
1010

11+
import type * as auth from "../auth.js";
12+
import type * as http from "../http.js";
13+
1114
import type {
1215
ApiFromModules,
1316
FilterApi,
1417
FunctionReference,
1518
} from "convex/server";
1619

17-
declare const fullApi: ApiFromModules<{}>;
20+
declare const fullApi: ApiFromModules<{
21+
auth: typeof auth;
22+
http: typeof http;
23+
}>;
1824

1925
/**
2026
* A utility for referencing Convex functions in your app's public API.

convex/_generated/dataModel.d.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@
88
* @module
99
*/
1010

11-
import { AnyDataModel } from "convex/server";
11+
import type {
12+
DataModelFromSchemaDefinition,
13+
DocumentByName,
14+
TableNamesInDataModel,
15+
SystemTableNames,
16+
} from "convex/server";
1217
import type { GenericId } from "convex/values";
13-
14-
/**
15-
* No `schema.ts` file found!
16-
*
17-
* This generated code has permissive types like `Doc = any` because
18-
* Convex doesn't know your schema. If you'd like more type safety, see
19-
* https://docs.convex.dev/using/schemas for instructions on how to add a
20-
* schema file.
21-
*
22-
* After you change a schema, rerun codegen with `npx convex dev`.
23-
*/
18+
import schema from "../schema.js";
2419

2520
/**
2621
* The names of all of your Convex tables.
2722
*/
28-
export type TableNames = string;
23+
export type TableNames = TableNamesInDataModel<DataModel>;
2924

3025
/**
3126
* The type of a document stored in Convex.
27+
*
28+
* @typeParam TableName - A string literal type of the table name (like "users").
3229
*/
33-
export type Doc = any;
30+
export type Doc<TableName extends TableNames> = DocumentByName<
31+
DataModel,
32+
TableName
33+
>;
3434

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

4951
/**
@@ -55,4 +57,4 @@ export type Id<TableName extends TableNames = TableNames> =
5557
* This type is used to parameterize methods like `queryGeneric` and
5658
* `mutationGeneric` to make them type-safe.
5759
*/
58-
export type DataModel = AnyDataModel;
60+
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;

convex/auth.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
providers: [
3+
{
4+
domain: process.env.CONVEX_SITE_URL,
5+
applicationID: "convex",
6+
},
7+
],
8+
};

convex/auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Google from "@auth/core/providers/google";
2+
import { convexAuth } from "@convex-dev/auth/server";
3+
4+
export const { auth, signIn, signOut, store, isAuthenticated } = convexAuth({
5+
providers: [Google],
6+
});

convex/http.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { httpRouter } from "convex/server";
2+
import { auth } from "./auth";
3+
4+
const http = httpRouter();
5+
6+
auth.addHttpRoutes(http);
7+
8+
export default http;

convex/schema.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineSchema } from "convex/server";
2+
import { authTables } from "@convex-dev/auth/server";
3+
4+
const schema = defineSchema({
5+
...authTables,
6+
// Your other tables...
7+
});
8+
9+
export default schema;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"type-check": "tsc --noEmit"
1212
},
1313
"dependencies": {
14+
"@auth/core": "0.37.0",
15+
"@convex-dev/auth": "^0.0.91",
1416
"convex": "^1.32.0",
1517
"react": "^19.2.0",
1618
"react-dom": "^19.2.0"

src/App.tsx

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
1-
import { useState } from 'react'
2-
import reactLogo from './assets/react.svg'
3-
import viteLogo from '/vite.svg'
4-
import './App.css'
1+
import './App.css';
52

63
function App() {
7-
const [count, setCount] = useState(0)
8-
4+
95
return (
10-
<>
11-
<div>
12-
<a href="https://vite.dev" target="_blank">
13-
<img src={viteLogo} className="logo" alt="Vite logo" />
14-
</a>
15-
<a href="https://react.dev" target="_blank">
16-
<img src={reactLogo} className="logo react" alt="React logo" />
17-
</a>
18-
</div>
19-
<h1>Vite + React</h1>
20-
<div className="card">
21-
<button onClick={() => setCount((count) => count + 1)}>
22-
count is {count}
23-
</button>
24-
<p>
25-
Edit <code>src/App.tsx</code> and save to test HMR
26-
</p>
27-
</div>
28-
<p className="read-the-docs">
29-
Click on the Vite and React logos to learn more
30-
</p>
31-
</>
6+
<div>
7+
App
8+
</div>
329
)
33-
}
10+
};
3411

3512
export default App

src/Auth.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useAuthActions } from "@convex-dev/auth/react";
2+
3+
export function SignIn() {
4+
const { signIn } = useAuthActions();
5+
return (
6+
<button onClick={() => {
7+
console.log("Signing in...");
8+
void signIn("google").then(() => console.log("Signed in!"));
9+
}}>Sign in with Google</button>
10+
);
11+
}
12+
13+
export function SignOut() {
14+
const { signOut } = useAuthActions();
15+
return (
16+
<button onClick={() => {
17+
console.log("Signing out...");
18+
void signOut().then(() => console.log("Signed out!"));
19+
}}>Sign Out</button>
20+
);
21+
}

0 commit comments

Comments
 (0)