-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathver_reset_pass.js
85 lines (52 loc) · 2.01 KB
/
ver_reset_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
// 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)) ){
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 verify_reset_pass_token(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) {
json_data.token_no=json_data.token_no.trim();
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" } ;
}
return {status:"ok" , message: "verfied email successfully",token_no: result.token_no,token_str:result.token_str,email:result.email};
//
}
else {
return {status:"error" , message:"Verfication Failed" } ;
}
}
async function main(data) {
connect_to_db();
let result = await verify_reset_pass_token(data);
return result;
}
module.exports = main;