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 | // mozhe da se najavi kako policaec i kako officer, znaeme koj e koj po znachkata
|
---|
22 |
|
---|
23 | $policeman = true;
|
---|
24 | $is_policeman = DB::select('select * from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
|
---|
25 | $is_officer = DB::select('select * from officer where o_badge_no = :badge_no;', ['badge_no' => $badge_no]);
|
---|
26 | if($is_officer==null && $is_policeman==null) {
|
---|
27 | return back()->withErrors(['password' => 'Invalid credentials']);
|
---|
28 | }
|
---|
29 | if($is_officer!=null) {
|
---|
30 | $pass = DB::select('select o_password from officer where o_badge_no = :o_badge_no;', ['o_badge_no' => $badge_no]);
|
---|
31 | $policeman = false;
|
---|
32 | } else {
|
---|
33 | $pass = DB::select('select p_password from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
|
---|
34 | }
|
---|
35 |
|
---|
36 | foreach ($pass[0] as $key => $val) {
|
---|
37 | $value = $val;
|
---|
38 | break; // Break after the first key-value pair
|
---|
39 | }
|
---|
40 |
|
---|
41 | if ($value == $password) {
|
---|
42 | // Authentication passed
|
---|
43 | Session::put('badge_no', $badge_no);
|
---|
44 | Session::put('is_policeman', $policeman);
|
---|
45 | if($policeman){
|
---|
46 | Session::put('pe_id', $is_policeman[0]->pe_id);
|
---|
47 | Session::put('p_id', $is_policeman[0]->p_id);
|
---|
48 | } else {
|
---|
49 | Session::put('pe_id', $is_officer[0]->pe_id);
|
---|
50 | }
|
---|
51 | return view('welcome');
|
---|
52 | }
|
---|
53 |
|
---|
54 | // Authentication failed
|
---|
55 | return back()->withErrors(['password' => 'Invalid credentials']);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public function logout()
|
---|
59 | {
|
---|
60 | Session::forget('badge_no');
|
---|
61 | Session::forget('p_id');
|
---|
62 | Session::forget('pe_id');
|
---|
63 | Session::forget('is_policeman');
|
---|
64 | return redirect('/login');
|
---|
65 | }
|
---|
66 | }
|
---|