source: app/Http/Controllers/SessionsController.php@ 092fcda

main
Last change on this file since 092fcda was 092fcda, 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);
[092fcda]47 Session::put('p_id', $is_policeman[0]->p_id);
[d9c4096]48 } else {
[69e9f5d]49 Session::put('pe_id', $is_officer[0]->pe_id);
[d9c4096]50 }
[7e9dadd]51 return view('welcome');
[cf84baa]52 }
53
54 // Authentication failed
55 return back()->withErrors(['password' => 'Invalid credentials']);
56 }
57
58 public function logout()
59 {
60 Session::forget('badge_no');
[69e9f5d]61 Session::forget('p_id');
62 Session::forget('pe_id');
[cf84baa]63 Session::forget('is_policeman');
64 return redirect('/login');
65 }
[75151c6]66}
Note: See TracBrowser for help on using the repository browser.