Skip to content

Conversation

@flacial
Copy link
Member

@flacial flacial commented Feb 13, 2023

Motivation

At present, we do not have a streamlined approach to prevent unauthorized access to pages that require the user to be logged in. The current implementation involves manual checks on the session to determine if the user needs to be redirected to the login page or not.

Solution

Introduce next-auth middleware to handle the protection of pages that require the user to be logged in. The middleware will maintain a list of protected paths and in case the user is not logged in, they will be immediately redirected to the /login page. This not only simplifies the codebase by removing the redundant code for page protection but also saves bandwidth for the user.

Changes

Testing

  1. Log out of the application
  2. Access the /settings/account page and verify if it redirects to /login
  3. Log in to the application by filling in the login form
  4. Verify if it redirects to /settings/account after successful login

Additional Info

In future pull requests, more pages such as /review/[lessonSlug] will be added to the list of protected pages.

Related issues

@vercel
Copy link

vercel bot commented Feb 13, 2023

@flacial is attempting to deploy a commit to the c0d3-prod Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Feb 13, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
c0d3-app ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 20, 2023 at 6:02PM (UTC)

@codecov
Copy link

codecov bot commented Feb 13, 2023

Codecov Report

Merging #2756 (839237b) into master (38342fd) will not change coverage.
The diff coverage is 100.00%.

❗ Current head 839237b differs from pull request most recent head a92623b. Consider uploading reports for the commit a92623b to get more accurate results

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2756   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          189       190    +1     
  Lines         3562      3566    +4     
  Branches       985       986    +1     
=========================================
+ Hits          3562      3566    +4     
Impacted Files Coverage Δ
pages/api/auth/[...nextauth].ts 100.00% <ø> (ø)
constants/index.ts 100.00% <100.00%> (ø)
middleware.ts 100.00% <100.00%> (ø)
pages/login.tsx 100.00% <100.00%> (ø)

@SlyBouhafs
Copy link
Member

I noticed there are some hardcoded paths in the new changes, maybe consider using the constants instead?

@flacial
Copy link
Member Author

flacial commented Feb 20, 2023

@SlyBouhafs Thanks for reviewing the PR :)

I fixed most of the issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants