Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploying new signup flow #31

Merged
merged 16 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 54 additions & 31 deletions controllers/authController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import dotenv from "dotenv";
import mongoose from "mongoose";
dotenv.config();
import { cognito } from "../config/awsConfig.js";

import axios from "axios";
import jwt from "jsonwebtoken";

import {
AdminDeleteUserCommand,
AdminGetUserCommand,
AdminInitiateAuthCommand,
AdminRespondToAuthChallengeCommand,
ConfirmSignUpCommand,
Expand Down Expand Up @@ -35,24 +38,30 @@ const createVendor = async (req, res) => {
res.status(500).json({ error: error.message });
}
};

const getVendor = async (req, res) => {
try {
let { email, phone } = req.body;
let { email, vendorId, mobile } = req.body;

if (!email && !phone) {
return res.status(400).json({ message: "Email or phone is required" });
if (!email && !mobile && !vendorId) {
return res
.status(400)
.json({ message: "Please provide at least one detail to get vendor." });
}

let user;
if (!email) {
phone = "+91" + phone;
user = await User.findOne({ phone });
} else if (!phone) {
if (vendorId) {
user = await User.findOne({ id: vendorId });
} else if (email) {
user = await User.findOne({ email });
} else if (mobile) {
mobile = "+91" + mobile;
user = await User.findOne({ mobile });
}

if (!user) {
return res.status(404).json({ message: "User not found" });
return res.status(404).json({ message: "User not found." });
}

res.status(200).json(user);
} catch (error) {
res.status(500).json({ error: error.message });
Expand All @@ -72,18 +81,31 @@ const signUp = async (req, res) => {
};

try {
const user = await userExists(`+91${mobile}`);
const getUserCommand = new AdminGetUserCommand({
UserPoolId: process.env.COGNITO_USER_POOL_ID,
Username: `+91${mobile}`,
});
var user = await cognito.send(getUserCommand);

if (user) {
const deleteCommand = new AdminDeleteUserCommand({
UserPoolId: process.env.COGNITO_USER_POOL_ID,
Username: `+91${mobile}`,
});
await cognito.send(deleteCommand);
}
var user = await userExists(`${mobile}`);

if (user !== null) {
return res.status(400).json({ message: "User already exists" });
}
const command = new SignUpCommand(params);
const data = await cognito.send(command);
await cognito.send(command);
login(req, res);
// console.log(data);

// res.status(200).json({ message: "OTP sent", data });
} catch (error) {
res.status(400).json({ error: error.message });
if (error.name !== "UserNotFoundException") {
res.status(400).json({ error: error.message });
}
}
};

Expand All @@ -110,7 +132,7 @@ const login = async (req, res) => {
};

const verifyLoginOtp = async (req, res) => {
const { mobile, code, session } = req.body;
const { mobile, code, session, name } = req.body;

const params = {
ChallengeName: "CUSTOM_CHALLENGE",
Expand All @@ -128,8 +150,17 @@ const verifyLoginOtp = async (req, res) => {

try {
const command = new AdminRespondToAuthChallengeCommand(params);
const data = await cognito.send(command);

var data = await cognito.send(command);
const user = await User.findOne({ mobile: `+91${mobile}` });
if (!user) {
const newUser = new User({
name,
mobile,
});
var userData = await newUser.save();
data = { ...data, userData };
return res.status(200).json({ message: "Vendor registered", data });
}
res.status(200).json({ message: "Login Success", data });
} catch (error) {
res.status(400).json({ error: error.message });
Expand Down Expand Up @@ -225,19 +256,11 @@ const googleCallback = async (req, res) => {
}
};

const userExists = async (phoneNumber) => {
const params = {
UserPoolId: process.env.COGNITO_USER_POOL_ID,
Filter: `phone_number="${phoneNumber}"`,
};

try {
const command = new ListUsersCommand(params);
const data = await cognito.send(command);
return data.Users && data.Users.length > 0;
} catch (error) {
throw error;
}
const userExists = async (credential) => {
const user = await User.findOne({
$or: [{ email: credential }, { mobile: credential }],
});
return user;
};

const addBusinessDetails = async (req, res) => {
Expand Down
2 changes: 2 additions & 0 deletions controllers/queryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const createQuery = async (req, res) => {
const query = new Query({
fullname,
email,
services,
city,
message,
});

Expand Down
2 changes: 1 addition & 1 deletion controllers/razorpay.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const verifyPayment = async (req, res) => {

const generatedSignature = crypto
.createHmac("sha256", key_secret)
.update(order_id + "|" + payment_id) // Use the variables directly
.update(order_id + "|" + payment_id)
.digest("hex");
console.log(generatedSignature);
console.log(signature);
Expand Down
8 changes: 8 additions & 0 deletions models/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const querySchema = new Schema({
type: String,
required: true,
},
services: {
type: [String],
required: true,
},
city: {
type: String,
required: true,
},
message: {
type: String,
required: true,
Expand Down
2 changes: 1 addition & 1 deletion models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Schema = _Schema;
const vendorSchema = new Schema({
id: { type: String, default: generateUniqueId("ven"), required: true },
name: { type: String, required: true },
phone: { type: String },
mobile: { type: String },
email: { type: String },
businessDetails: businessSchema,
});
Expand Down
9 changes: 5 additions & 4 deletions routes/emailRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import nodemailer from "nodemailer";
const router = express.Router();

router.post("/send-email", async (req, res) => {
const { fullName, email, message } = req.body;
const { fullName, email, message, services, city } = req.body;

try {
// Create a transporter object using SMTP transport
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
Expand Down Expand Up @@ -79,6 +78,8 @@ router.post("/send-email", async (req, res) => {
<div class="content">
<p><strong>Sender's Name:</strong> ${fullName}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Services:</strong> ${services.join(", ")}</p>
<p><strong>City:</strong> ${city}</p>
<p><strong>Message Details:</strong></p>
<div class="message-box">
<p>${message}</p>
Expand Down Expand Up @@ -187,9 +188,9 @@ router.post("/send-email", async (req, res) => {

let userMailOptions = {
from: `"Eventory Support" <${process.env.RECEIVER_EMAIL}>`,
to: email, // Send to the user's email
to: email,
subject: "Thank You for Your Query",
html: userEmailBody, // Set the HTML body
html: userEmailBody,
};

// Send email to the user
Expand Down