An open API service indexing awesome lists of open source software.

https://github.com/passport/todos-express-twitter

Express 4.x app using Passport for sign in with Twitter.
https://github.com/passport/todos-express-twitter

express passport twitter

Last synced: 8 days ago
JSON representation

Express 4.x app using Passport for sign in with Twitter.

Awesome Lists containing this project

README

        

This example illustrates how to use [Express](https://expressjs.com) 4.x and
[Passport](https://www.passportjs.org) to sign users in with [Twitter](https://twitter.com).
Use this example as a starting point for your own web applications.

## Quick Start

To get started with this example, clone the repository and install the
dependencies.

```bash
$ git clone [email protected]:passport/express-4.x-twitter-example.git
$ cd express-4.x-twitter-example
$ npm install
```

This example requires credentials from Twitter, which can be obtained by
[creating](https://developer.twitter.com/en/docs/apps/overview) an app in the
developer portal's [App page](https://developer.twitter.com/en/apps). The
callback URL of the app should be set to: `http://localhost:3000/oauth/callback/twitter.com`

Once credentials have been obtained, create a `.env` file and add the following
environment variables:

```
TWITTER_CONSUMER_KEY={{INSERT_API_KEY_HERE}}
TWITTER_CONSUMER_SECRET={{INSERT_API_SECRET_KEY_HERE}}
```

Start the server.

```bash
$ npm start
```

Navigate to [`http://localhost:3000`](http://localhost:3000).

## Overview

This example illustrates how to use [Passport](https://www.passportjs.org) and
the [`passport-twitter`](https://www.passportjs.org/packages/passport-twitter/)
strategy within an [Express](https://expressjs.com) application to sign users in
with [Twitter](https://twitter.com).

The example builds upon the scaffolding created by [Express generator](https://expressjs.com/en/starter/generator.html),
and uses [EJS](https://ejs.co) as a view engine and plain CSS for styling. This
scaffolding was generated by executing:

```
$ express --view ejs express-4.x-twitter-example
```

The example uses [SQLite](https://www.sqlite.org) for storing user accounts.
SQLite is a lightweight database that works well for development, including this
example.

Added to the scaffolding are files which add authentication to the application.

* [`boot/db.js`](boot/db.js)

This file initializes the database by creating the tables used to store user
accounts and credentials.

* [`boot/auth.js`](boot/auth.js)

This file initializes Passport. It configures the Twitter strategy and
supplies the serialization functions used for session management.

* [`routes/auth.js`](routes/auth.js)

This file defines the routes used for authentication. In particular, there
are three routes used to authenticate with Twitter:

- `GET /login`

This route renders a page that prompts the user to sign in with Twitter.

- `GET /login/federated/twitter.com`

This route begins the authentication sequence by redirecting the user to
Twitter.

- `POST /oauth/callback/twitter.com`

This route completes the authentication sequence when Twitter redirects the
user back to the application. When a new user signs in, a user account is
automatically created and their Twitter account is linked. When an existing
user returns, they are signed in to their linked account.

## License

[The Unlicense](https://opensource.org/licenses/unlicense)

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)