Raj Posted January 19, 2018 Share Posted January 19, 2018 Dear All, Request your assistance on building the Sign In form, my query might be very silly but I am trying from past few days to get this working. Basically, I am calling the LoginPersist module to log the user in, to utilize the persistent cookie setting; however, once logged in I want to check the user role and redirect them to respective page accordingly. I am stuck in referring the $user instance from LoginPersist module to perform this check. Please help... Login page ------------------------ <?php namespace ProcessWire; ob_start(); require_once('/var/www/bluemine/index.php'); require("./ip.php"); ob_end_clean(); if($session->CSRF->hasValidToken()) { $input = wire('input'); $sanitizer = wire('sanitizer'); $config = wire('config'); $session = wire('session'); $ip = ip2long($ipaddress); if ($input->post && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { $rte = array(); $remme = $input->post->rememberme; if($remme == 'rememberme'){ $persist = wire('modules')->get('LoginPersist')->persist(); $uname = $persist->$session->$name; $rte['msg4'] = 'persistent'; if ($uname->hasRole('superuser')) { $rte['msg'] = 'admin'; $rte['route'] = '/admin/access/users/'; } elseif ($uname->hasRole('mine')) { $rte['msg'] = 'member'; $rte['route'] = '/phpconf/'; } else { $rte['msg'] = 'Not Activated'; } } else { if(!$user->isLoggedin()) { $uname = $sanitizer->username($input->post->lname); $pass = $input->post->lpswd; $rte['msg4'] = 'non_persistent'; try { $u = $session->login($uname, $pass); if($u && $u->id) { if ($u->hasRole('superuser')) { //$session->redirect('/admin/access/users/'); $rte['msg'] = 'admin'; $rte['route'] = '/admin/access/users/'; } elseif ($u->hasRole('mine')) { //$session->redirect('/profile/'); $rte['msg'] = 'member'; $rte['route'] = '/phpconf/'; } else { $rte['msg'] = 'Not Activated'; } } else { $rte['msg'] = 'Login Failed'; $errors .= "Login failed."; } echo json_encode($rte); } catch(WireException $e){ // in case of multiple false login (throttle login) $errors .= $e->getMessage(); // get the error message } } else { $rte['msg'] = 'You are already logged in.'; echo json_encode($rte); die(); } } } else { echo "<font color=red><strong>Unauthorised access. Your access to this site might be permanently blocked if tresspassing continues</strong></font>"; die(); } } else { //throw new WireException('CSRF check failed!'); $rte['msg'] = 'Tresspassing'; $rte['msg1'] = '<div class="w3-panel w3-red w3-card-4 w3-display-middle"><p class="w3-jumbo w3-red"><strong><h1>Forgery Attempt:</h1> <h3>The site has closed the connection. This occurs due to abrupt network disruption or in an event of hackers trying to steal information.</h3> <h2><small>Return to <a href="./">Homepage</a></small></h2></strong></p>'; echo json_encode($rte); die(); } ?> AJAX post from Login Page... $.ajax({ type: 'POST', url: url, data: postData, dataType: 'json', success: function (data) { if(data.msg == "member") { window.location.href = data.route; } else if(data.msg == "admin") { window.location.href = data.route; } else if(data.msg == "Not Activated") { $('#errmsg').css('display','block'); $('#loginerror').html('You are yet to activate the account. Please follow the link sent via email during registration for activation.'); document.getElementById('limgcaptcha').src="/site/templates/lcaptcha.php?id=" +((new Date()).getTime()); $('#loginform :input').attr('disabled', false); $('#show_pswd').attr('disabled',false); document.getElementById('lname').value = ""; document.getElementById('lpswd').value = ""; document.getElementById('lsec').value = ""; $("#cookie").prop('checked',false ); } else if(data.msg == "Login Failed") { $('#errmsg').css('display','block'); $('#loginerror').html('Login Failed. Please try again, or use the forgot password link below.'); document.getElementById('limgcaptcha').src="/site/templates/lcaptcha.php?id=" +((new Date()).getTime()); $('#loginform :input').attr('disabled', false); $('#show_pswd').attr('disabled',false); document.getElementById('lname').value = ""; document.getElementById('lpswd').value = ""; document.getElementById('lsec').value = ""; $("#cookie").prop('checked',false ); } else if(data.msg == "Tresspassing") { $('#main').html(data.msg1); //$('#errmsg').css('display','block'); //$('#loginerror').html(data.msg1); } } }); }); I know this code if lot more repetitive and can be cut short but I am learning this whole thing (PHP Language -> Processwire, CSS and JavaScript) of my own, hence Site Moderator / Members valuable feedback, above and beyond the exact issue is very much welcomed. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now