Author: Reuven Chachashvili
Please run on node version 6.9.0+, npm version 3+
- Clone the repository:
git clone https://github.com/Reuvenc/twitter-oauth-api-usage.git
. - Install dependencies by running
npm install
. - Paste the provided tweeter credentials into
twitter-credentials.json
. - Run
npm run build
- will build the client side application bundle. - Run
npm start
- at the end of the process the application will be available throughhttp://localhost:3000/
.
In order to achieve the requirements described in the home assignment, I used the following:
- Frontend - React application, generated by create-react-app library, and Material-UI components.
- Backend - Node.js, using Express.js framework.
The application provides two main abilities:
- Sign in using twitter oAuth.
- When logged in, Fetch #ai related latest tweets (the values are predefined for the demonstration).
The frontend application is composed of the following:
Components:
- App - Top level component.
- Header - Header at the top the page.
- TweetsList - A wrapper for a collection of received tweets.
- Tweet - Component that treats single tweet.
- Login - A minimal login component that has 'sign in with twitter' button.
Services:
- Api - A wrapper for communication against the server. fetch library is used in order to perform ajax calls in a convenient way.
For state management, redux is used. The state is composed of two main entities:
- user - user information, whether the user is logged in or not, and user name.
- tweets - holds the list of the fetched tweets, and an indication of whether the tweets were already fetched or not.
As mentioned, the server side is built using Express.js framework.
The application consists of the following:
- Express application (
app.js
) - the main server application. - Configuration (
config.js
) - a configuration file which holds shared configuration values. - Routes (
routes.js
) - Given express application, the method in routes will attach the required routes:/
- Application entry point, will serveindex.html
template./twitter/tweets
- Using Twitter api described below, returns #ai related latest tweets./login
- Begins login process/logout
- Logs out the user- Static content path is also configured.
- Twitter API wrapper (
twitter.js
) - A wrapper to tweeter api, which provides methods that are required across the application. - Authentication Middleware (
middleware/authentication.js
) - Provides authentication related middleware methods:- Verify user logged in
- Applying user info to a request.
- Getting request token for beginning login process in front of Twitter API.
**Although there are existing packages, such as passport.js, for handling authentication, for demonstration purposes I chose to implement it by myself.
The server implements twitter oauth sign in flow as described in their docs. Twitter api wrapper provides the methods required for the three steps of the authentication. The flow is as follows:
When /login
endpoint is accessed:
AuthMiddleware.getRequestToken()
middleware is triggered, resulting in request token and the corresponding secret (step 1).- The response is redirected to twitter sign in page, using the request token granted from the previous step (step 2)
- After user signs in
/auth/callback
route is triggered by Twitter, providing user credentials (step 3) - User information is saved in cookies, and the server redirects the response back to the page.
When /logout
is accessed, all the user information stored in httpOnly cookies is deleted, and the user considered as logged out.