使用 auth laravel 5.7 自定义登录和注册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54443021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom login and registration with auth laravel 5.7
提问by Giu
I'm trying to create the first project with Laravel 5.7.
我正在尝试使用Laravel 5.7创建第一个项目。
In this project I have three different areas with three different login (ex. Login for users, login for admin, login for technicians).
在这个项目中,我有三个不同的区域,三个不同的登录(例如,用户登录,管理员登录,技术人员登录)。
Now, I would like to customize the first login (url, html, input, class, db table, db column ecc...) but I don't know how to do.
现在,我想自定义第一次登录(url、html、输入、类、db 表、db 列 ecc...)但我不知道该怎么做。
The URLof first login is website.com/public/users
第一次登录的网址是website.com/public/users
My Tableis:
我的表是:
CREATE TABLE `gf_users` (
`gf_id` int(11) NOT NULL,
`gf_name` text NOT NULL,
`gf_email` varchar(200) NOT NULL,
`gf_password` varchar(300) NOT NULL,
)
I have searched a lot and with the information that I have found, I have tried to do this:
我已经搜索了很多,并根据我找到的信息,我尝试这样做:
config/auth.php:
配置/auth.php:
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\GFUsers::class
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
web.php
网页.php
Route::get('users', 'Auth\LoginController@showLoginFormUsers')->name('login');
Route::post('users', 'Auth\LoginController@login');
LoginController.php
登录控制器.php
public function showLoginFormUsers(){
return view('users.login');
}
public function username(){
return 'gf_email';
}
GFUsers.php
GFUsers.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class GFUsers extends Authenticatable
{
use Notifiable;
protected $table = 'gf_users';
protected $primaryKey = 'gf_id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'gf_email', 'gf_password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'gf_password'
];
public function getAuthPassword(){
return $this->gf_password;
}
}
login.blade.php
登录名.blade.php
<form method="POST" action="{{ asset('/users') }}">
@csrf
<div class="form-group">
<input type="email" class="form-control" name="gf_email" id="gf_email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="password" class="form-control" name="gf_password" id="gf_password" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-primary">LOGIN</button>
</form>
If I compile the form and click on login button, this refresh the page but I don't understand if it works or not.
如果我编译表单并单击登录按钮,这将刷新页面,但我不明白它是否有效。
1. Is the procedure correct?
2. Are there some errors?
3. Have I forget something?
4. What can I do for registration?
1. 程序是否正确?
2. 是否有一些错误?
3. 我是不是忘记了什么?
4. 我可以做什么来注册?
采纳答案by Giu
After long research I have find my solution. I hope that this can help someone else.
I have customized: id, username, password and remember token in the table and the files User.php, LoginController.php.
经过长时间的研究,我找到了我的解决方案。我希望这可以帮助别人。
我已经自定义了:表中的 id、用户名、密码和记住令牌以及文件 User.php、LoginController.php。
Table:
表:
CREATE TABLE `gf_users` (
`gf_id` int(11) NOT NULL,
`gf_name` text NOT NULL,
`gf_email` varchar(200) NOT NULL,
`gf_password` varchar(300) NOT NULL,
`gf_remember_token` varchar(300) NOT NULL,
)
web.php:
网页.php:
// Authentication Routes
Route::get('users', 'Auth\NewLoginController@showLoginFormUsers')->name('login');
Route::post('users', 'Auth\NewLoginController@checklogin');
Route::post('logout', 'Auth\NewLoginController@logout')->name('logout');
// Registration Routes
Route::get('users/registration', 'Auth\RegisterController@showRegistrationFormUsers')->name('register');
Route::post('users/registration', 'Auth\RegisterController@register');
NewLoginController.php:
新登录控制器.php:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/users/details';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginFormUsers(){
return view('users.login');
}
public function username(){
return 'gf_email';
}
function checklogin(Request $request){
$this->validate($request, [
'input-email' => 'required',
'input-password' => 'required',
]);
$user_data = array(
'gf_email' => $request->get('input-email'),
'password' => $request->get('input-password')
);
if(!Auth::attempt($user_data)){
return redirect('users');
}
if ( Auth::check() ) {
return redirect('users/details');
}
}
function logout(){
Auth::logout();
return redirect('users');
}
}
NewUser.php:
新用户.php:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'gf_users';
protected $primaryKey = 'gf_id';
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'gf_email', 'gf_password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'gf_password', 'gf_remember_token',
];
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword(){
return $this->gf_password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->gf_remember_token;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->gf_remember_token = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'gf_remember_token';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->gf_email;
}
}
login.blade.php
登录名.blade.php
<form method="POST" action="{{ asset('/users') }}">
@csrf
<div class="form-group">
<input type="email" class="form-control" name="input-email" id="input-email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="password" class="form-control" name="input-password" id="input-password" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-primary">LOGIN</button>
</form>
config/auth.php:
配置/auth.php:
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\NewUser::class
],
//'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
After this changes I did the commandphp artisan config:cache
.
在此更改后,我执行了命令php artisan config:cache
。
回答by Lim Kean Phang
config/auth.php:
配置/auth.php:
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'gfuser' => [
'driver' => 'session',
'provider' => 'gfuser',
],
],
'providers' => [
'gfuser' => [
'driver' => 'eloquent',
'model' => App\GFUsers::class
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
LoginController
登录控制器
Auth::guard('gfuser')->login($user); //array data with username and password
OR
if (Auth::guard('gfuser')->attempt(['username' => $username, 'password' => $password]) {//Redirect to dashboard}
回答by Edgars Praulins
You better not to make different tables for users, admin and technicians. They all suposed to be users. In one table and just with different attrinutes (for example - role, statuss, is_admin, is_user... ).
最好不要为用户、管理员和技术人员制作不同的表格。他们都应该是用户。在一张表中,只是具有不同的属性(例如 - 角色、状态、is_admin、is_user...)。