Skip to content

Twitter Authentication using Passport.js enhanced with the ability to store sessions in MongDB

Notifications You must be signed in to change notification settings

01231/passport-twitter-authentication

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twitter Authentication using Passport.js and MongDB

Netlify Status Live Demo

About

This is a simple web application that supports "Login with Twitter" via Twitter OAuth. Sessions are Stored in MongoDB.

The API is deployed on Heroku and the Frontend on Netlify.

Get started

  1. Create a developer account on Twitter (must have elevated access)
  2. Add http://localhost:4000/auth/redirect to the Redirect URL section in Twitter
  3. Rename .env.copy file to .env and adjust your credentials
  4. Start frontend (http://localhost:3000)
cd client/
npm install
npm run dev
  1. Start backend (http://localhost:4000) and DB

    Make sure you got the local/live DB set up an adjusted the credentials and paths ind the .env and server/config/globals.js file. You may need to update the cluster name in the prodMongoURL connection link.

cd server/
npm install
# Use local MongoDB
npm run db
npm run dev
# or use production MongoDB
npm run start

Helpful Commands for using the local MongoDB

mongosh # connect to deployed MongoDB
> show dbs # show all available db's
> use userDB # (your name may be different)
> db.sessions.find() # list all sessions
> db.users.find() # list all users

Deployment

  1. Add https://your-heroku-app.herokuapp.com/auth/redirect to the Redirect URL section in Twitter
  2. Adjust the Heroku and Netlify links in server/config/globals to your apps

Netlify deployment

Settings Value
Base directory client/
Build command npm run build
Publish directory client/build

Heroku deployment

  • set the following environment variables:
    • COOKIE_KEY
    • DB_PASSWORD
    • DB_USER
    • TWITTER_CONSUMER_KEY
    • TWITTER_CONSUMER_SECRET
    • PROJECT_PATH = server
  • add the buildpacks heroku/nodejs and https://github.com/timanovsky/subdir-heroku-buildpack

Stack

  • MongoDB
    • mongoose
  • Express.js
  • React
  • Node.js
  • Passport.js

Languages

  • JavaScript 94.9%
  • HTML 5.1%