Passport Sendoso OAuth 2.0 Strategy

1.0.2 · maintenance · verified Wed Apr 22

This package provides a Passport authentication strategy specifically designed for integrating with Sendoso using the OAuth 2.0 protocol. It enables Node.js applications, particularly those leveraging Connect-style middleware such as Express, to authenticate users via their Sendoso accounts. The current stable version is 1.0.2, indicating a specific, potentially specialized or modified, integration rather than a general-purpose, high-cadence library. Its key differentiator is its direct focus on Sendoso's unique authentication flow, offering a structured way to connect Passport.js applications to Sendoso for user identity verification. Developers must provide a `clientID`, `clientSecret`, and `callbackURL` to configure the strategy, and a `verify` callback to handle user data after successful authentication. This package simplifies the OAuth 2.0 handshake for Sendoso, allowing Passport's robust session management and user serialization features to be applied.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure and use the Passport-Sendoso-postilize strategy with Express, including session management, authentication routes, and user serialization/deserialization.

const express = require('express');
const passport = require('passport');
const session = require('express-session'); // Required for Passport sessions
const { Strategy: SendosoStrategy } = require('passport-sendoso-postilize'); // Using destructuring for clarity

const app = express();

// Passport configuration
passport.use(new SendosoStrategy({
    clientID:     process.env.SENDOSO_CLIENT_ID ?? 'YOUR_SENDOSO_CLIENT_ID',
    clientSecret: process.env.SENDOSO_CLIENT_SECRET ?? 'YOUR_SENDOSO_CLIENT_SECRET',
    callbackURL:  "http://localhost:3000/auth/sendoso/callback",
    passReqToCallback: true
  },
  function(request, accessToken, refreshToken, profile, done) {
    // In a real application, you would typically find or create a user in your database
    // based on the profile information returned by Sendoso.
    // The 'profile' object would contain user details provided by Sendoso.
    // For demonstration, we'll return a placeholder user.
    const user = { id: profile?.id || 'sendoso_user_123', name: profile?.displayName || 'Sendoso User' };
    console.log('Sendoso Profile:', profile);
    console.log('Access Token:', accessToken);
    done(null, user); // Call done with null for error and the user object
  }
));

// Passport session setup.
//   To support persistent login sessions, Passport needs to be able to
//   serialize users into and deserialize users out of the session.
//   Typically, this will be as simple as storing the user ID when serializing
//   and finding the user by ID when deserializing.
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  // In a real application, retrieve user from database by ID
  const user = { id: id, name: 'Deserialized User' }; // Placeholder
  done(null, user);
});

// Middleware for Express
app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());

// Define authentication routes
app.get('/auth/sendoso',
  passport.authenticate('sendoso', { scope: 'profile email' } // Replace 'profile email' with actual Sendoso scopes if available
));

app.get('/auth/sendoso/callback',
  passport.authenticate('sendoso', {
    successRedirect: '/profile', // Redirect to a profile page on success
    failureRedirect: '/login'    // Redirect to login on failure
  })
);

app.get('/profile', (req, res) => {
  if (req.isAuthenticated()) {
    res.send(`<h1>Welcome, ${req.user.name || 'authenticated user'}!</h1><pre>${JSON.stringify(req.user, null, 2)}</pre><p><a href="/logout">Logout</a></p>`);
  } else {
    res.redirect('/login');
  }
});

app.get('/login', (req, res) => {
  res.send('<h1>Login with Sendoso</h1><p><a href="/auth/sendoso">Login with Sendoso</a></p>');
});

app.get('/logout', (req, res, next) => {
  req.logout((err) => {
    if (err) { return next(err); }
    res.redirect('/login');
  });
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log('Visit http://localhost:3000/login to start the authentication flow.');
});

view raw JSON →