Laravel custom authenticated

Laravel Custom Authentication

Membuat custom authentication sendiri pada laravel

Kali ini kita akan belajar bagaimana membuat login authentication sendiri agar kita lebih paham, oke langsung saja kita mulai…

1. Buat project laravel nya dulu

composer create-project laravel-laravel nama_project prefer-dist

2. Buat Controller Auth

php artisan make:controller Auth\AuthController

3. Masuk ke Auth controller nya

kita import apa saja yang diperlukan

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User; #kita import model User nya

use Illuminate\Support\Facades\Validator; # untuk validasi
use Illuminate\Support\Facades\Hash; # untuk decrypt password

4. Kita buat function untuk handle login

public function login(Request $req){
 $user = User::where('email', $req->email)->first();
 if($user){
   if (Hash::check($req->password, $user->password)) {
            
            $auth = User::where('email',$req->email)->firstOrFail();
            $response = ['status_login' => true, 'user' => $auth];
            return response($response, 200);
        }else{
            $response = 'Password Missmatch';
            return response($response, 422);
        } else{
            $response = 'User Does not Exist';
            return response($response, 422);
        }
   }
}

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply