FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

frontegg-samples/nodejs-sample: Sample on how to protect your Node.JS routes using Frontegg nodejs SDK · GitHub

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting Started with Frontegg Node.JS authentication middleware

This sample provides samples on how to protect your Node.JS application with Frontegg authentication middleware.

Importing the Frontegg Node.JS withAuthentication middleware

const { withAuthentication, ContextHolder } = require('@frontegg/client');

Setting your clientId and apiKey on the context holder

ContextHolder.setContext({
    FRONTEGG_CLIENT_ID: '<YOUR_CLIENT_ID>',
    FRONTEGG_API_KEY: '<YOUR_API_KEY>',
});

Making all routes to be protected with authentication

// We are protecting ALL routes from this point and require them to be authenticated
app.use(withAuthentication());

Authorizing routes by role

app.use('/admin', withAuthentication({ roles: ['admin']}));

Authorizing routes by permission

app.post('/upload', withAuthentication({ permissions: ['file.upload']}), (req, res) => {
    res.status(200).send('uploaded file');
});

Running the sample

After cloning the project, install it using

yarn

In order to run the project, run

yarn start

The application will be opened on http://localhost:8080 in development mode.

Running few samples

Public route should be accessible without authentication.

curl -I http://localhost:8080/public

Private route should require authentication

curl -I http://localhost:8080/private

Admins route should require admin role

curl -I http://localhost:8080/admin

Upload route should require file.upload permission

curl -I http://localhost:8080/upload

About

Sample on how to protect your Node.JS routes using Frontegg nodejs SDK

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages


Back | FazBrowse Home | New Git URL