-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_pass.js
104 lines (60 loc) · 2.49 KB
/
new_pass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// const { json } = require("express");
require('dotenv').config();
const mongoose = require("mongoose");
const validator = require("validator");
var link = process.env.DB_LINK;
var crypto = require("crypto");
var user_detail_schema = require("./schema/user_detail");
const { json } = require('body-parser');
const { join } = require('path');
function connect_to_db() {
mongoose.connect(link, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true }).catch(error => { });
}
function is_validate_data(json_data) {
if((!json_data)|| (!(json_data.email)) || (!json_data.password) ){
return { status:"error" , message:"missing data "};
}
json_data.email = json_data.email.trim();
json_data.password = json_data.password.trim()
if(json_data.email=="" || json_data.password ==""){
return { status:"error" , message:"missing data "};
}
else if (!validator.isEmail(json_data.email)) {
return { status:"error" , message:"Enter a valid email"};
}
return json_data;
}
async function insert_new_password(json_data) {
let model = mongoose.models["user_detail"] === undefined ? mongoose.model("user_detail",
user_detail_schema) : mongoose.model("user_detail");
var result;
if (json_data.token_no) {
result = await model.findOne({ email: json_data.email, token_no: json_data.token_no });
} else if (json_data.token_str) {
result = await model.findOne({ email: json_data.email, token_str: json_data.token_str });
}
if (result ) {
if(result.expire_time <Date.now()){
return {status:"error" , message:"Link Expired" } ;
}
let result2 = await model.updateOne({ email: json_data.email, token_str: json_data.token_str },{"$set":{token_str: crypto.randomBytes(25).toString('hex'),token_no:Math.round((Math.random() * 1000000)).toString(),password:json_data.password}});
if(result2){
return {status:"ok" , message: "Successfully Update your Password"};
}else{
return {status:"error" , message:"Not updated Password" } ;
}
;
//
}
else {
return {status:"error" , message:"Not updated Password" } ;
}
}
async function main(data) {
connect_to_db();
let result = is_validate_data(data);
if (result.status=="error") { return result; }
result = await insert_new_password(result);
return result;
}
module.exports = main;