您的位置:首页 > 编程学习 > > 正文

laravel开发登录接口(解决laravel5中auth用户登录其他页面获取不到登录信息的问题)

更多 时间:2021-10-11 00:53:41 类别:编程学习 浏览量:1310

laravel开发登录接口

解决laravel5中auth用户登录其他页面获取不到登录信息的问题

首先创建user表,里面有:id, name, password,remember_token等字段。

然后再Models添加表模型User.php

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • <?php
  •  
  • namespace App\Models;
  •  
  • use Illuminate\Database\Eloquent\Model;
  •  
  • use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  • use DB;
  •  
  • class User extends Model implements AuthenticatableContract
  • {
  •  
  •   protected $table = 'user';
  •  
  •   protected $primarykey = 'id';
  •  
  •   public $timestamps = false;
  •  
  •   protected $fillable = ['user_name', 'password', 'user_phone', 'user_email', 'user_role_id', 'user_avart', 'user_sex', 'user_age', 'user_birthday', 'last_login_ip', 'last_login_time', 'is_disabled', 'remember_token'];
  •  
  •   /**
  •    * Get the unique identifier for the user.
  •    *
  •    * @return mixed
  •    */
  •   public function getAuthIdentifier(){
  •     return $this->getKey();
  •   }
  •  
  •   /**
  •    * Get the password for the user.
  •    *
  •    * @return string
  •    */
  •   public function getAuthPassword(){
  •     return $this->password;
  •   }
  •  
  •   /**
  •    * Get the token value for the "remember me" session.
  •    *
  •    * @return string
  •    */
  •   public function getRememberToken()
  •   {
  •     return $this->{$this->getRememberTokenName()};
  •   }
  •  
  •   /**
  •    * Set the token value for the "remember me" session.
  •    *
  •    * @param string $value
  •    * @return void
  •    */
  •   public function setRememberToken($value)
  •   {
  •     $this->{$this->getRememberTokenName()} = $value;
  •   }
  •  
  •   /**
  •    * Get the column name for the "remember me" token.
  •    *
  •    * @return string
  •    */
  •   public function getRememberTokenName()
  •   {
  •     return 'remember_token';
  •   }
  • }
  • 然后需要在配置文件config/auth.php中修改如下配置:

  • ?
  • 1
  • 2
  • 'model' => App\Models\User::class,    //指定模型
  • 'table' => 'user',                   //指定用户表(user是我数据中储存用户的表)
  • 接着在登录方法里使用Auth::login() 方法登录,如下:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • public function store(Request $request)
  •   {
  •  
  •     if(empty($request->get('chkCode')) || trim($request->get('chkCode')) != trim(Session::get('admincaptcha'))){
  •  
  •       $error->add('result','验证码不正确');
  •  
  •       return back()->withErrors($error);
  •     }
  •     $adminUser = User::where('user_name',$request->get('user_name'))->where('user_role_id', '>', 0)->first();
  •  
  •     if(empty($adminUser)){
  •  
  •       $error->add('result','用户无后台权限');
  •  
  •       return back()->withErrors($error);
  •     }else{
  •  
  •       if (md5($request->get('user_pwd'))===$adminUser->password&&$adminUser->user_role_id){
  •  
  •         Auth::login($adminUser);
  •  
  •         Session::put('admincaptcha', "");
  •  
  •         return redirect()->route('admin.home');
  •  
  •       }else{
  •  
  •         $error->add('result','用户名或密码错误');
  •  
  •         return back()->withErrors($error);
  •       }
  •     }
  •   }
  • 然而虽然这个页面可以获取到登录信息,然而其他页面却没有,原来是因为id和密码我用的是user_id和user_pwd不是id和password,这两个必须不能变,改了之后可以正常登录。

    以上这篇解决laravel5中auth用户登录其他页面获取不到登录信息的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。

    原文链接:https://blog.csdn.net/leedaning/article/details/53102703

    您可能感兴趣