-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforgot_pass.js
109 lines (55 loc) · 2.37 KB
/
forgot_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
97
98
99
100
101
// 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');
// var profile_schema = require("./schema/profile");
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)) ){
return { status:"error" , message:"missing data "};
}
json_data.email = json_data.email.trim();
if(json_data.email==""){
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 create_token(json_data) {
let model = mongoose.models["user_detail"] === undefined ? mongoose.model("user_detail",
user_detail_schema) : mongoose.model("user_detail");
let result = await model.findOne({ email: json_data.email },{u_id:1,expire_time:1});
if ( result) {
if(false && result.expire_time-600000 +60000 > Date.now()){
return {status:"error",message: "Reset Password link already sent to your Email"};
}
let token_string = crypto.randomBytes(25).toString('hex');
let token_num = Math.round((Math.random() * 1000000)).toString();
let result2 = await model.updateOne({ u_id: result.u_id , email: json_data.email},{token_str:token_string,token_no:token_num,expire_time:Date.now() + 600000});
if(result2.nModified== 1){
return {status:"ok", token_str: token_string,email:json_data.email,token_no:token_num }
}else{
return {status:"error",message: "token not created"};
}
}
else {
return {status:"error",message: "Email Not Found"};
}
}
async function main(data) {
connect_to_db();
let result= is_validate_data(data)
if (result.status=="error") { return result; }
result = await create_token(result);
return result;
}
module.exports = main;