23
loading...
This website collects cookies to deliver better user experience
We are going to build an app like TwiterStats.
npx create-next-app -e with-tailwindcss twitter-count
cd twitter-count
and start the dev server using yarn dev
command. You can see the below page if you hit http://localhost:3000
in the browser.next auth
to your project using the below commandyarn add next-auth
[…nextauth].js
in pages/api/auth
folder and add the below codeauthToken
and authSecret
to the client-side, so we save them in JWT token objects. Then we can access the user credential on the server-side using the getToken
helper method.secret
a random string used to hash tokens, sign or encrypt cookies and generate cryptographic keys.useSession
hooks to get session data in child components as belowrefetchInterval
is used to fetch the session periodically in the background._app.js
is the top-level component, so we have wrapped the session provider. Here we have wrapped ThemeProvide context from next-theme to enable dark mode support. /api/twitter/user
API to get basic details of the Twitter user such as name, followers count, profile description and location. We have used SWR to get the data from the API in an interval of time.api/*
are considered as API endpoints. Which are processed on the server, not on the client-side. Twitter APIs can be accessed from the server-side only, so we have a user API in the api/
folder to access the show API using the Twitter lite package.pages/api/twitter/user.js
to access the user details using /api/twitter/userAPI
.index.js
file as below.signIn
and signOut
the method from next-auth
to initiate OAuth login. To log in with Twitter we pass Twitter param to the sign-in method as belowsignIn('twitter');
signIn
method, the app will be redirected to the Twitter OAuth page and clicking the Authorize App button on OAuth Page will redirect back to our followers component as below image.We need to configure the OAuth redirect URL in Twitter Developer Portal when registering.