Skip to content

Commit e6285a0

Browse files
committed
Merge branch 'apis'
2 parents f13fec1 + 72d5075 commit e6285a0

File tree

11 files changed

+479
-33
lines changed

11 files changed

+479
-33
lines changed

server/models/OwnerSchema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const ownerSchema = new mongoose.Schema({
1414
phone: {
1515
type: String,
1616
required: true,
17-
unique: true,
17+
// unique: true,
1818
},
1919
gstin: {
2020
type: String,
2121
required: true,
22-
unique: true,
22+
// unique: true,
2323
},
2424
password: {
2525
type: String,

server/models/PersonSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const personSchema = new mongoose.Schema({
7474
recommends: [
7575
{
7676
employer: {
77-
type: String,
77+
type: String, // gstin
7878
required: false,
7979
},
8080
message: {

server/routes/auth.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ const jwt = require('jsonwebtoken');
66
const { Owner } = require("../models/OwnerSchema");
77
const { JWT_SECRET } = require('../constants/config');
88

9+
10+
function prepareJWT(owner){
11+
return jwt.sign(
12+
{
13+
_id: owner._id,
14+
restaurant_name: owner.restaurant_name,
15+
address: owner.address,
16+
phone: owner.phone,
17+
gstin: owner.gstin,
18+
},
19+
JWT_SECRET,
20+
{ expiresIn: '2d' }
21+
);
22+
}
23+
924
router.post('/api/auth/signup', async (req, res) => {
1025
try {
1126
// Check for mandatory fields
@@ -19,12 +34,35 @@ router.post('/api/auth/signup', async (req, res) => {
1934

2035
// Create new owner object
2136
const owner = new Owner(req.body);
37+
38+
console.log("New owner", owner);
2239

2340
// Save owner to database
2441
const savedOwner = await owner.save();
25-
26-
// Return success response with ID of new owner
27-
res.status(201).json({ message: 'New owner created successfully', id: savedOwner._id });
42+
43+
// Prepare the JWT with the owner's fields
44+
const token = prepareJWT(savedOwner);
45+
46+
// Send the JWT in a cookie
47+
// res.cookie('jwt', token, {
48+
// // httpOnly: true,
49+
// maxAge: 2 * 24 * 60 * 60 * 1000, // 2 days
50+
// sameSite: 'lax',
51+
// });
52+
53+
// Return a success response with the owner's fields
54+
return res.status(201).json({
55+
success: true,
56+
message: "New owner created successfully",
57+
profile: {
58+
_id: savedOwner._id,
59+
restaurant_name: savedOwner.restaurant_name,
60+
address: savedOwner.address,
61+
phone: savedOwner.phone,
62+
gstin: savedOwner.gstin,
63+
},
64+
token,
65+
});
2866
} catch (err) {
2967
// Handle errors
3068
console.error(err);
@@ -64,17 +102,7 @@ router.post('/api/auth/login', async (req, res) => {
64102
}
65103

66104
// Prepare the JWT with the owner's fields
67-
const token = jwt.sign(
68-
{
69-
_id: owner._id,
70-
restaurant_name: owner.restaurant_name,
71-
address: owner.address,
72-
phone: owner.phone,
73-
gstin: owner.gstin,
74-
},
75-
JWT_SECRET,
76-
{ expiresIn: '2d' }
77-
);
105+
const token = prepareJWT(owner);
78106

79107
// Send the JWT in a cookie
80108
// res.cookie('jwt', token, {

server/routes/sms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { sendOTP, verifyOTP } = require("../services/smsAPI");
55

66
// OTP routes
77
router.post("/apis/sendOTP", (req, res) => {
8+
console.log(req.body);
89
const phone = `+91${req.body.phone}`;
910

1011
if (!phone)
@@ -28,7 +29,6 @@ router.post("/apis/sendOTP", (req, res) => {
2829
message: "Server error, contact administrator",
2930
phone,
3031
success: false,
31-
err,
3232
});
3333
});
3434
});
@@ -38,7 +38,7 @@ router.post("/apis/verifyOTP", (req, res) => {
3838
const phone = `+91${req.body.phone}`;
3939
const code = req.body.code;
4040

41-
if (!phone || code.length != CODE_LENGTH)
41+
if (!phone || !code.length)
4242
return res.status(400).send({
4343
message: "Invalid phone number or code :(",
4444
success: false,

server/services/smsAPI.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ console.log("Twilio params", {
1111
const twilioClient = require('twilio')(TWILIO_ACCOUNT_SID, AUTH_TOKEN);
1212

1313
exports.sendOTP = (phone) => {
14-
console.log("Hello")
1514
return new Promise((resolve, reject) => {
1615
twilioClient.verify.v2.services(SERVICE_ID)
1716
.verifications
@@ -23,7 +22,7 @@ exports.sendOTP = (phone) => {
2322

2423
exports.verifyOTP = (phone, code) => {
2524
return new Promise((resolve, reject) => {
26-
client.verify.v2.services(SERVICE_ID)
25+
twilioClient.verify.v2.services(SERVICE_ID)
2726
.verificationChecks
2827
.create({to: phone, code: code})
2928
.then(resolve)

src/components/otpVerification.jsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1+
import axios from 'axios';
12
import React, { useState } from 'react'
23
import { Button } from 'flowbite-react'
34
import OtpInput from 'react-otp-input';
5+
import { toast } from 'react-toastify';
6+
import { SERVER_URL } from '../constants/config';
47

5-
const OTPVerification = () => {
8+
const OTPVerification = (props) => {
69
const [otp, setOtp] = useState('');
710

811
const verifyOTPHandler = () => {
9-
console.log(otp)
12+
console.log("Verify otp:", otp);
13+
// props.postVerification();
14+
15+
axios
16+
.post(`${SERVER_URL}/apis/verifyOTP`, { phone: props.phone, code: otp })
17+
.then((res) => {
18+
if(res.data.success){
19+
toast.success("Verified OTP successfuly !");
20+
props.postVerification();
21+
} else {
22+
toast.error("Invalid OTP !");
23+
}
24+
})
25+
// .catch((err) => {
26+
// console.error(err);
27+
// toast.error("Some error verifying OTP");
28+
// });
1029
}
1130

1231
return (
1332
<div className='flex flex-col mt-[40px]'>
1433
<div>
1534
<span className='text-[20px]'>We have sent you an OTP on</span><br/>
16-
<span className='text-gray-600 dark:text-gray-200 mt-[10px]'>+91 9137357003</span>
35+
<span className='text-gray-600 dark:text-gray-200 mt-[10px]'>+91 {props.phone}</span>
1736
</div>
1837

1938
<div className='flex flex-col justify-center items-center'>

src/constants/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const SERVER_URL = process.env.NEXT_APP_SERVER_URL || "http://localhost:5001";
1+
export const SERVER_URL = process.env.NEXT_APP_SERVER_URL || "http://localhost:5001";

0 commit comments

Comments
 (0)