Redirect to last requested path after logging in

This commit is contained in:
printempw 2017-06-28 20:42:51 +08:00
parent 1957f97807
commit 10801b8f7d
4 changed files with 48 additions and 28 deletions

View File

@ -60,6 +60,8 @@ class AuthController extends Controller
event(new Events\UserLoggedIn($user)); event(new Events\UserLoggedIn($user));
session()->forget('last_requested_path');
return json(trans('auth.login.success'), 0, [ return json(trans('auth.login.success'), 0, [
'token' => $user->getToken() 'token' => $user->getToken()
]) // set cookies ]) // set cookies

View File

@ -25,8 +25,10 @@ class CheckAuthenticated
$user = app('user.current'); $user = app('user.current');
} }
if (session('token') != $user->getToken()) if (session('token') != $user->getToken()) {
$this->flashLastRequestedPath();
return redirect('auth/login')->with('msg', trans('auth.check.token')); return redirect('auth/login')->with('msg', trans('auth.check.token'));
}
if ($user->getPermission() == "-1") { if ($user->getPermission() == "-1") {
delete_sessions(); delete_sessions();
@ -45,6 +47,8 @@ class CheckAuthenticated
return $returnUser ? $user : $next($request); return $returnUser ? $user : $next($request);
} else { } else {
$this->flashLastRequestedPath();
return redirect('auth/login')->with('msg', trans('auth.check.anonymous')); return redirect('auth/login')->with('msg', trans('auth.check.anonymous'));
} }
@ -75,4 +79,11 @@ class CheckAuthenticated
return response()->view('auth.bind'); return response()->view('auth.bind');
} }
protected function flashLastRequestedPath($path = null)
{
$path = $path ?: app('request')->path();
return session(['last_requested_path' => $path]);
}
} }

View File

