Laravel认证后重定向到登录名 [英] Laravel redirects to login after authentication

查看:60
本文介绍了Laravel认证后重定向到登录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于我的第一个 Laravel 应用程序,虽然我看到了编写内容的方式的好处,但我很难理解其中的一些行为.

I'm busy with my first Laravel app and although I see the benefits of the way things are written, I'm having a hard time understanding some of the behaviour.

当我尝试登录时,我被重定向到登录页面.看来用户已正确验证身份,但无论如何它都会重定向到登录页面.

When I try to login I get redirected to the login page. It seems like the user authenticates correctly, but it redirects to the login page regardless.

我的用户表如下:

,---------,---------------,---------------,----------------,---------------,-----------,
| user_id | user_username | user_password | user_firtsname | user_lastname | user_type |
|---------|---------------|---------------|----------------|---------------|-----------|
| 1       | me@domain.com | encrypted     | Foo            | Bar           | farmer    |
'---------'---------------'---------------'----------------'---------------'-----------'

这是我的路线文件:

<?php
Route::get('/login', 'UsersController@login');

Auth::routes();
Route::get('/dashboard', 'HomeController@dashboard')->name('dashboard');
Route::get('/users/grid', 'UsersController@grid');
Route::resource('/users', 'UsersController');

LoginController.php

LoginController.php

<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * @var string
     */
    protected $redirectTo = '/dashboard';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * @return string
     */
    public function username()
    {
        return 'user_username';
    }

    /**
     * @param Request $request
     * @param $user
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function authenticated(Request $request, $user)
    {
        return $user->user_type === 'farmer' ? redirect('/dashboard') : redirect('/admin');
    }
}

User.php

<?php
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * @var array
     */
    protected $fillable = [
        'user_username', 'user_firstname', 'user_lastname', 'user_password',
    ];

    /**
     * @var array
     */
    protected $hidden = [
        'user_password', 'remember_token',
    ];

    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * @return mixed|string
     */
    public function getAuthPassword()
    {
        return $this->user_password;
    }

    /**
     * @return string
     */
    public function getKey()
    {
        return $this->user_id;
    }
}

我做了什么

我已经阅读了各种问题 堆栈溢出,但由于某些原因,我无法登录工作.

WHAT I'VE DONE

I've read various questions on stackoverflow but for some reason I can't get login to work.

我使用 php artisan make:auth 创建了auth.我也尝试阅读文档,但是还是没有运气.

I created the auth using php artisan make:auth. I've tried reading the documentation too, but still no luck.

如何在登录后将其重定向到仪表板?我想念什么?

How do I get it to redirect to the dashboard after login? What am I missing?

推荐答案

您需要在用户模型中添加此道具.

You need to add this prop in your user model.

protected $primaryKey = 'user_id';

从登录控制器中删除 authenticated()方法.

并根据 RedirectIfAuthenticated 中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
          if (Auth::guard($guard)->user->user_type === 'farmer'){
            redirect('/dashboard'); // make sure the urls is correct with condition above 
          }
          return redirect('/admin');
        }

        return $next($request);
    }
}

App \ Exceptions \ Handler

如果每种用户类型的登录页面不同,请添加此方法

Add this method if you have different login pages for each user type

/**
 * @param Request $request
 * @param AuthenticationException $exception
 * @return JsonResponse|RedirectResponse|\Symfony\Component\HttpFoundation\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }
    if ($request->is('dashboard') || $request->is('dashboard/*')) {
        return redirect()->guest('/dashboard/login');
    }

    return redirect()->guest('farmer/login');
}

这篇关于Laravel认证后重定向到登录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