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));
session()->forget('last_requested_path');
return json(trans('auth.login.success'), 0, [
'token' => $user->getToken()
]) // set cookies

View File

@ -25,8 +25,10 @@ class CheckAuthenticated
$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'));
}
if ($user->getPermission() == "-1") {
delete_sessions();
@ -45,6 +47,8 @@ class CheckAuthenticated
return $returnUser ? $user : $next($request);
} else {
$this->flashLastRequestedPath();
return redirect('auth/login')->with('msg', trans('auth.check.anonymous'));
}
@ -75,4 +79,11 @@ class CheckAuthenticated
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';
$(document).ready(function() {
$('input').iCheck({
$(document).ready(() => $('input').iCheck({
checkboxClass: 'icheckbox_square-blue'
});
});
}));
function freshCaptcha() {
$('.captcha').attr('src', './captcha?' + new Date().getTime());
@ -49,16 +47,22 @@ $('#login-button').click(function() {
url: "./login",
dataType: "json",
data: data,
beforeSend: function() {
$('#login-button').html('<i class="fa fa-spinner fa-spin"></i> '+trans('auth.loggingIn')).prop('disabled', 'disabled');
beforeSend: () => {
$('#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) {
swal({
type: 'success',
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 {
if (json.login_fails > 3) {
@ -78,7 +82,7 @@ $('#login-button').click(function() {
$('#login-button').html(trans('auth.login')).prop('disabled', '');
}
},
error: function(json) {
error: (json) => {
showAjaxError(json);
$('#login-button').html(trans('auth.login')).prop('disabled', '');
}
@ -128,7 +132,9 @@ $('#register-button').click(function() {
dataType: "json",
data: { 'email': email, 'password': password, 'nickname': nickname, 'captcha': captcha },
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) {
if (json.errno == 0) {
@ -143,7 +149,7 @@ $('#register-button').click(function() {
$('#register-button').html(trans('auth.register')).prop('disabled', '');
}
},
error: function(json) {
error: (json) => {
showAjaxError(json);
$('#register-button').html(trans('auth.register')).prop('disabled', '');
}
@ -174,10 +180,10 @@ $('#forgot-button').click(function() {
url: "./forgot",
dataType: "json",
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');
},
success: function(json) {
success: (json) => {
if (json.errno == 0) {
showMsg(json.msg, 'success');
$('#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', '');
}
},
error: function(json) {
error: (json) => {
showAjaxError(json);
$('#forgot-button').html(trans('auth.send')).prop('disabled', '');
}
@ -220,23 +226,23 @@ $('#reset-button').click(function() {
url: "./reset",
dataType: "json",
data: { 'uid': uid, 'password': password },
beforeSend: function() {
$('#reset-button').html('<i class="fa fa-spinner fa-spin"></i> '+trans('auth.resetting')).prop('disabled', 'disabled');
beforeSend: () => {
$('#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) {
swal({
type: 'success',
html: json.msg
}).then(function() {
window.location = "./login";
});
}).then(() => (window.location = "./login"));
} else {
showMsg(json.msg, 'warning');
$('#reset-button').html(trans('auth.reset')).prop('disabled', '');
}
},
error: function(json) {
error: (json) => {
showAjaxError(json);
$('#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">
<!-- App Styles -->
{!! bs_header('auth') !!}
<script>blessing.redirect_to = '<?php echo session('last_requested_path') ?>';</script>
</head>
<body class="hold-transition login-page">