09/11/2018, 17:44

Tìm hiểu về Auth

Laravel giúp cho việc thực hiện việc xác thực vô cùng đơn giản. Trong thực tế, hầu hết mọi thứ đã được cấu hình cho bạn mà bạn chỉ việc dùng. File cấu hình xác thực được đặt tại config/auth.php bao gồm một số hướng dẫn tùy biến rõ ràng cho việc tùy biến cách xử lí của các dịch vụ authentication.

Laravel giúp cho việc thực hiện việc xác thực vô cùng đơn giản. Trong thực tế, hầu hết mọi thứ đã được cấu hình cho bạn mà bạn chỉ việc dùng. File cấu hình xác thực được đặt tại config/auth.php bao gồm một số hướng dẫn tùy biến rõ ràng cho việc tùy biến cách xử lí của các dịch vụ authentication.

Ví dụ về Auth

 

Ví dụ cho thấy có Auth thì chúng ta sẽ giảm bớt được code truy vấn và nó sẽ xử lý tất cả cho chúng ta

2.Các hàm trong Auth

Để sử dụng Auth ta phải thêm thư viện

use Illuminate\Support\Facades\Auth;
Phương Thức Chức Năng
attempt(array()) Đăng nhập với thông tin đăng nhập
login(model) Đăng nhập với đối tượng
logout() Hủy đăng nhập
user() Lấy thông tin người đăng nhập
check() Kiểm tra đăng nhập chưa

Ví Dụ

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

public function login(Request $request)
		{
			$username = $request['username'];
			$password = $request['password'];
			if (Auth::attempt(['username' => $username,'password' => $password])) {
				return redirect()->route('admin.index')->with('success','Chao mung');
			}else{
				return redirect()->back()->with('error','That bai');
			}
		}

 

public function login(Request $request)
		{
			$username = $request['username'];
			$password = $request['password'];
			if (Auth::attempt([$username,$password])) {
				return redirect()->route('admin.index')->with('user' => Auth::user());
			}else{
				return redirect()->back()->with('erro','That bai');
			}
		}

Auth::login()

use App\Models\User;

public function login(Request $request)
		{
			$username = $request['username'];
			$password = $request['password'];
			$user = User::find(2);
			Auth::login($user);
			return redirect()->route('admin.index')->with('success','Chao mung');
			//Khi dùng hàm login thì nó sẽ mặc định vào id user mà t viết
			// if (Auth::attempt([$username,$password])) {
			// 	return redirect()->route('admin.index')->with('success','Chao mung');
			// }else{
			// 	return redirect()->back()->with('erro','That bai');
			// }
		}

Loguot

public function logout(){
			Auth::logout();
			return redirect()->route('admin.login')->with('info','Thoát Thành Công');
		}

view login

<form action="" method="POST" role="form">
  <legend>Login</legend>

  <div class="form-group">
    <label for="">label</label>
    <input type="text" class="form-control" name="username" placeholder="Input field">
  </div>
  <div class="form-group">
    <label for="">label</label>
    <input type="text" class="form-control" name="password" placeholder="Input field">
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

view khi đăng nhập thành công

@if(isset($user))
  @if(Session::has('success'))
  <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    @endif
@endif

Route

Route::get('/login',[
		'uses' => '\App\Http\Controllers\admin\AuthController@index',
		'as' => 'admin.login'
	]);
Route::post('/login',[
		'uses' => '\App\Http\Controllers\admin\AuthController@login',
		'as' => 'admin.login'
	]);
Route::get('/logout',[
	'uses' => '\App\Http\Controllers\FrontendController@logout',
	'as' => 'home.logout'
]);

 

+1