![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
trailpack-proxy-passport
Advanced tools
Trailpack to allow passport authentification for Proxy Engine application
The Proxy Passport is built to be used on Trailsjs with Proxy Engine.
Repo | Build Status (edge) |
---|---|
trailpack-sequelize |
Repo | Build Status (edge) |
---|---|
trailpack-express |
With yo :
npm install -g yo generator-trails
yo trails:trailpack trailpack-proxy-passport
With npm (you will have to create config file manually):
npm install --save trailpack-proxy-passport
First you need to add this trailpack to your main configuration :
// config/main.js
module.exports = {
...
packs: [
...
require('trailpack-proxy-passport'),
...
]
...
}
You need to add passportInit
and optionally passportSession
:
// config/web.js
middlewares: {
order: [
'addMethods',
'cookieParser',
'session',
'passportInit',
'passportSession',
'bodyParser',
'methodOverride',
'router',
'www',
'404',
'500'
]
}
And to configure passport:
// config/passport.js
'use strict'
const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const EXPIRES_IN_SECONDS = 60 * 60 * 24
const SECRET = process.env.tokenSecret || 'mysupersecuretoken';
const ALGORITHM = 'HS256'
const ISSUER = 'localhost'
const AUDIENCE = 'localhost'
module.exports = {
redirect: {
login: '/',//Login successful
logout: '/'//Logout successful
},
bcrypt: require('bcryptjs'), // custom bcrypt version if you prefer the native one instead of full js
//Called when user is logged, before returning the json response
onUserLogin: (req, app, user) => {
return Promise.resolve(user)
},
onUserLogout: (req, app, user) => {
return Promise.resolve(user)
},
//Optional: can be used to merge data from all third party profiles and the default user properties.
mergeThirdPartyProfile: (user, profile) => {
const mergedProfile = {
email: user.email,
gender: profile.gender
}
return Promise.resolve(mergedProfile)
},
strategies: {
jwt: {
strategy: JwtStrategy,
tokenOptions: {
expiresInSeconds: EXPIRES_IN_SECONDS,
secret: SECRET,
algorithm: ALGORITHM,
issuer: ISSUER,
audience: AUDIENCE
},
options: {
secretOrKey: SECRET,
issuer: ISSUER,
audience: AUDIENCE,
jwtFromRequest: ExtractJwt.fromAuthHeader()
}
},
local: {
strategy: require('passport-local').Strategy,
options: {
usernameField: 'username' // If you want to enable both username and email just remove this field
}
}
/*
twitter : {
name : 'Twitter',
protocol : 'oauth',
strategy : require('passport-twitter').Strategy,
options : {
consumerKey : 'your-consumer-key',
consumerSecret : 'your-consumer-secret'
}
},
facebook : {
name : 'Facebook',
protocol : 'oauth2',
strategy : require('passport-facebook').Strategy,
options : {
clientID : 'your-client-id',
clientSecret : 'your-client-secret',
scope : ['email'] // email is necessary for login behavior
}
},
google : {
name : 'Google',
protocol : 'oauth2',
strategy : require('passport-google-oauth').OAuth2Strategy,
options : {
clientID : 'your-client-id',
clientSecret : 'your-client-secret'
}
}
github: {
strategy: require('passport-github').Strategy,
name: 'Github',
protocol: 'oauth2',
options: {
clientID : 'your-client-id',
clientSecret : 'your-client-secret',
callbackURL: 'your-app-url' + '/auth/google/callback',
scope: [
'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/plus.profile.emails.read'
]
}
}*/
}
}
Then make sure to include the new file in config/index.js
//config/index.js
...
exports.passport = require('./passport')
Further documentation on passport-jwt config can be found at themikenicholson/passport-jwt
Now you can apply some policies to control sessions under config/policies.js
ViewController: {
helloWorld: [ 'Passport.sessionAuth' ]
}
or
ViewController: {
helloWorld: [ 'Passport.jwt' ]
}
By default auth routes doesn't have prefix, but if you use trailpack-footprints
it automatically use footprints prefix to match your API. You can change this prefix by setting config.proxyPassport.prefix
.
You can register or log users with third party strategies by redirect the user to :
http://localhost:3000/auth/{provider}
example github
http://localhost:3000/auth/github
For adding a new user you can make a POST to auth/local/register
with at least this fields : username
(or email
) and password
.
For local authentication you have to POST credentials to /auth/local
in order to log the user.
If you want to disconnect a user from a provider you can call :
http://localhost:3000/auth/{provider}/disconnect
example if a user don't want to connect with github anymore
http://localhost:3000/auth/github/disconnect
Just make a GET to auth/logout
FAQs
Trailpack to allow passport authentification for Proxy Engine application
We found that trailpack-proxy-passport demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.