-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update ssh-deploy.yml * Update ssh-deploy.yml * ethvjti apis (#222) --------- Co-authored-by: Shubhankar Kanchan Gupta <shubhankar.gupto.11@gmail.com>
- Loading branch information
1 parent
3fa059d
commit f624922
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const EthUser = require("../models/EthVJTIWallet"); | ||
const sendEmail = require("../utility/sendEmail"); | ||
|
||
const generateOTP = () => { | ||
var digits = "0123456789"; | ||
let OTP = ""; | ||
for (let i = 0; i < 4; i++) { | ||
OTP += digits[Math.floor(Math.random() * 10)]; | ||
} | ||
return OTP; | ||
}; | ||
|
||
const sendMailToSingerUser = async (otp, email) => { | ||
const mailSubject = "EthVJTI Launch NFT One Time Password"; | ||
const mailData = `Your OTP for EthVJTI Launch NFT is: ${otp}`; | ||
await sendEmail(email, mailSubject, mailData); | ||
}; | ||
|
||
const sendOTP = async (req, res) => { | ||
try { | ||
const { email, walletAddress } = req.body; | ||
const newOTP = generateOTP(); | ||
|
||
sendMailToSingerUser(newOTP, email); | ||
|
||
const userDetails = { | ||
email, | ||
walletAddress, | ||
isMinted: false, | ||
emailVerificationOTP: newOTP, | ||
}; | ||
|
||
const userCheck = await EthUser.findOne({ | ||
email: email, | ||
}) | ||
.lean(); | ||
|
||
let newUser; | ||
if(userCheck){ | ||
if(userCheck.isMinted){ | ||
return res.status(400).json({ error: "NFT is already minted for this user" }); | ||
} | ||
newUser = await EthUser.findByIdAndUpdate( | ||
userCheck._id, | ||
userDetails, | ||
{new: true} | ||
); | ||
}else{ | ||
res.status(404).json({error: "Email ID was not registered"}); | ||
} | ||
|
||
return res | ||
.status(200) | ||
.json({ | ||
message: `OTP Sent to ${email}`, | ||
user: newUser, | ||
}); | ||
} catch (error) { | ||
return res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
const verifyOTP = async (req, res) => { | ||
try { | ||
const { email, otp } = req.body; | ||
|
||
const userCheck = await EthUser.findOne({ | ||
email: email, | ||
}) | ||
.lean(); | ||
if(userCheck && userCheck.emailVerificationOTP == otp){ | ||
await EthUser.findByIdAndUpdate(userCheck._id, {isMinted: true, emailVerificationOTP: ""}); | ||
return res.status(200).json({ message: "OTP Correct", email: email }); | ||
} | ||
return res.status(400).json({ error: "OTP not valid" }); | ||
} catch (error) { | ||
return res.status(400).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
sendOTP, | ||
verifyOTP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const ethuser = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: true | ||
}, | ||
walletAddress: { | ||
type: String, | ||
required: false | ||
}, | ||
isMinted: { | ||
type: Boolean, | ||
required: false, | ||
default: false, | ||
}, | ||
emailVerificationOTP: { | ||
type: String, | ||
required: false | ||
} | ||
}) | ||
|
||
ethuser.index({ email: 1 }) | ||
|
||
const EthUser = mongoose.model('ethuser', ethuser) | ||
|
||
module.exports = EthUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters