-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🔥 migrating to sessions, using file routes, adding auth provider
- Loading branch information
Showing
49 changed files
with
768 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CREATE TABLE `session` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`user_id` text NOT NULL, | ||
`expires_at` integer NOT NULL, | ||
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE no action | ||
); | ||
--> statement-breakpoint | ||
PRAGMA foreign_keys=OFF;--> statement-breakpoint | ||
CREATE TABLE `__new_user` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`name` text NOT NULL, | ||
`password` text NOT NULL, | ||
`email` text NOT NULL, | ||
`created_at` integer DEFAULT '"2025-01-04T22:24:29.828Z"' NOT NULL | ||
); | ||
--> statement-breakpoint | ||
INSERT INTO `__new_user`("id", "name", "password", "email", "created_at") SELECT "id", "name", "password", "email", "created_at" FROM `user`;--> statement-breakpoint | ||
DROP TABLE `user`;--> statement-breakpoint | ||
ALTER TABLE `__new_user` RENAME TO `user`;--> statement-breakpoint | ||
PRAGMA foreign_keys=ON;--> statement-breakpoint | ||
CREATE UNIQUE INDEX `user_email_unique` ON `user` (`email`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
{ | ||
"version": "6", | ||
"dialect": "sqlite", | ||
"id": "1c74d9e8-3e5e-4199-9175-7514767a6912", | ||
"prevId": "e1a9d8d8-e2dd-4fa3-864a-b396b6f6034e", | ||
"tables": { | ||
"session": { | ||
"name": "session", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "text", | ||
"primaryKey": true, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"user_id": { | ||
"name": "user_id", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"expires_at": { | ||
"name": "expires_at", | ||
"type": "integer", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": { | ||
"session_user_id_user_id_fk": { | ||
"name": "session_user_id_user_id_fk", | ||
"tableFrom": "session", | ||
"tableTo": "user", | ||
"columnsFrom": ["user_id"], | ||
"columnsTo": ["id"], | ||
"onDelete": "no action", | ||
"onUpdate": "no action" | ||
} | ||
}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {}, | ||
"checkConstraints": {} | ||
}, | ||
"user": { | ||
"name": "user", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "text", | ||
"primaryKey": true, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"name": { | ||
"name": "name", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"password": { | ||
"name": "password", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"email": { | ||
"name": "email", | ||
"type": "text", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false | ||
}, | ||
"created_at": { | ||
"name": "created_at", | ||
"type": "integer", | ||
"primaryKey": false, | ||
"notNull": true, | ||
"autoincrement": false, | ||
"default": "'\"2025-01-04T22:24:29.828Z\"'" | ||
} | ||
}, | ||
"indexes": { | ||
"user_email_unique": { | ||
"name": "user_email_unique", | ||
"columns": ["email"], | ||
"isUnique": true | ||
} | ||
}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {}, | ||
"checkConstraints": {} | ||
} | ||
}, | ||
"views": {}, | ||
"enums": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": {} | ||
}, | ||
"internal": { | ||
"indexes": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { sha256 } from "@oslojs/crypto/sha2"; | ||
import { encodeHexLowerCase } from "@oslojs/encoding"; | ||
import db from "../../database"; | ||
import type { Session } from "../../database/schema"; | ||
import { sessionTable } from "../../database/schema"; | ||
|
||
async function createSession(token: string, userId: string): Promise<Session> { | ||
const sessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(token))); | ||
const session: Session = { | ||
id: sessionId, | ||
userId, | ||
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30), | ||
}; | ||
await db.insert(sessionTable).values(session); | ||
return session; | ||
} | ||
|
||
export default createSession; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { eq } from "drizzle-orm"; | ||
import db from "../../database"; | ||
import { sessionTable } from "../../database/schema"; | ||
|
||
async function invalidateSession(sessionId: string): Promise<void> { | ||
await db.delete(sessionTable).where(eq(sessionTable.id, sessionId)); | ||
} | ||
|
||
export default invalidateSession; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { sha256 } from "@oslojs/crypto/sha2"; | ||
import { encodeHexLowerCase } from "@oslojs/encoding"; | ||
import { eq } from "drizzle-orm"; | ||
import db from "../../database"; | ||
import { sessionTable, userTable } from "../../database/schema"; | ||
import type { SessionValidationResult } from "../types"; | ||
|
||
export async function validateSessionToken( | ||
token: string, | ||
): Promise<SessionValidationResult> { | ||
const sessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(token))); | ||
const result = await db | ||
.select({ user: userTable, session: sessionTable }) | ||
.from(sessionTable) | ||
.innerJoin(userTable, eq(sessionTable.userId, userTable.id)) | ||
.where(eq(sessionTable.id, sessionId)); | ||
|
||
if (result.length < 1) { | ||
return { session: null, user: null }; | ||
} | ||
|
||
const { user, session } = result[0]; | ||
|
||
const isSessionExpired = Date.now() >= session.expiresAt.getTime(); | ||
|
||
if (isSessionExpired) { | ||
await db.delete(sessionTable).where(eq(sessionTable.id, session.id)); | ||
return { session: null, user: null }; | ||
} | ||
|
||
const isSessionHalfWayExpired = | ||
Date.now() >= session.expiresAt.getTime() - 1000 * 60 * 60 * 24 * 15; | ||
|
||
if (isSessionHalfWayExpired) { | ||
session.expiresAt = new Date(Date.now() + 1000 * 60 * 60 * 24 * 30); | ||
await db | ||
.update(sessionTable) | ||
.set({ | ||
expiresAt: session.expiresAt, | ||
}) | ||
.where(eq(sessionTable.id, session.id)); | ||
} | ||
|
||
return { session, user }; | ||
} |
Oops, something went wrong.