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