{"id":28383892,"url":"https://github.com/simple-login/passportjs-example","last_synced_at":"2025-08-03T22:38:28.602Z","repository":{"id":36744716,"uuid":"202014468","full_name":"simple-login/passportjs-example","owner":"simple-login","description":"Add social login support to a Node.js app using passport.js with OpenID Connect (OIDC) strategy","archived":false,"fork":false,"pushed_at":"2022-12-10T05:35:49.000Z","size":1127,"stargazers_count":6,"open_issues_count":4,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-18T21:01:33.337Z","etag":null,"topics":["oidc","passport","simplelogin"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simple-login.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-12T21:51:40.000Z","updated_at":"2025-03-07T11:37:38.000Z","dependencies_parsed_at":"2023-01-17T04:29:00.229Z","dependency_job_id":null,"html_url":"https://github.com/simple-login/passportjs-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/simple-login/passportjs-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simple-login%2Fpassportjs-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simple-login%2Fpassportjs-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simple-login%2Fpassportjs-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simple-login%2Fpassportjs-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simple-login","download_url":"https://codeload.github.com/simple-login/passportjs-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simple-login%2Fpassportjs-example/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266621697,"owners_count":23957673,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["oidc","passport","simplelogin"],"created_at":"2025-05-30T07:38:13.982Z","updated_at":"2025-08-03T22:38:28.594Z","avatar_url":"https://github.com/simple-login.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"A demo can be found at https://simple-login-passportjs-example.glitch.me\n\nFeel free to **Remix** it on https://glitch.com/~simple-login-passportjs-example 🙂\n\n# Step 1: Bootstrap\n\nCreate a folder for the project \n\n```bash\nmkdir passportjs-example \ncd passportjs-example \n```\n\nInstall express generator:\n\n\u003e npm install express-generator -g\n\nGenerate the project\n\n\u003e express -e .\n\nInstall all dependencies:\n\n\u003e npm i\n\nRun the project\n\n\u003e npm start\n\nOpen http://localhost:3000, you should see this empty page\n\n![](./docs/step-1.png)\n\n# Step 2: Bootstrap OpenID\n\nInstall `dotenv`\n\n\u003e npm install dotenv --save\n\nLoad `dotenv`, add the following lines on top of `app.js`:\n\n```js\nrequire('dotenv').config()\n```\n\nCreate the `.env` file based on the `.env.example` one:\n\n\u003e cp .env.example .env\n\nGet the OAuth-Client-Id and OAuth-Client-Secret from your SimpleLogin app and make sure to fill up the corresponding values in `.env`\n\nInstall `passport passport-openidconnect express-session`\n\n\u003e npm install passport@0.4.0 passport-openidconnect@0.0.2 express-session@1.15.6 --save\n\nAdd the following lines to `app.js` just below `var logger = require('morgan');` to import passport:\n\n```js\nvar session = require('express-session');\nvar passport = require('passport');\nvar OidcStrategy = require('passport-openidconnect').Strategy;\n```\n\nAt this step, `npm start` should still work and http://localhost:3000 is still this empty page.\n\n\n# Step 3: Config passport.js\n\nJust below `app.use(express.static(path.join(__dirname, 'public')));`, add the following line to init passport.js.\n\nThe first part is to config `session` for passport.js, please make sure to replace `very-secret` if you decide to deploy the code on production 😎. We also need to tell passport.js how to serialize/deserialize user from/to session.\n\nThe second part is to setup passport.js with OIDC endpoints and with the OAuth credential parsed from the `.env` file setup in previous step. \n\n```js\n// passportjs use session to store user info\napp.use(session({\n  secret: 'very-secret',\n  resave: false,\n  saveUninitialized: true\n}));\n\napp.use(passport.initialize());\napp.use(passport.session());\n\npassport.serializeUser((user, next) =\u003e {\n  next(null, user);\n});\n\npassport.deserializeUser((obj, next) =\u003e {\n  next(null, obj);\n});\n\n// config different OIDC endpoints\npassport.use('oidc', new OidcStrategy({\n  issuer: 'https://app.simplelogin.io',\n  authorizationURL: 'https://app.simplelogin.io/oauth2/authorize',\n  tokenURL: 'https://app.simplelogin.io/oauth2/token',\n  userInfoURL: 'https://app.simplelogin.io/oauth2/userinfo',\n  clientID: process.env.CLIENT_ID, // OAuth config from env thanks to dotenv\n  clientSecret: process.env.CLIENT_SECRET,\n  callbackURL: 'http://localhost:3000/authorization-code/callback',\n  scope: 'openid profile'\n}, (issuer, sub, profile, accessToken, refreshToken, done) =\u003e {\n  return done(null, profile);\n}));\n\n```\n\n# Step 4: OpenID Connect Flow\n\nThe social login starts by redirecting user to the Social Login Provider authorization page. Once user approves sharing their data with the app, user gets redirected back to the `callback` endpoint with a `code` in url that we'll use to exchange for `access token`. This `access token` will then allow us to get user information.\n\nWe would need the following routes:\n\n- `/login` for redirecting user to the authorization page\n- `/authorization-code/callback` to received the `code` when user is redirected back from authorization page.\n- (Optional) `/profile` page to show the obtained user information.\n\nLet's start by adding a \"Login\" button onto the home page: in `views/index.ejs`, add this line just before `\u003c/body\u003e`\n\n```\n\u003ca href=\"/login\"\u003eLog In\u003c/a\u003e\n```\n\nCreate a profile page to show user information. In the same terminal, create a `profile.ejs` file by `touch views/profile.ejs` and add this content to this file:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003ctitle\u003e\u003c%= title %\u003e\u003c/title\u003e\n    \u003clink rel='stylesheet' href='/stylesheets/style.css' /\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003ch1\u003e\u003c%= title %\u003e\u003c/h1\u003e\n    \u003cp\u003eWelcome \u003c%= user.name %\u003e!\u003c/p\u003e    \n    \u003cp\u003eEmail: \u003c%= user.email %\u003e\u003c/p\u003e\n    \u003cimg src=\"\u003c%= user.avatar_url %\u003e\"\u003e\n    \u003ca href=\"/\"\u003eHome\u003c/a\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nNow let's add the 3 endpoints `/login`, `/authorization-code/callback`, `/profile` just below the code we added in the previous step:\n\n```js\n// redirect user to authorization page\napp.use('/login', passport.authenticate('oidc'));\n\n// user is redirected back with the *code*\napp.use('/authorization-code/callback',\n  passport.authenticate('oidc', {\n    failureRedirect: '/error'\n  }),\n  (req, res) =\u003e {\n    // redirect user to /profile so they can see their information\n    res.redirect('/profile');\n  }\n);\n\n// show user info\napp.use('/profile', (req, res) =\u003e {\n  console.log(\"user:\", req);\n  res.render('profile', {\n    title: 'User Info',\n    user: req.user._json\n  });\n});\n```\n\nNow re-run `npm start`, http://localhost:3000 should show this login button and clicking on the login button should open the authorization page.\n\n![](./docs/step-4a.png)\n\nAuthorization page\n\n![](./docs/step-4b.png)\n\nUser gets redirected back to our app:\n\n![](./docs/step-4c.png)\n\nCongratulations, you just add social login to a Node.js app using passport.js with OpenID Connect strategy!\n\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimple-login%2Fpassportjs-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimple-login%2Fpassportjs-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimple-login%2Fpassportjs-example/lists"}