Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rr/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
NODE_ENV=development
DATABASE_URL=postgres://user:postgres@localhost:5432/
BETTER_AUTH_SECRET=
BETTER_AUTH_URL=http://localhost:3000
2 changes: 2 additions & 0 deletions rr/app/lib/auth-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient({});
17 changes: 17 additions & 0 deletions rr/app/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { getDB } from "./db";

export function getAuth(env?: Env) {
const auth = betterAuth({
database: drizzleAdapter(getDB(env), {
provider: "pg",
}),
emailAndPassword: {
enabled: true,
},
});
return auth;
}

export const auth = getAuth();
15 changes: 15 additions & 0 deletions rr/app/lib/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { drizzle as drizzleNeon } from "drizzle-orm/neon-http";
import { drizzle as drizzlePg } from "drizzle-orm/node-postgres";

export function getDB(env?: Env) {
// better-auth/cli を実行するとき
if (!env) {
const db = drizzlePg(process.env.DATABASE_URL);
return db;
}
const db =
env.NODE_ENV === "development"
? drizzlePg(env.DATABASE_URL)
: drizzleNeon(env.DATABASE_URL);
return db;
}
70 changes: 69 additions & 1 deletion rr/app/lib/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
import {
boolean,
integer,
pgTable,
text,
timestamp,
varchar,
} from "drizzle-orm/pg-core";

export const usersTable = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 255 }).notNull(),
age: integer().notNull(),
email: varchar({ length: 255 }).notNull().unique(),
});

// better-auth
export const user = pgTable("user", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").default(false).notNull(),
image: text("image"),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
});

export const session = pgTable("session", {
id: text("id").primaryKey(),
expiresAt: timestamp("expires_at").notNull(),
token: text("token").notNull().unique(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
ipAddress: text("ip_address"),
userAgent: text("user_agent"),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
});

export const account = pgTable("account", {
id: text("id").primaryKey(),
accountId: text("account_id").notNull(),
providerId: text("provider_id").notNull(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
accessToken: text("access_token"),
refreshToken: text("refresh_token"),
idToken: text("id_token"),
accessTokenExpiresAt: timestamp("access_token_expires_at"),
refreshTokenExpiresAt: timestamp("refresh_token_expires_at"),
scope: text("scope"),
password: text("password"),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
});

export const verification = pgTable("verification", {
id: text("id").primaryKey(),
identifier: text("identifier").notNull(),
value: text("value").notNull(),
expiresAt: timestamp("expires_at").notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at")
.defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(),
});
14 changes: 14 additions & 0 deletions rr/app/routes/api.auth.$.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getAuth } from "~/lib/auth";
import type { Route } from "./+types/api.auth.$";

export async function loader({ request, context }: Route.LoaderArgs) {
const { env } = context.cloudflare;
const auth = getAuth(env);
return auth.handler(request);
}

export async function action({ request, context }: Route.ActionArgs) {
const { env } = context.cloudflare;
const auth = getAuth(env);
return auth.handler(request);
}
75 changes: 74 additions & 1 deletion rr/bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@neondatabase/serverless": "^1.0.2",
"@react-router/fs-routes": "^7.9.4",
"better-auth": "^1.3.33",
"drizzle-orm": "^0.44.7",
"isbot": "^5.1.31",
"pg": "^8.16.3",
Expand Down