What friends are I working on a project and I have this to close the session, but I would like to implement JQuery Confirm the problem is that I do not know how to do it, I have this code, which does the following with a sweet alert:
var timeout;
document.onmousemove = function(){
clearTimeout(timeout);
timeout = setTimeout(function(){
swal({
title: "Cierre de Sesion Automatica",
text: "Si deseas salir haz clic en Cerrar Sesion o en Cancel para continuar trabajando",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Cerrar Sesion",
closeOnConfirm: false,
showLoaderOnConfirm: true,
},
function(isConfirm){
if (isConfirm) {
$.ajax({
url: base_url + "auth/logout",
type: "POST",
success:function(resp){
window.location.href= base_url;
}
});
}else{
window.location.href = base_url + "movimientos/ordenes";
}
});
}, 30000);
}
Now there is a code that I provided @Walter Cordova but I would need you to help me implement it in my code, since I need the countdown anyway, and after that destroy the session.
This is the code of @Walter Cordova which works very well, I leave it in a box of JS.
$( document ).ready(function() {
$.confirm({
title: 'Alerta!',
content: 'La sesión esta por expirar.',
autoClose: 'logoutUser|10000',
buttons: {
logoutUser: {
text: 'cerrar sesión',
action: function () {
$.alert('La sesión ha expirado');
//tu codigo AJAX
}
},
cancelar: function () {
$.alert('cancelado');
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.js"></script>
This is my Session controller, and below is the logout function that destroys the session:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("Usuarios_model");
}
public function index()
{
if ($this->session->userdata("login")) {
redirect(base_url()."dashboard");
}
else{
$this->load->view("admin/login");
}
}
public function login(){
$username = $this->input->post("username");
$password = $this->input->post("password");
$res = $this->Usuarios_model->login($username, sha1($password));
if (!$res) {
/*$this->session->set_flashdata("error","El usuario y/o contraseña son incorrectos");*/
//redirect(base_url());
echo "0";
}
else{
$data = array(
'id' => $res->id,
'nombre' => $res->nombres,
'rol' => $res->rol_id,
'login' => TRUE
);
$this->session->set_userdata($data);
//redirect(base_url()."dashboard");
echo "1";
}
}
public function logout(){
$this->session->sess_destroy();
redirect(base_url());
}
}