[75151c6] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers;
|
---|
| 4 |
|
---|
[cf84baa] | 5 | use App\Models\Officer;
|
---|
[75151c6] | 6 | use Illuminate\Http\Request;
|
---|
[cf84baa] | 7 | use Illuminate\Support\Facades\Auth;
|
---|
| 8 | use Illuminate\Support\Facades\DB;
|
---|
| 9 | use Illuminate\Support\Facades\Session;
|
---|
[75151c6] | 10 |
|
---|
| 11 | class SessionsController extends Controller
|
---|
| 12 | {
|
---|
[cf84baa] | 13 | public function store()
|
---|
| 14 | {
|
---|
| 15 |
|
---|
| 16 | $credentials = request()->validate([
|
---|
| 17 | 'badge_no' => 'required',
|
---|
| 18 | 'password' => 'required'
|
---|
| 19 | ]);
|
---|
| 20 | $password = $credentials['password'];
|
---|
| 21 | $badge_no = $credentials['badge_no'];
|
---|
| 22 | $policeman = true;
|
---|
| 23 | $exists = DB::select('select * from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
|
---|
| 24 | $pass = DB::select('select p_password from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
|
---|
| 25 | if($exists == null) {
|
---|
| 26 | $exists = DB::select('select * from officer where o_badge_no = :badge_no;', ['badge_no' => $badge_no]);
|
---|
| 27 | $pass = DB::select('select o_password from officer where o_badge_no = :badge_no;', ['badge_no' => $badge_no]);
|
---|
| 28 | $policeman = false;
|
---|
| 29 | }
|
---|
| 30 | if($exists == null) {
|
---|
| 31 | return back()->withErrors(['badge_no' => 'Invalid credentials']);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | foreach ($pass[0] as $key => $val) {
|
---|
| 35 | $value = $val;
|
---|
| 36 | break; // Break after the first key-value pair
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | if ($value == $password) {
|
---|
| 41 | // Authentication passed
|
---|
| 42 | Session::put('badge_no', $badge_no);
|
---|
| 43 | Session::put('is_policeman', $policeman);
|
---|
| 44 | return redirect()->intended('/');
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | // Authentication failed
|
---|
| 48 | return back()->withErrors(['password' => 'Invalid credentials']);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public function logout()
|
---|
| 52 | {
|
---|
| 53 | Session::forget('badge_no');
|
---|
| 54 | Session::forget('is_policeman');
|
---|
| 55 | return redirect('/login');
|
---|
| 56 | }
|
---|
[75151c6] | 57 | }
|
---|