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

[#38] [Backend] As a user I can login with an OAuth provider #79

Merged
merged 43 commits into from
May 25, 2023

Conversation

tednguyendev
Copy link
Contributor

@tednguyendev tednguyendev commented May 22, 2023

Close #38

What happened 👀

Insight 📝

Reuse old structure Next.js code

This PR mostly reuse the code from PR #2 for convenience.

NextAuth

When compare NextAuth to Passport, we can easily see that Passport is more battle-tested, support more oAuth providers, more popular(21.5k stars on Github, compared to 16.7k of NextAuth), but I see NextAuth is more suitable for our POC, since:

Proof Of Work 📹

  • Login flow:
CleanShot.2023-05-23.at.15.18.13-converted.mp4
  • Stored Cookie:
CleanShot 2023-05-23 at 15 18 57@2x

Copy link
Member

@olivierobert olivierobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While re-using existing code is a good idea, I am unsure if we should still keep using next-connect with Next.JS 13.

I used that package as 1) passport is THE battle-tested oAuth library in the Node community, and 2) API routes in Next.JS 12 (with the Pages router) were quite "poor" (we had to check on the HTTP verb, e.g., `if req == 'POST').

However, Next.JS 13 (with the App router) has better server-side components, including routes handlers. So we can do defining GET and POST endpoints with function GET() {} and function POST() {}. So I believe we can do more backend logic similarly to what we do with Express (see this article to do oAuth without even passport).

I am not against using https://next-auth.js.org/ if it is easier (e.g, easier to test) and better (e.g., more secure, bug-free). I want to make sure we understand our options.

"next": "13.4.3",
"next-connect": "0.13.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package is now outdated as there is a newer 1.0.0 version that has been in the works and released: https://github.com/hoangvvo/next-connect/releases/tag/v1.0.0

@tednguyendev
Copy link
Contributor Author

tednguyendev commented May 23, 2023

While re-using existing code is a good idea, I am unsure if we should still keep using next-connect with Next.JS 13.

I used that package as 1) passport is THE battle-tested oAuth library in the Node community, and 2) API routes in Next.JS 12 (with the Pages router) were quite "poor" (we had to check on the HTTP verb, e.g., `if req == 'POST').

However, Next.JS 13 (with the App router) has better server-side components, including routes handlers. So we can do defining GET and POST endpoints with function GET() {} and function POST() {}. So I believe we can do more backend logic similarly to what we do with Express (see this article to do oAuth without even passport).

I am not against using https://next-auth.js.org/ if it is easier (e.g, easier to test) and better (e.g., more secure, bug-free). I want to make sure we understand our options.

Yes I see.

I just updated the PR description. What do you think about this?

And there's also this one thing. When using passport authenticate, we are using it as authService.authenticate()(req, res), which needs both request and response, but since the syntax of Route Handlers is export async function GET(request: Request) {}, we don't have the response here. This is why I want to use the API Route temporarily for just this PR, which usePassport. Do you know if there are any other ways for us to do this if we still want to use Route Handlers?

And seems like it's indeed theNextAuth is easier to use with Route Handler .

The Nextjs and it's ecosystem are still new to me so I appreciate your help a lot :D.

@tednguyendev tednguyendev marked this pull request as ready for review May 23, 2023 09:27
@tednguyendev
Copy link
Contributor Author

tednguyendev commented May 23, 2023

Hi @olivierobert, I decided to go with the NextAuth and reimplemented the flow. Please help me re-review this POC 🚀

@tednguyendev tednguyendev self-assigned this May 24, 2023
@tednguyendev tednguyendev added this to the 0.3.0 milestone May 24, 2023
@tednguyendev tednguyendev force-pushed the feature/38-nextjs-v13-backend-google-oauth branch from 88ecdf6 to 1759569 Compare May 24, 2023 03:27
@tednguyendev tednguyendev force-pushed the feature/38-nextjs-v13-backend-google-oauth branch from 1759569 to 2884dbe Compare May 24, 2023 03:28
@tednguyendev tednguyendev requested a review from phisith May 24, 2023 03:44
nextjs-13/.env.sample Outdated Show resolved Hide resolved
Copy link
Member

@olivierobert olivierobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great overall 👍 Just two minor issues to fix before it can be merged.

nextjs-13/prisma/migrations/migration_lock.toml Outdated Show resolved Hide resolved
});

describe('given there is NO matching user', () => {
it('return null', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor typo issue:

Suggested change
it('return null', async () => {
it('returns null', async () => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just fixed it!

…om:nimblehq/js-framework-benchmark into feature/38-nextjs-v13-backend-google-oauth
@olivierobert olivierobert merged commit d20ea65 into develop May 25, 2023
@olivierobert olivierobert deleted the feature/38-nextjs-v13-backend-google-oauth branch May 25, 2023 04:54
olivierobert pushed a commit that referenced this pull request May 26, 2023
[#38] [Backend] As a user I can login with an OAuth provider (#79)

[#56] Create GitHub action CI workflow (#80)

[#54] Create GitHub Action CI workflow (#88)

setup unit testing with simple Test and setup GH action

fix testing setup and fix CI

fix CI

fix CI 2

fix CI 3

setup Remix auth for login by gmail, create url login and 403

Fix new request

add link break to the third-party
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Backend] As a user I can login with an OAuth provider
3 participants