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

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

Corrected authentication

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[75151c6]1<?php
2
3namespace App\Http\Controllers;
4
[cf84baa]5use App\Models\Officer;
[75151c6]6use Illuminate\Http\Request;
[cf84baa]7use Illuminate\Support\Facades\Auth;
8use Illuminate\Support\Facades\DB;
9use Illuminate\Support\Facades\Session;
[75151c6]10
11class SessionsController extends Controller
12{
[cf84baa]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'];
[69e9f5d]21 // mozhe da se najavi kako policaec i kako officer, znaeme koj e koj po znachkata
22
[cf84baa]23 $policeman = true;
[69e9f5d]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']);
[cf84baa]28 }
[69e9f5d]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]);
[cf84baa]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);
[d9c4096]45 if($policeman){
[69e9f5d]46 Session::put('pe_id', $is_policeman[0]->pe_id);
[d9c4096]47 } else {
[69e9f5d]48 Session::put('pe_id', $is_officer[0]->pe_id);
[d9c4096]49 }
[7e9dadd]50 return view('welcome');
[cf84baa]51 }
52
53 // Authentication failed
54 return back()->withErrors(['password' => 'Invalid credentials']);
55 }
56
57 public function logout()
58 {
59 Session::forget('badge_no');
[69e9f5d]60 Session::forget('p_id');
61 Session::forget('pe_id');
[cf84baa]62 Session::forget('is_policeman');
63 return redirect('/login');
64 }
[75151c6]65}
Note: See TracBrowser for help on using the repository browser.