Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login Draft #2

Open
wants to merge 2 commits into
base: Final-Project
Choose a base branch
from
Open
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
135 changes: 13 additions & 122 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@ datasource db {
}

model User {
id String @id
username String @unique
displayName String
email String? @unique
passwordHash String?
googleId String? @unique
avatarUrl String?
bio String?
sessions Session[]
posts Post[]
following Follow[] @relation("Following")
followers Follow[] @relation("Followers")
likes Like[]
bookmarks Bookmark[]
comments Comment[]
receivedNotifications Notification[] @relation("Recipient")
issuedNotifications Notification[] @relation("Issuer")

createdAt DateTime @default(now())
id String @id
username String @unique
displayName String
email String? @unique
passwordHash String?
googleID String? @unique
avatarUrl String?
bio String?
sessions Session[]

createAt DateTime @default(now())

@@map("users")
}
Expand All @@ -43,108 +35,7 @@ model Session {
id String @id
userId String
expiresAt DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@map("sessions")
}

model Follow {
followerId String
follower User @relation("Following", fields: [followerId], references: [id], onDelete: Cascade)
followingId String
following User @relation("Followers", fields: [followingId], references: [id], onDelete: Cascade)

@@unique([followerId, followingId])
@@map("follows")
}

model Post {
id String @id @default(cuid())
content String
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
attachments Media[]
likes Like[]
bookmarks Bookmark[]
comments Comment[]
linkedNotifications Notification[]

createdAt DateTime @default(now())

@@map("posts")
}

model Media {
id String @id @default(cuid())
postId String?
post Post? @relation(fields: [postId], references: [id], onDelete: SetNull)
type MediaType
url String

createdAt DateTime @default(now())

@@map("post_media")
}

enum MediaType {
IMAGE
VIDEO
}

model Comment {
id String @id @default(cuid())
content String
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
postId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)

createdAt DateTime @default(now())

@@map("comments")
}

model Like {
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
postId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)

@@unique([userId, postId])
@@map("likes")
}

model Bookmark {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
postId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)

createdAt DateTime @default(now())

@@unique([userId, postId])
@@map("bookmarks")
}

model Notification {
id String @id @default(cuid())
recipientId String
recipient User @relation("Recipient", fields: [recipientId], references: [id], onDelete: Cascade)
issuerId String
issuer User @relation("Issuer", fields: [issuerId], references: [id], onDelete: Cascade)
postId String?
post Post? @relation(fields: [postId], references: [id], onDelete: Cascade)
type NotificationType
read Boolean @default(false)

createdAt DateTime @default(now())

@@map("notifications")
}
User User @relation(fields: [userId], references: [id], onDelete: Cascade)

enum NotificationType {
LIKE
FOLLOW
COMMENT
@@map("sessions")
}
26 changes: 26 additions & 0 deletions src/app/(auth)/actions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<<<<<<< Updated upstream
"use server";

import { lucia, validateRequest } from "@/auth";
=======
"user server"
import { lucia, validateRequest } from "@/autho"
>>>>>>> Stashed changes
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export async function logout() {
<<<<<<< Updated upstream
const { session } = await validateRequest();

if (!session) {
Expand All @@ -23,3 +29,23 @@ export async function logout() {

return redirect("/login");
}
=======
const {session} = await validateRequest();

if (!session) {
throw new Error("Unauthorized");
}

await lucia.invalidateSession(session.id);

const sessionCookie = lucia.createBlankSessionCookie();

cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes,
);

return redirect("/login");
}
>>>>>>> Stashed changes
59 changes: 59 additions & 0 deletions src/app/(auth)/login/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use server"
import prisma from "@/lib/prisma";
import { loginSchema, LoginValues } from "@/lib/validation";
import { isRedirectError } from "next/dist/client/components/redirect";
import {verify} from "@node-rs/argon2";
import { lucia } from "@/autho";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export async function login(
credentials: LoginValues,
): Promise<{error: string}> {
try {
const {username, password} = loginSchema.parse(credentials)
const existingUser = await prisma.user.findFirst({
where: {
username: {
equals: username,
mode: "insensitive"
}
}
})
if (!existingUser || !existingUser.passwordHash) {
return{
error: "Incorrect username or password"
}
}

const validPassword = await verify(existingUser.passwordHash, password, {
memoryCost: 19456,
timeCost: 2,
outputLen: 32,
parallelism: 1,
})

if (!validPassword) {
return{
error: "Incorrect username or password"
}
}

const session = await lucia.createSession(existingUser.id,{});
const sessionCookie = lucia.createSessionCookie(session.id);
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes,
);

return redirect("/");

} catch (error) {
if (isRedirectError(error)) throw error;
console.error(error);
return {
error: "Something went wrong. Please try again.",
};
}
}
Loading