Thanks for visiting my GitHub account!

- VS Code, Download ->https://code.visualstudio.com/download
- Node, Download-> https://nodejs.org/en/download
- MongoDB Shell(msi) , Download-> https://www.mongodb.com/try/download/shell
- MongoDB Compass (msi), Download-> https://www.mongodb.com/try/download/community
- Postman, Download-> https://www.postman.com/downloads/
Or Online Database (MongoDB Atlas)
- Register -> https://www.mongodb.com/cloud/atlas/register
-
Install Node.js
-
To verify installation into command form by node -v
-
For initialization npm write the query in the command window as npm init -y
-
Setup the opening file into the package.json and change the file with main:'server.js'
-
To create a server using the express package then write a query into the command window as npm install express. Write code in the server file for initialization const express = require("express"); const app = express(); app.listen(3000, () => { console.log("Server is running at http://localhost:3000"); });
-
Install the nodemon package for automatically running the server as- npm i --save-dev nodemon (For Developing purpose)
-
setup the package.json file in the scripts key, write "scripts": { "start": "node ./resources/backend/server.js", "dev": "nodemon ./resources/backend/server.js", "test": "echo "Error: no test specified" && exit 1" },
-
use the Morgan package for automatic restart. Hence install the morgan package as npm install --save-dev morgan (Development purpose) Write code in the server file for initialization const morgan = require("morgan"); app.use(morgan("dev")); --> Middlewire.
-
Install Postman software for API testing by the URL endpoint.
-
Install Mongobd + MongobdCompass and Mongoshell (For Database)
- Install Mondodb + Mongodb Compass and Mongodb Shell download from the google.
- Set up Environment Variable in drive:c/program file
- Create a directory in the base path of the c drive named data. Inside the data directory create another folder db.
- Write the command in the CMD window as Mongod. And write the other command in the other CMD window as mongosh.
- Then Check the version as mongod --version and mongosh --version.
- Install mongoose package as npm i mongoose
- Create an atlas account. In the atlas account create a cluster that have a user(as atlas admin) and network access with any access IP address.
- Connect the database using URL from the atlas cluster or local Mongodb compass using the mongoose package as mongoose. connect('mongodb://localhost:27017/database-name);
- Step-1: In the Frontend site
import React, { useEffect } from "react";
import GoogleLogin from "react-google-login";
import { gapi } from "gapi-script";
import { loginWithGoogle } from "../services/AuthService";
const Google = () => {
const responseGoogle = async (response) => {
try {
// 1. get the tokenId from response
console.log(response.tokenId);
// 2. send the tokenId to the server
const result = await loginWithGoogle({ idToken: response.tokenId });
console.log("Google signin success: ", result);
} catch (error) {
console.log("Google signin error: ", error.response.data.message);
}
};
useEffect(() => {
function start() {
gapi.client.init({
clientId: process.env.REACT_APP_CLIENT_ID,
scope: "email",
});
}
gapi.load("client:auth2", start);
}, []);
return (
<div className="pb-3">
<GoogleLogin
clientId={`${process.env.REACT_APP_CLIENT_ID}`}
render={(renderProps) => (
<button
onClick={renderProps.onClick}
disabled={renderProps.disabled}
className="btn btn-danger btn-lg btn-block"
>
<i className="fab fa-google p-2"></i>Login With Google
</button>
)}
onSuccess={responseGoogle}
onFailure={responseGoogle}
cookiePolicy={"single_host_origin"}
/>
</div>
);
};
export default Google;- Step-2: In the
services/AuthServicepath
export const loginWithGoogle = async (data) => {
const response = await axios.post(
`http://localhost:3030/api/google-login`,
data
);
return response.data;
};-
Step-3: In the Server site
- install
npm install google-auth-library
- install
// google
authRoutes.post("/google-login", handleGoogleLogin);
const { OAuth2Client } = require("google-auth-library");
const jwt = require("jsonwebtoken");
const handleGoogleLogin = async (req, res) => {
try {
// creating client which help us to verify the idToken that we receive from front end
const client = new OAuth2Client(dev.app.googleClientId);
// get the idToken form react app request body
const { idToken } = req.body;
console.log("idToken ", idToken);
// lets verify the idToken
client
.verifyIdToken({ idToken, audience: dev.app.googleClientId })
.then(async (response) => {
console.log("GOOGLE LOGIN RESPONSE", response);
const { email_verified, name, email } = response.payload;
const exsitingUser = await User.findOne({ email });
if (email_verified) {
// if the user already exist in our website
if (exsitingUser) {
console.log("user exist");
// create a token for user
const token = jwt.sign(
{ _id: exsitingUser._id },
String(dev.app.jwtSecretKey),
{
expiresIn: "7d",
}
);
// step 7: create user info
const userInfo = {
_id: exsitingUser._id,
name: exsitingUser.name,
email: exsitingUser.email,
phone: exsitingUser.phone,
isAdmin: exsitingUser.isAdmin,
};
return res.json({ token, userInfo });
} else {
// if user does not exist create a new user
let password = email + dev.app.jwtSecretKey;
const newUser = new User({
name,
email,
password,
});
// console.log(newUser);
const userData = await newUser.save();
if (!userData) {
return res.status(400).send({
message: "user was not created with google",
});
}
// if user is created
const token = jwt.sign(
{ _id: userData._id },
String(dev.app.jwtSecretKey),
{
expiresIn: "7d",
}
);
// step 7: create user info
const userInfo = {
_id: userData._id,
name: userData.name,
email: userData.email,
phone: userData.phone,
isAdmin: userData.isAdmin,
};
return res.json({ token, userInfo });
}
} else {
return res.status(400).send({
message: "Google login failed try again",
});
}
});
} catch (error) {
res.send({
message: error.message,
});
}
};- Step-4: Setup .env file in the server folder
SERVER_PORT=8080
MONGODB_URL=
JWT_ACCOUNT_ACTIVATION_KEY=
JWT_RESET_PASSWORD_KEY=
JWT_ACCESS_TOKEN_KEY=
JWT_REFRESH_TOKEN_KEY=
SMTP_USERNAME=YOUR_GMAIL_HERE
SMTP_PASSWORD=
CLIENT_URL=
SESSION_SECRET=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
- Step-5: Setup for secret key
require("dotenv").config();
const dev = {
db: {
mongoURL:
process.env.MONGODB_URL || "mongodb://127.0.0.1:27017/database-name",
},
app: {
port: process.env.SERVER_PORT || 8000,
jwtAccountActivationKey: process.env.JWT_ACCOUNT_ACTIVATION_KEY,
jwtResetPasswordKey: process.env.JWT_RESET_PASSWORD_KEY,
jwtAcessTokenKey: process.env.JWT_ACCESS_TOKEN_KEY,
jwtRefreshTokenKey: process.env.JWT_REFRESH_TOKEN_KEY,
smtpUsername: process.env.SMTP_USERNAME,
smtpPassword: process.env.SMTP_PASSWORD,
clientUrl: process.env.CLIENT_URL,
googleClientId: process.env.GOOGLE_CLIENT_ID,
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
},
};
module.exports = dev;