Socket
Book a DemoInstallSign in
Socket

cf-access-auth

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cf-access-auth

Authenticate your application using Cloudflare Access.

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Cloudflare Access Authenticator

Get User Email in your application protected via Cloudflare access and validate the user login.

How to use

  • Create a New Application on Cloudflare Teams Dashboard.
  • Configure your application to be protected with Cloudflare access
  • Visit your Protected Application
  • You will get the Auth Domain now which looks like https://XXXX.cloudflareaccess.com

Express JS Example


const cookieParser = require('cookie-parser')

const CFAccessAuth = require('cf-access-auth')
const cfAuthenticator = new CFAccessAuth({
    AuthDomain:'https://XXXX.cloudflareaccess.com',
})

// Required to Parse the Cookies
app.use(cookieParser())

const protectedRoute = (req, res, next) => {
    const cfCookieValue = req.cookies['CF_Authorization'];
    cfAuthenticator.Authenticate(cfCookieValue, (err, data)=>{
        
        if(err) {
            console.error('Cloudflare Access Error:', err)
            return res.status(403).send("Login Required")
        }

        if(data && data.UserData && data.UserData.email){
            req.UserData = data.UserData
            next();
        }
    })
}

// Private Route
app.get("/private", protectedRoute, (req, res)=>{
    const loggedInUser = req.UserData.email;
    res.send(`Private Route accessed by ${loggedInUser}`)
})

// Public Route
app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Keywords

cloudflare

FAQs

Package last updated on 09 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.