-
Notifications
You must be signed in to change notification settings - Fork 70
feat: Add next-auth middleware to protect pages #2756
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
feat: Add next-auth middleware to protect pages #2756
Conversation
|
@flacial is attempting to deploy a commit to the c0d3-prod Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Codecov Report
@@ 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
|
|
I noticed there are some hardcoded paths in the new changes, maybe consider using the constants instead? |
|
@SlyBouhafs Thanks for reviewing the PR :) I fixed most of the issues. |
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-authmiddleware 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/loginpage. This not only simplifies the codebase by removing the redundant code for page protection but also saves bandwidth for the user.Changes
next-authmiddleware to protect the/settings/accountpage/settings/account/loginfor unauthenticated userssecretoption in favor of usingNEXTAUTH_SECRETin Vercel's project environment variablesmiddleware.tsfileTesting
/settings/accountpage and verify if it redirects to/login/settings/accountafter successful loginAdditional Info
In future pull requests, more pages such as
/review/[lessonSlug]will be added to the list of protected pages.Related issues