Laravel 7 自定义登录

同样模板用的是H-ui

1. 登录页 ajax请求

<script>
	var isAjax = true;
	/**
	 * [submitLoginForm 登录操作. ]
	 * @author mgang 2020-08-03
	 * @return {[type]} [description]
	 */
	function submitLoginForm()
	{
		if( isAjax==false ) return false;
		var user_name 	= $.trim($("input[name=user_name]").val());
		var password 	= $.trim($("input[name=password]").val());
		if( user_name=='' ){
			layer.tips('用户名不能为空',"input[name=user_name]");
			return false;
		}
		if( password=='' ){
			layer.tips('密码不能为空',"input[name=password]");
			return false;
		}
		var token = $.trim($("input[name=_token]").val());
		if( user_name!='' && password!='' ){
			isAjax = false;
			$.ajax({
				url:'{{url("backend/login/login")}}?user_name='+user_name+'&password='+password+'&_token='+token,
				type:'get',
				success:function( result ){
					isAjax = true;
					if( result.action_status=="OK" ){
						layer.msg("登陆成功",{time:3000},function(){
							window.location.href="{{url('backend/index')}}";
						});
					}else{
						layer.msg(result.error_msg,{time:3000});
						$("input[name=user_name]").val('');
						$("input[name=password]").val('');
					}
				},
				error:function(){
					isAjax = true;
					layer.msg("网络错误,请联系管理员!");
				}
			});
		}

	}
</script>

2. LoginController

<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends \App\Http\Controllers\Controller
{
    /**
     * [index 登录页面. ]
     * @author mgang 2020-08-01
     * @return [type] [description]
     */
    public function index()
    {
        return view('backend.auth.index');
    }

    /**
     * [login 登陆动作. ]
     * @author mgang 2020-08-05
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function login( Request $request )
    {
        $params = array();
        $params = $request->only('user_name','password');
        if( Auth::attempt($params) ){
            return response()->json(['action_status'=>'OK', 'error_code'=>0, 'error_msg'=>'', 'success_msg'=>'登陆成功']);
        }
        return response()->json(['action_status'=>'FAIL', 'error_code'=>60001, 'error_msg'=>'用户名或者密码不正确', 'success_msg'=>'']);
    }


    /**
     * [loginout 退出动作. ]
     * @author mgang 2020-08-05
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function loginout()
    {
        Auth::logout();
        return response()->json(['action_status'=>'OK', 'error_code'=>0, 'error_msg'=>'', 'success_msg'=>'退出成功']);
    }




}

3 . 认证中间件为 app/Http/Middleware/Authenticate [ 这里代码不用动 ]

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Closure;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo( $request )
    {
        #    用户认证失败后,跳转登陆页 , 可根据需求自行设置跳转的页面
        return route('login');
    }

}

4. backend 路由

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

#	后台Auth验证用户登录状态
Route::namespace('Backend')->group(function(){
	Route::get('/login', 'LoginController@index')->name('backend.login.index');
	Route::get('/login/login', 'LoginController@login');
	
	Route::middleware('auth')->group(function(){
		Route::get('/index', 'indexController@index')->name('backend.index.index');
	});
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值