Skip to content

PlusAuth Node.js starter project demonstrating basic API authorization using Express and Express Jwt.

Notifications You must be signed in to change notification settings

PlusAuth/plusauth-nodejs-backend-starter

Repository files navigation

PlusAuth NodeJS Express Backend Starter Project

Simple NodeJS Express project demonstrates basic API authorization flow with PlusAuth

Explore the PlusAuth Express API docs »

Table of Contents
  • About The Project
  • Prerequisites
  • Getting Started
  • Calling Endpoints
  • License
  • What is PlusAuth
  • About The Project

    This is a very simple NodeJS project demonstrating basic API authorization flows with PlusAuth. To keep things simple Express.js used as the server framework and jsonwebtoken used for authorization.

    Prerequisites

    Before running the project, you must first follow these steps:

    Create PlusAuth Account

    Create PlusAuth API

    • Go to Api's page and create a new Api.

    • Navigate to Permissions tab and create permissions below for your Api.

      • users:read
      • users:write
      • users:update
      • users:delete
    • Finally navigate to Authroized Clients tab, authorize your client and grant the permissions.

    Finally write down your api audience and tenant id for server configuration

    Getting Started

    First install dependencies

    $ npm install
    # or with yarn
    $ yarn install

    After that all you need to do is configuring the application. Rename .env.example file as just.env.

    Then configure the .env file using your api audience and PlusAuth tenant id.

    Now you can start the server:

    $ npm run start
    // or with yarn
    $ yarn start

    The example is hosted at http://localhost:3000/

    Calling Endpoints

    All endpoints are secured and requires access token in request header. You can make requests to following endpoints:

    Obtain an access token using command line or a REST Client with your PlusAuth Client and API properties.

    # bash
    
    curl --request POST \
      --url 'https://<YOUR_TANENT_ID>.plusauth.com/oauth2/token' \
      --header 'content-type: application/x-www-form-urlencoded' \
      --data 'grant_type=client_credentials' \
      --data 'client_id=<YOUR_CLIENT_ID>' \
      --data 'client_secret=<YOUR_CLIENT_SECRET>' \
      --data 'audience=<YOUR_AUDIENCE>' \
      --data 'scope=users:read users:write users:update users:delete'
      

    Create a GET request and pass the access token you obtained as Authorization header. You will get following response:

    # bash
    
    > curl -i http://localhost:3000/users \
    -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6ImF0K2p3dCIsImtpZCI6Inh4T3l2R0hWV3dCIsImtpZ..."
    HTTP/1.1 200 OK
    Access-Control-Allow-Origin: *
    Content-Type: text/html; charset=utf-8
    Content-Length: 14
    Connection: keep-alive
    Keep-Alive: timeout=5
    
    All Users List

    License

    This project is licensed under the MIT license. See the LICENSE file for more info.

    What is PlusAuth

    PlusAuth helps to individuals, team and organizations for implementing authorization and authentication system in a secure, flexible and easy way.

    Explore the PlusAuth Website »

    Explore the PlusAuth Docs »

    Explore the PlusAuth Forum »

    About

    PlusAuth Node.js starter project demonstrating basic API authorization using Express and Express Jwt.

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published