This is a NodeJS application which uses Google OAuth to authenticate users and fetches attachments from their Gmail account based on the search query. The attachments are downloaded on the server and the unique attachment id is stored in the MongoDB database. We can then download the attachment by providing the attachment id.
- Create a new cluster in MongoDB to store user and email data. Copy the connection string and add the
MONGO_URI
to the.env
file
- Go to Google Cloud Console and create a new project
- Enabling Google API
- In sidemenu options, select
APIs and Services
->Library
-> Search forGmail API
and enable it
- In sidemenu options, select
- Setting up the OAuth Screen
- Inside
APIs and Services
->OAuth Consent Screen
-> SelectUser Type
(SelectExternal
for testing) and click onCreate
- Now enter all the application details and click
Save and Continue
- Inside
Scopes
section ->Add Scopes
-> Seach forGmail API
and select the/auth/gmail.readonly
scope. This gives our app access to read the user's emails ->Save and Continue
- If your application is still in
Testing
phase and you selectedExternal
user type in Step 1, you'll have to provide email ids of all the users who can access your app ->Save and Continue
- Check the app summary and click
Save
- Inside
- Generating Credentials
- Again in
APIs and Services
, selectCredentials
->Create Credentials
->oAuth Client ID
- Select
Application Type
(Web Application), add authorized origin (Usehttp://localhost:3000
if you don't have a Domain) and a callback URL where Google will send the response after OAuth (http://localhost:3000/auth/google/callback
in our case). Also add this asCALLBACK_URL
in the.env
file - Save you client id and secret as
GOOGLE_CLIENT_ID
andGOOGLE_CLIENT_SECRET
in the.env
file
- Again in
- Clone the github repo
- Open terminal, go to the directory and run
npm i
- Make sure followed all the above steps and added all the variable as specified in the
.sample.env
inside the.env
file - Run
npm run dev
which will start a Nodejs server atlocalhost:3000
Please feel free to reach out to me at [email protected]
in case you have any queries.