2022-03-23
2683
#node
Diogo Souza
18542
Mar 23, 2022 â‹… 9 min read

How to implement OAuth 2.0 in Node.js

Diogo Souza Brazilian dev. Creator of altaluna.com.br

Recent posts:

Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 â‹… 10 min read
Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 â‹… 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 â‹… 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 â‹… 11 min read
View all posts

12 Replies to "How to implement OAuth 2.0 in Node.js"

  1. HI , I getting “OAuth2Error: User credentials are invalid” . I am sure that both user name and password correct

  2. I hope it can help somebody, to test http://localhost:3000/test/hello you must use postman 7.36.1 and the code to test it is
    curl –location –request POST ‘http://localhost:3000/test/hello’ \
    –header ‘Authorization: Bearer 9bc8aefc932074166cada3cb1234705824e6e6ad’

    put token that you get in the previous step.

  3. This is a string injection security vulnerability:

    ___________
    function getUserIDFromBearerToken(bearerToken, cbFunc) {
    const getUserIDQuery = `SELECT * FROM access_tokens WHERE access_token = ‘${bearerToken}’;`;
    ___________

    Don’t use this code. Parameterized SQL queries are the reliable way to escape a string.

  4. Hey!

    Thank you for your effort and time writing this post. However, the post is a kind of bad example of OAuth 2.0 protocol that we find on the web. We should notice that OAuth 2.0 is an authorization protocol, not an authentication one, although many developers try to use it for that purpose.

    You can find the spec here: https://oauth.net/2/. Some useful links to visit:

    * https://dev.to/kimmaida/authorization-and-authentication-for-everyone-27j3
    * https://newbedev.com/why-is-it-a-bad-idea-to-use-plain-oauth2-for-authentication
    * https://auth0.com/docs/authorization/protocols/protocol-oauth2
    * https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2
    * https://dev.to/bam92/oauth-2-spec-is-misunderstood-why-14jd

    1. The effort you did to create this content is considerable but it’s really important to take note of this:

      “This implements the password credential grant. The error should be JSON as well (currently HTML). Please note, that the password credential grant is not the recommended way to do OAuth – consider using the authorization code grant.”

  5. I tried to rewrite for MySQL. I reated user successfully but I had not had any success with the “/login” route:
    OAuth2Error: User credentials are invalid
    at OAuth2Error (d:\web-work\oa2\logrocket-oauth2-example\node_modules\node-oauth2-server\lib\error.js:30:12)
    at d:\web-work\oa2\logrocket-oauth2-example\node_modules\node-oauth2-server\lib\grant.js:228:19
    at d:\web-work\oa2\logrocket-oauth2-example\db\userDB.js:29:9
    at Query.onResult (d:\web-work\oa2\logrocket-oauth2-example\db\pgWrapper.js:37:9)
    at d:\web-work\oa2\logrocket-oauth2-example\node_modules\mysql2\lib\commands\query.js:86:16
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

  6. OAuth2Error: Method must be POST with application/x-www-form-urlencoded encoding
    I want to send payload as Content-Type: application/json

Leave a Reply