-
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.
* wip Co-authored-by: DamianUduevbo <[email protected]> * post merge * lin:ws * remove sessions from select * reorder * clean up some tests * mock * fixes * add cookies to tests * add mockClear between tests * woo * fix * cookies tests * one more test * middleware tests * ensure seed is working * remove todo * clean up test * userWithSesion -> userWithSession --------- Co-authored-by: DamianUduevbo <[email protected]> Co-authored-by: DamianUduevbo <[email protected]>
- Loading branch information
1 parent
67a3646
commit 9265908
Showing
25 changed files
with
650 additions
and
247 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
2 changes: 2 additions & 0 deletions
2
apps/db/prisma/migrations/20241025232323_name_required/migration.sql
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,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ALTER COLUMN "name" SET NOT NULL; |
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 |
---|---|---|
@@ -1,89 +1,119 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
import { hashPassword } from "@good-dog/auth/password"; | ||
|
||
const prisma = new PrismaClient(); | ||
async function main() { | ||
const alice = await prisma.user.upsert({ | ||
// Alice has a session that expires in the future | ||
await prisma.user.upsert({ | ||
where: { email: "[email protected]" }, | ||
update: {}, | ||
update: { | ||
sessions: { | ||
update: { | ||
where: { id: "23" }, | ||
data: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
}, | ||
}, | ||
}, | ||
create: { | ||
id: "7", | ||
email: "[email protected]", | ||
name: "Alice", | ||
password: "alicePasswod", | ||
}, | ||
}); | ||
const aliceSession = await prisma.session.upsert({ | ||
where: { id: "23" }, | ||
update: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
create: { | ||
userId: alice.id, | ||
id: "23", | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
hashedPassword: await hashPassword("alicePassword"), | ||
sessions: { | ||
create: { | ||
id: "23", | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const bob = await prisma.user.upsert({ | ||
// Bob has a session that expired in the past and another that expires in the future | ||
await prisma.user.upsert({ | ||
where: { email: "[email protected]" }, | ||
update: {}, | ||
update: { | ||
sessions: { | ||
updateMany: [ | ||
{ | ||
where: { id: "12" }, | ||
data: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() - 1), | ||
), | ||
}, | ||
}, | ||
{ | ||
where: { id: "45" }, | ||
data: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
create: { | ||
id: "9", | ||
email: "[email protected]", | ||
name: "Bob Jones", | ||
password: "bobPassword", | ||
}, | ||
}); | ||
const bobSession1 = await prisma.session.upsert({ | ||
where: { id: "12" }, | ||
update: { | ||
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)), | ||
}, | ||
create: { | ||
userId: bob.id, | ||
id: "12", | ||
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)), | ||
}, | ||
}); | ||
const bobSession2 = await prisma.session.upsert({ | ||
where: { id: "45" }, | ||
update: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
create: { | ||
userId: bob.id, | ||
id: "45", | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
hashedPassword: await hashPassword("bobPassword"), | ||
sessions: { | ||
createMany: { | ||
data: [ | ||
{ | ||
id: "12", | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() - 1), | ||
), | ||
}, | ||
{ | ||
id: "45", | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const eve = await prisma.user.upsert({ | ||
// Eve has a session that expired in the past | ||
await prisma.user.upsert({ | ||
where: { email: "[email protected]" }, | ||
update: {}, | ||
update: { | ||
sessions: { | ||
update: { | ||
where: { id: "78" }, | ||
data: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() - 1), | ||
), | ||
}, | ||
}, | ||
}, | ||
}, | ||
create: { | ||
id: "56", | ||
email: "[email protected]", | ||
name: "Eve Smith", | ||
password: "evePassword", | ||
}, | ||
}); | ||
const eveSession = await prisma.session.upsert({ | ||
where: { id: "78" }, | ||
update: { | ||
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)), | ||
}, | ||
create: { | ||
userId: eve.id, | ||
id: "78", | ||
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)), | ||
hashedPassword: await hashPassword("evePassword"), | ||
sessions: { | ||
create: { | ||
id: "78", | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() - 1), | ||
), | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
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,21 @@ | ||
import { cookies } from "next/headers"; | ||
|
||
export const SESSION_COOKIE_NAME = "sessionId"; | ||
|
||
export const getSessionCookie = () => { | ||
return cookies().get(SESSION_COOKIE_NAME); | ||
}; | ||
|
||
export const setSessionCookie = (sessionId: string, expires: Date) => { | ||
cookies().set(SESSION_COOKIE_NAME, sessionId, { | ||
httpOnly: true, | ||
secure: true, | ||
sameSite: "lax", | ||
path: "/", | ||
expires, | ||
}); | ||
}; | ||
|
||
export const deleteSessionCookie = () => { | ||
cookies().delete(SESSION_COOKIE_NAME); | ||
}; |
File renamed without changes.
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
Oops, something went wrong.