Giter Site home page Giter Site logo

simple-login / passportjs-example Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 2.0 1.1 MB

Add social login support to a Node.js app using passport.js with OpenID Connect (OIDC) strategy

License: MIT License

JavaScript 85.93% CSS 2.02% HTML 12.06%
passport oidc simplelogin

passportjs-example's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

msgpo serenalong

passportjs-example's Issues

TypeError: done is not a function

Preface

I am very excited to integrate my first (and probably only) non-local strategy in passport.js though I am having a small issue that has been bugging me for a while. I checked the official SimpleLogin docs to find instructions to integrate SIWSL and it seemed pretty straightforward until I encountered the error TypeError: done is not a function. I was able to reach the SimpleLogin auth page and logged in but that is where the fun ended. Once reaching the "http:localhost:3000/authorization-code/callback?state=..." route I get that error. This has not been tested in a production environment yet as this error is holding me back.

Can't wait to get this up and running, thank you SimpleLogin team for all the hard work.

My troublesome code

const express = require("express");
const config = require('dotenv').config({ path: __dirname + "/.env" });
const bodyParser = require("body-parser");
var _ = require('lodash');

// MODULES
const mongoose = require(__dirname + "/modules/schemas.js").mongoose;
let schemas = require(__dirname + "/modules/schemas.js");

// Authentication
const passport = require('passport');
const passportLocalMongoose = require('passport-local-mongoose');
const OidcStrategy = require('passport-openidconnect').Strategy;

// Start App
const app = express();

const session = require('express-session');
const sess = {
	secret: process.env.CRYPT_KEY,
	resave: false,
	saveUninitialized: false,
	cookie: {}
}

if (app.get('env') === 'production') {
	app.set('trust proxy', 1) // trust first proxy
	sess.cookie.secure = true // serve secure cookies
}

// APP.LISTEN
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ... ` + port));

// APP.USE
app.use(bodyParser.urlencoded({ extended: true }));

// Creating the model
const Entry = mongoose.model("Entry", journalEntrySchema);
const Comment = mongoose.model("Comment", entryCommentSchema);
const User = mongoose.model("User", userSchema);

// Initialize Passport & Session
app.use(session(sess));
app.use(passport.initialize());
app.use(passport.session());

// Create User Strategy for Passport
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

// SimpleLogin Integration
passport.use('oidc', new OidcStrategy({
	// SimpleLogin OIDC Settings
	issuer: 'https://app.simplelogin.io',
	authorizationURL: 'https://app.simplelogin.io/oauth2/authorize',
	tokenURL: 'https://app.simplelogin.io/oauth2/token',
	userInfoURL: 'https://app.simplelogin.io/oauth2/userinfo',
	clientID: process.env.CLIENT_ID,
	clientSecret: process.env.CLIENT_SECRET,
	// you might need to change the callbackURL when deploying on production
	callbackURL: 'http://localhost:3000/authorization-code/callback',
	// openid needs to be in scope
	scope: 'openid profile',
}, (issuer, sub, profile, accessToken, refreshToken, done) => {
	return done(null, profile);
//       ^^^^^^^^^--------------------- ERROR on this line :/
}));
// redirect user to authorization page
app.use('/continue-with-simplelogin', passport.authenticate('oidc'));
// user is redirected back with the *code*
app.use('/authorization-code/callback',
	passport.authenticate('oidc', {
		failureRedirect: '/error'
	}), (req, res) => {
		var user = req.user._json
		res.send(`
    		Welcome ${user.name}! <br>
    		Your email: ${user.email} <br>
    		Avatar: <img src="${user.avatar_url}">
    	`)
	}
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.