-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from thatgurkangurk/first-user-admin
set first user to admin + auth refactor
- Loading branch information
Showing
11 changed files
with
205 additions
and
78 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 |
---|---|---|
@@ -1,17 +1,22 @@ | ||
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 | ||
); | ||
CREATE TABLE IF NOT EXISTS `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 | ||
CREATE TABLE `user` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`username` text NOT NULL, | ||
`hashed_password` text NOT NULL, | ||
`role` text DEFAULT 'user' NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS `user` (`id` text PRIMARY KEY NOT NULL, | ||
`username` text NOT NULL, | ||
`hashed_password` text NOT NULL, | ||
`role` text DEFAULT 'user' NOT NULL); | ||
|
||
--> statement-breakpoint | ||
CREATE UNIQUE INDEX `session_id_unique` ON `session` (`id`);--> statement-breakpoint | ||
CREATE UNIQUE INDEX `user_id_unique` ON `user` (`id`);--> statement-breakpoint | ||
CREATE UNIQUE INDEX `user_username_unique` ON `user` (`username`); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS `session_id_unique` ON `session` (`id`);--> statement-breakpoint | ||
|
||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS `user_id_unique` ON `user` (`id`);--> statement-breakpoint | ||
|
||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS `user_username_unique` ON `user` (`username`); |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
CREATE TABLE `link_click` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`link_id` text NOT NULL, | ||
`timestamp` integer DEFAULT (unixepoch()) NOT NULL | ||
); | ||
CREATE TABLE IF NOT EXISTS `link_click` (`id` text PRIMARY KEY NOT NULL, | ||
`link_id` text NOT NULL, | ||
`timestamp` integer DEFAULT (unixepoch()) NOT NULL); | ||
|
||
--> statement-breakpoint | ||
CREATE TABLE `link` ( | ||
`id` text PRIMARY KEY NOT NULL, | ||
`user_id` text NOT NULL, | ||
`slug` text NOT NULL, | ||
`target` text NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS `link` (`id` text PRIMARY KEY NOT NULL, | ||
`user_id` text NOT NULL, | ||
`slug` text NOT NULL, | ||
`target` text NOT NULL); | ||
|
||
--> statement-breakpoint | ||
CREATE UNIQUE INDEX `link_slug_unique` ON `link` (`slug`); | ||
|
||
CREATE UNIQUE INDEX IF NOT EXISTS `link_slug_unique` ON `link` (`slug`); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { hash, verify } from "@node-rs/argon2"; | ||
|
||
async function hashPassword(password: string) { | ||
return await hash(password); | ||
} | ||
|
||
async function verifyPassword(hash: string, password: string) { | ||
return await verify(hash, password); | ||
} | ||
|
||
export { | ||
hashPassword, | ||
verifyPassword | ||
} |
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,42 @@ | ||
import type { User, Cookie, Session } from "lucia"; | ||
import { lucia } from "./auth"; | ||
import type { RequestEvent } from "@sveltejs/kit"; | ||
import { db } from "$lib/db"; | ||
import { sessions } from "$lib/db/schema/session"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
async function _createSession(userOrUserId: User | string): Promise<Session> { | ||
if (typeof userOrUserId === "string") { | ||
const session = await lucia.createSession(userOrUserId, {}); | ||
return session; | ||
} else { | ||
const session = await lucia.createSession(userOrUserId.id, {}); | ||
return session; | ||
} | ||
} | ||
|
||
async function createSessionCookie(user: User): Promise<Cookie>; | ||
async function createSessionCookie(userId: string): Promise<Cookie>; | ||
async function createSessionCookie( | ||
userOrUserId: User | string | ||
): Promise<Cookie> { | ||
const session = await _createSession(userOrUserId); | ||
const sessionCookie = lucia.createSessionCookie(session.id); | ||
return sessionCookie; | ||
} | ||
|
||
async function invalidateSession(session: Session, event: RequestEvent) { | ||
try { | ||
await lucia.invalidateSession(session.id); | ||
const sessionCookie = lucia.createBlankSessionCookie(); | ||
event.cookies.set(sessionCookie.name, sessionCookie.value, { | ||
path: ".", | ||
...sessionCookie.attributes, | ||
}); | ||
await db.delete(sessions).where(eq(sessions.id, session.id)); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
|
||
export { createSessionCookie, 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,81 @@ | ||
import { db } from "$lib/db"; | ||
import { users } from "$lib/db/schema/user"; | ||
import { generateId, type User } from "lucia"; | ||
import { hashPassword } from "./password"; | ||
import { sql } from "drizzle-orm"; | ||
|
||
async function userExists(username: string) { | ||
const existingUser = await db.query.users.findFirst({ | ||
where: (user, { eq }) => eq(user.username, username), | ||
}); | ||
|
||
return existingUser; | ||
} | ||
|
||
async function getTotalUserCount() { | ||
const [count] = await db.select({ count: sql<number>`count(*)` }).from(users); | ||
|
||
return count.count; | ||
} | ||
|
||
async function isUsersEmpty() { | ||
const count = await getTotalUserCount(); | ||
const isEmpty = count === 0; | ||
|
||
return isEmpty; | ||
} | ||
|
||
async function createUser({ | ||
username, | ||
password, | ||
}: { | ||
username: string; | ||
password: string; | ||
}): Promise< | ||
| { | ||
success: true; | ||
user: User; | ||
} | ||
| { | ||
success: false; | ||
cause: "USER_EXISTS" | "UNEXPECTED_ERROR"; | ||
} | ||
> { | ||
if (await userExists(username)) | ||
return { | ||
success: false, | ||
cause: "USER_EXISTS", | ||
}; | ||
|
||
const id = generateId(15); | ||
const hashedPassword = await hashPassword(password); | ||
const isFirstUser = await isUsersEmpty(); | ||
|
||
try { | ||
const insertedUser = await db | ||
.insert(users) | ||
.values({ | ||
id: id, | ||
username: username, | ||
hashed_password: hashedPassword, | ||
role: isFirstUser ? "admin" : "user", | ||
}) | ||
.returning({ | ||
id: users.id, | ||
username: users.username, | ||
role: users.role, | ||
}); | ||
|
||
return { | ||
success: true, | ||
user: insertedUser[0], | ||
}; | ||
} catch (e) { | ||
return { | ||
success: false, | ||
cause: "UNEXPECTED_ERROR", | ||
}; | ||
} | ||
} | ||
|
||
export { userExists, createUser }; |
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
Oops, something went wrong.