source: app/Http/Controllers/SessionsController.php@ 69e9f5d

main
Last change on this file since 69e9f5d was 69e9f5d, checked in by bube-ristovska <ristovska725@…>, 4 months ago

Corrected authentication

  • Property mode set to 100644
File size: 2.2 KB
Line 
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Models\Officer;
6use Illuminate\Http\Request;
7use Illuminate\Support\Facades\Auth;
8use Illuminate\Support\Facades\DB;
9use Illuminate\Support\Facades\Session;
10
11class 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 } else {
48 Session::put('pe_id', $is_officer[0]->pe_id);
49 }
50 return view('welcome');
51 }
52
53 // Authentication failed
54 return back()->withErrors(['password' => 'Invalid credentials']);
55 }
56
57 public function logout()
58 {
59 Session::forget('badge_no');
60 Session::forget('p_id');
61 Session::forget('pe_id');
62 Session::forget('is_policeman');
63 return redirect('/login');
64 }
65}
Note: See TracBrowser for help on using the repository browser.