Skip to content

Commit

Permalink
Update cookie handling for sessionToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Eprince-hub committed Sep 27, 2024
1 parent 0a946a7 commit 81fbe1d
Show file tree
Hide file tree
Showing 11 changed files with 2,149 additions and 1,973 deletions.
9 changes: 5 additions & 4 deletions app/(auth)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { getSafeReturnToPath } from '../../../util/validation';
import LoginForm from './LoginForm';

type Props = {
searchParams: {
searchParams: Promise<{
returnTo?: string | string[];
};
}>;
};

export default async function LoginPage({ searchParams }: Props) {
export default async function LoginPage(props: Props) {
// Task: Add redirect to home if user is logged in
const searchParams = await props.searchParams;

// 1. Checking if the sessionToken cookie exists
const sessionCookie = cookies().get('sessionToken');
const sessionCookie = (await cookies()).get('sessionToken');

// 2. Check if the sessionToken cookie is still valid
const session = sessionCookie && (await getValidSession(sessionCookie.value));
Expand Down
2 changes: 1 addition & 1 deletion app/(auth)/logout/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function logout() {
// Task: Implement the user logout workflow

// 1. Get the session token from the cookie
const cookieStore = cookies();
const cookieStore = await cookies();

const session = cookieStore.get('sessionToken');

Expand Down
2 changes: 1 addition & 1 deletion app/(auth)/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async function RegisterPage() {
// Task: Add redirect to home if user is logged in

// 1. Checking if the sessionToken cookie exists
const sessionCookie = cookies().get('sessionToken');
const sessionCookie = (await cookies()).get('sessionToken');

// 2. Check if the sessionToken cookie is still valid
const session = sessionCookie && (await getValidSession(sessionCookie.value));
Expand Down
4 changes: 2 additions & 2 deletions app/animals/[animalId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Link from 'next/link';
import { getAnimalInsecure } from '../../../database/animals';

type Props = {
params: { animalId: string };
params: Promise<{ animalId: string }>;
};

export default async function AnimalPage(props: Props) {
const animal = await getAnimalInsecure(Number(props.params.animalId));
const animal = await getAnimalInsecure(Number((await props.params).animalId));

if (!animal) {
return (
Expand Down
2 changes: 1 addition & 1 deletion app/animals/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async function Dashboard() {
// Task: Protect the dashboard page and redirect to login if the user is not logged in

// 1. Checking if the sessionToken cookie exists
const sessionCookie = cookies().get('sessionToken');
const sessionCookie = (await cookies()).get('sessionToken');
// 2. Check if the sessionToken cookie is still valid
const session = sessionCookie && (await getValidSession(sessionCookie.value));
// 3. If the sessionToken cookie is invalid or doesn't exist, redirect to login with returnTo
Expand Down
4 changes: 2 additions & 2 deletions app/api/graphql/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const resolvers: Resolvers = {
throw new GraphQLError('Sessions creation failed');
}

cookies().set({
(await cookies()).set({
name: 'sessionToken',
value: session.token,
httpOnly: true,
Expand Down Expand Up @@ -235,7 +235,7 @@ const resolvers: Resolvers = {
throw new GraphQLError('Sessions creation failed');
}

cookies().set({
(await cookies()).set({
name: 'sessionToken',
value: session.token,
httpOnly: true,
Expand Down
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function RootLayout({
// Task: Protect the dashboard page and redirect to login if the user is not logged in

// 1. Checking if the sessionToken cookie exists
const sessionCookie = cookies().get('sessionToken');
const sessionCookie = (await cookies()).get('sessionToken');

// 2. Get the current logged in user from the database using the sessionToken value
const user = sessionCookie && (await getUser(sessionCookie.value));
Expand Down
11 changes: 7 additions & 4 deletions app/notes/[noteId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ import Link from 'next/link';
import { getNote } from '../../../database/notes';

type Props = {
params: {
params: Promise<{
noteId: string;
};
}>;
};

export default async function NotePage(props: Props) {
// Task: Restrict access to the note page only to the user who created the note

// 1. Checking if the sessionToken cookie exists
const sessionTokenCookie = cookies().get('sessionToken');
const sessionTokenCookie = (await cookies()).get('sessionToken');

// 2. Query the note with the session token and noteId
const note =
sessionTokenCookie &&
(await getNote(sessionTokenCookie.value, Number(props.params.noteId)));
(await getNote(
sessionTokenCookie.value,
Number((await props.params).noteId),
));

// 3. If there is no note for the current user, show restricted access message
if (!note) {
Expand Down
2 changes: 1 addition & 1 deletion app/notes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function NotesPage() {
// Task: Restrict access to the notes page and only display notes belonging to the current logged in user

// 1. Checking if the sessionToken cookie exists
const sessionTokenCookie = cookies().get('sessionToken');
const sessionTokenCookie = (await cookies()).get('sessionToken');

// 2. Query user with the sessionToken
const user = sessionTokenCookie && (await getUser(sessionTokenCookie.value));
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"bcrypt": "^5.1.1",
"dotenv-safe": "^9.1.0",
"graphql": "^16.8.2",
"next": "15.0.0-canary.37",
"next": "15.0.0-canary.171",
"postgres": "^3.4.4",
"react": "19.0.0-rc.0",
"react-dom": "19.0.0-rc.0",
Expand All @@ -41,13 +41,14 @@
"@types/react": "^18",
"@types/react-dom": "^18",
"concurrently": "^8.2.2",
"eslint-config-upleveled": "^8.2.3",
"libpg-query": "^15.2.0",
"eslint": "^9.11.1",
"eslint-config-upleveled": "^8.7.1",
"libpg-query": "^16.2.0",
"prettier": "^3.3.2",
"prettier-plugin-embed": "^0.4.15",
"prettier-plugin-sql": "^0.18.0",
"stylelint": "^16.6.1",
"stylelint-config-upleveled": "^1.1.3",
"stylelint-config-upleveled": "^1.1.4",
"typescript": "^5"
},
"packageManager": "[email protected]+sha512.ee7b93e0c2bd11409c6424f92b866f31d3ea1bef5fbe47d3c7500cdc3c9668833d2e55681ad66df5b640c61fa9dc25d546efa54d76d7f8bf54b13614ac293631"
Expand Down
Loading

0 comments on commit 81fbe1d

Please sign in to comment.