Subversion Repositories SmartDukaan

Rev

Rev 34815 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23343 ashik.ali 1
var googleProfile;
24383 amit.gupta 2
var partnerName;
25085 amit.gupta 3
 
24383 amit.gupta 4
$(function() {
23343 ashik.ali 5
 
25089 amit.gupta 6
	$('form').on('submit', function() {
25090 amit.gupta 7
		if($(this).find("#password").length > 0) {
25085 amit.gupta 8
			login();
9
		} else {
35458 amit 10
			var email = $("#email").val();
11
			if(!email || email.trim() === '') {
12
				alert("Input field email can't be empty");
13
				return false;
14
			}
25375 amit.gupta 15
			forgetPassword(email);
23343 ashik.ali 16
		}
25085 amit.gupta 17
		return false;
24383 amit.gupta 18
	});
25085 amit.gupta 19
 
27754 amit.gupta 20
	$(document).on('click', ".forgetPassword", function() {
24383 amit.gupta 21
		$("#auth").toggle();
22
		$('#forget-password').toggle();
23
	});
24
});
23343 ashik.ali 25
 
25085 amit.gupta 26
function login() {
27
	var emailIdOrMobileNumber = $("#emailormobile").val();
28
	var password = $("input[type='password']").val();
35458 amit 29
	if (!emailIdOrMobileNumber || emailIdOrMobileNumber.trim() === '') {
30
		alert("Input field emailIdOrMobileNumber can't be empty");
25085 amit.gupta 31
		return;
32
	}
35458 amit 33
	if (!password || password.trim() === '') {
34
		alert("Password can't be empty");
35
		return;
36
	}
37
	submitUser(null, emailIdOrMobileNumber, password);
25085 amit.gupta 38
}
39
 
33061 ranu 40
function onSignIn(googleUser) {
41
	var profile = googleUser.getBasicProfile();
42
	submitUser(googleUser, profile.getEmail(), null);
43
}
44
 
33085 ranu 45
function onLoad() {
46
	gapi.load('auth2', function () {
47
		gapi.auth2.init();
48
	});
49
}
50
 
51
// Function to handle sign-out
52
function signOut() {
53
	var auth2 = gapi.auth2.getAuthInstance();
54
	auth2.signOut().then(function () {
55
		console.log('User signed out.');
56
		// Add your custom logic for sign-out here
57
	});
58
}
59
 
60
// Function to toggle between sign-in and sign-out
61
function toggleSignInSignOut() {
62
	var auth2 = gapi.auth2.getAuthInstance();
63
	if (auth2.isSignedIn.get()) {
64
		// User is signed in, sign them out
65
		signOut();
66
	} else {
67
		// User is not signed in, initiate sign-in
68
		auth2.signIn().then(onSignIn);
69
	}
70
}
71
 
24383 amit.gupta 72
function submitUser(googleUser, emailIdOrMobileNumber, password) {
73
 
74
	if (googleUser) {
75
		var params = {
76
			"token" : googleUser.getAuthResponse().id_token,
25245 amit.gupta 77
			"emailIdOrMobileNumber" : emailIdOrMobileNumber,
24383 amit.gupta 78
			"password" : ""
79
		};
80
	} else {
81
		var params = {
82
			"token":"",
83
			"emailIdOrMobileNumber" : emailIdOrMobileNumber,
84
			"password" : password
85
		};
86
	}
87
	// var response = doPostAjaxRequestWithParams(context+"/login", params);
88
	doAjaxRequestWithParamsHandler(context + "/login", "POST", params,
89
			function(response) {
90
				response = JSON.parse(response);
91
				if (response.status) {
92
					if(response.name)
93
						{
94
						partnerName=response.name;
95
						}
96
					addProfileInLocalDb();
32390 amit.gupta 97
					if (!!window.location.search) {
98
						const urlParams = new URLSearchParams(window.location.search);
99
						if (urlParams.get("redirect") !== null) {
100
							window.location.href = `${context}${urlParams.get("redirect")}`;
101
							return;
102
						}
103
					}
24383 amit.gupta 104
					window.location.href = response.redirectUrl;
105
				} else {
106
					alert("Retailer not found, Please login");
107
				}
108
			});
109
 
110
}
35458 amit 111
function forgetPassword(emailId) {
112
	var params = {"emailId": emailId};
34815 vikas 113
	doAjaxRequestWithParamsHandler(context + "/forgetPassword", "POST", params, function(response) {
35458 amit 114
		if(response === "true") {
34815 vikas 115
			alert("Your password has been sent to your email, Please login");
35458 amit 116
			setTimeout(function() { window.location.reload(); }, 5000);
34815 vikas 117
		}
23343 ashik.ali 118
	});
119
}
120
 
24383 amit.gupta 121
function addProfileInLocalDb() {
35458 amit 122
	var profile;
123
	if(googleProfile) {
124
		profile = {
125
			'image_url': googleProfile.getImageUrl(),
126
			'name': googleProfile.getName()
127
		};
128
	} else {
129
		profile = {
130
			'image_url': "",
131
			'name': partnerName
132
		};
133
	}
24383 amit.gupta 134
	localStorage.setItem("profile", JSON.stringify(profile));
35458 amit 135
}