@ -7,11 +7,9 @@
'use strict'; 'use strict';
$(document).ready(function() { $(document).ready(() => $('input').iCheck({
$('input').iCheck({ checkboxClass: 'icheckbox_square-blue'
checkboxClass: 'icheckbox_square-blue' }));
});
});
function freshCaptcha() { function freshCaptcha() {
$('.captcha').attr('src', './captcha?' + new Date().getTime()); $('.captcha').attr('src', './captcha?' + new Date().getTime());
@ -20,7 +18,7 @@ function freshCaptcha() {
var login_fails = 0; var login_fails = 0;
$('#login-button').click(function() { $('#login-button').click(function () {
var data = new Object(); var data = new Object();
data.identification = $('#identification').val(); data.identification = $('#identification').val();
@ -49,16 +47,22 @@ $('#login-button').click(function() {
url: "./login", url: "./login",
dataType: "json", dataType: "json",
data: data, data: data,
beforeSend: function() { beforeSend: () => {
$('#login-button').html('<i class="fa fa-spinner fa-spin"></i> '+trans('auth.loggingIn')).prop('disabled', 'disabled'); $('#login-button').html(
'<i class="fa fa-spinner fa-spin"></i> ' + trans('auth.loggingIn')
).prop('disabled', 'disabled');
}, },
success: function(json) { success: (json) => {
if (json.errno == 0) { if (json.errno == 0) {
swal({ swal({
type: 'success', type: 'success',
html: json.msg html: json.msg
}); });
window.setTimeout('window.location = "../user"', 1000);
// redirect to last requested path
let redirect_to = url(blessing.redirect_to) || "../user";
window.setTimeout(() => (window.location = redirect_to), 1000);
} else { } else {
if (json.login_fails > 3) { if (json.login_fails > 3) {
@ -78,7 +82,7 @@ $('#login-button').click(function() {
$('#login-button').html(trans('auth.login')).prop('disabled', ''); $('#login-button').html(trans('auth.login')).prop('disabled', '');
} }
}, },
error: function(json) { error: (json) => {
showAjaxError(json); showAjaxError(json);
$('#login-button').html(trans('auth.login')).prop('disabled', ''); $('#login-button').html(trans('auth.login')).prop('disabled', '');
} }
@ -89,7 +93,7 @@ $('#login-button').click(function() {
$('.captcha').click(freshCaptcha); $('.captcha').click(freshCaptcha);
$('#register-button').click(function() { $('#register-button').click(function () {
var email = $('#email').val(); var email = $('#email').val();
var password = $('#password').val(); var password = $('#password').val();
@ -127,8 +131,10 @@ $('#register-button').click(function() {
url: "./register", url: "./register",
dataType: "json", dataType: "json",
data: { 'email': email, 'password': password, 'nickname': nickname, 'captcha': captcha }, data: { 'email': email, 'password': password, 'nickname': nickname, 'captcha': captcha },
beforeSend: function() { beforeSend: function () {
$('#register-button').html('<i class="fa fa-spinner fa-spin"></i> '+trans('auth.registering')).prop('disabled', 'disabled'); $('#register-button').html(
'<i class="fa fa-spinner fa-spin"></i> ' + trans('auth.registering')
).prop('disabled', 'disabled');
}, },
success: function(json) { success: function(json) {
if (json.errno == 0) { if (json.errno == 0) {
@ -143,7 +149,7 @@ $('#register-button').click(function() {
$('#register-button').html(trans('auth.register')).prop('disabled', ''); $('#register-button').html(trans('auth.register')).prop('disabled', '');
} }
}, },
error: function(json) { error: (json) => {
showAjaxError(json); showAjaxError(json);
$('#register-button').html(trans('auth.register')).prop('disabled', ''); $('#register-button').html(trans('auth.register')).prop('disabled', '');
} }
@ -153,7 +159,7 @@ $('#register-button').click(function() {
}); });
$('#forgot-button').click(function() { $('#forgot-button').click(function () {
var email = $('#email').val(); var email = $('#email').val();
var captcha = $('#captcha').val(); var captcha = $('#captcha').val();
@ -174,10 +180,10 @@ $('#forgot-button').click(function() {
url: "./forgot", url: "./forgot",
dataType: "json", dataType: "json",
data: { 'email': email, 'captcha': captcha }, data: { 'email': email, 'captcha': captcha },
beforeSend: function() { beforeSend: () => {
$('#forgot-button').html('<i class="fa fa-spinner fa-spin"></i> '+trans('auth.sending')).prop('disabled', 'disabled'); $('#forgot-button').html('<i class="fa fa-spinner fa-spin"></i> '+trans('auth.sending')).prop('disabled', 'disabled');
}, },
success: function(json) { success: (json) => {
if (json.errno == 0) { if (json.errno == 0) {
showMsg(json.msg, 'success'); showMsg(json.msg, 'success');
$('#forgot-button').html(trans('auth.send')).prop('disabled', 'disabled'); $('#forgot-button').html(trans('auth.send')).prop('disabled', 'disabled');
@ -187,7 +193,7 @@ $('#forgot-button').click(function() {
$('#forgot-button').html(trans('auth.send')).prop('disabled', ''); $('#forgot-button').html(trans('auth.send')).prop('disabled', '');
} }
}, },
error: function(json) { error: (json) => {
showAjaxError(json); showAjaxError(json);
$('#forgot-button').html(trans('auth.send')).prop('disabled', ''); $('#forgot-button').html(trans('auth.send')).prop('disabled', '');
} }
@ -197,7 +203,7 @@ $('#forgot-button').click(function() {
}); });
$('#reset-button').click(function() { $('#reset-button').click(function () {
var uid = $('#uid').val(); var uid = $('#uid').val();
var password = $('#password').val(); var password = $('#password').val();
@ -220,23 +226,23 @@ $('#reset-button').click(function() {
url: "./reset", url: "./reset",
dataType: "json", dataType: "json",
data: { 'uid': uid, 'password': password }, data: { 'uid': uid, 'password': password },
beforeSend: function() { beforeSend: () => {
$('#reset-button').html('<i class="fa fa-spinner fa-spin"></i> '+trans('auth.resetting')).prop('disabled', 'disabled'); $('#reset-button').html(
'<i class="fa fa-spinner fa-spin"></i> ' + trans('auth.resetting')
).prop('disabled', 'disabled');
}, },
success: function(json) { success: (json) => {
if (json.errno == 0) { if (json.errno == 0) {
swal({ swal({
type: 'success', type: 'success',
html: json.msg html: json.msg
}).then(function() { }).then(() => (window.location = "./login"));
window.location = "./login";
});
} else { } else {
showMsg(json.msg, 'warning'); showMsg(json.msg, 'warning');
$('#reset-button').html(trans('auth.reset')).prop('disabled', ''); $('#reset-button').html(trans('auth.reset')).prop('disabled', '');
} }
}, },
error: function(json) { error: (json) => {
showAjaxError(json); showAjaxError(json);
$('#reset-button').html(trans('auth.reset')).prop('disabled', ''); $('#reset-button').html(trans('auth.reset')).prop('disabled', '');
} }

View File

@ -8,6 +8,7 @@
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- App Styles --> <!-- App Styles -->
{!! bs_header('auth') !!} {!! bs_header('auth') !!}
<script>blessing.redirect_to = '<?php echo session('last_requested_path') ?>';</script>
</head> </head>
<body class="hold-transition login-page"> <body class="hold-transition login-page">