From 10801b8f7d4c5dc10dca3c1b53ffc4f6a93dc207 Mon Sep 17 00:00:00 2001 From: printempw Date: Wed, 28 Jun 2017 20:42:51 +0800 Subject: [PATCH] Redirect to last requested path after logging in --- app/Http/Controllers/AuthController.php | 2 + app/Http/Middleware/CheckAuthenticated.php | 13 ++++- resources/assets/src/scripts/auth.js | 60 ++++++++++++---------- resources/views/auth/master.tpl | 1 + 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 0ed30639..478d78bf 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -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 diff --git a/app/Http/Middleware/CheckAuthenticated.php b/app/Http/Middleware/CheckAuthenticated.php index ab6eb884..9f172f6b 100644 --- a/app/Http/Middleware/CheckAuthenticated.php +++ b/app/Http/Middleware/CheckAuthenticated.php @@ -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]); + } } diff --git a/resources/assets/src/scripts/auth.js b/resources/assets/src/scripts/auth.js index cd6dbef4..264af397 100644 --- a/resources/assets/src/scripts/auth.js +++ b/resources/assets/src/scripts/auth.js @@ -7,11 +7,9 @@ 'use strict'; -$(document).ready(function() { - $('input').iCheck({ - checkboxClass: 'icheckbox_square-blue' - }); -}); +$(document).ready(() => $('input').iCheck({ + checkboxClass: 'icheckbox_square-blue' +})); function freshCaptcha() { $('.captcha').attr('src', './captcha?' + new Date().getTime()); @@ -20,7 +18,7 @@ function freshCaptcha() { var login_fails = 0; -$('#login-button').click(function() { +$('#login-button').click(function () { var data = new Object(); data.identification = $('#identification').val(); @@ -49,16 +47,22 @@ $('#login-button').click(function() { url: "./login", dataType: "json", data: data, - beforeSend: function() { - $('#login-button').html(' '+trans('auth.loggingIn')).prop('disabled', 'disabled'); + beforeSend: () => { + $('#login-button').html( + ' ' + 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', ''); } @@ -89,7 +93,7 @@ $('#login-button').click(function() { $('.captcha').click(freshCaptcha); -$('#register-button').click(function() { +$('#register-button').click(function () { var email = $('#email').val(); var password = $('#password').val(); @@ -127,8 +131,10 @@ $('#register-button').click(function() { url: "./register", dataType: "json", data: { 'email': email, 'password': password, 'nickname': nickname, 'captcha': captcha }, - beforeSend: function() { - $('#register-button').html(' '+trans('auth.registering')).prop('disabled', 'disabled'); + beforeSend: function () { + $('#register-button').html( + ' ' + 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', ''); } @@ -153,7 +159,7 @@ $('#register-button').click(function() { }); -$('#forgot-button').click(function() { +$('#forgot-button').click(function () { var email = $('#email').val(); var captcha = $('#captcha').val(); @@ -174,10 +180,10 @@ $('#forgot-button').click(function() { url: "./forgot", dataType: "json", data: { 'email': email, 'captcha': captcha }, - beforeSend: function() { + beforeSend: () => { $('#forgot-button').html(' '+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', ''); } @@ -197,7 +203,7 @@ $('#forgot-button').click(function() { }); -$('#reset-button').click(function() { +$('#reset-button').click(function () { var uid = $('#uid').val(); var password = $('#password').val(); @@ -220,23 +226,23 @@ $('#reset-button').click(function() { url: "./reset", dataType: "json", data: { 'uid': uid, 'password': password }, - beforeSend: function() { - $('#reset-button').html(' '+trans('auth.resetting')).prop('disabled', 'disabled'); + beforeSend: () => { + $('#reset-button').html( + ' ' + 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', ''); } diff --git a/resources/views/auth/master.tpl b/resources/views/auth/master.tpl index 1dccc57f..213bf259 100644 --- a/resources/views/auth/master.tpl +++ b/resources/views/auth/master.tpl @@ -8,6 +8,7 @@ {!! bs_header('auth') !!} +