source: app/Http/Controllers/SessionsController.php@ 3c89e27

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

Refactored duplicate code

  • Property mode set to 100644
File size: 3.1 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;
10use Illuminate\Support\Facades\Hash;
11
12class SessionsController extends Controller
13{
14 function verifyPassword($inputPassword, $storedHash, $salt)
15 {
16 $hashedInputPassword = crypt($inputPassword . $salt, $storedHash);
17
18 return $hashedInputPassword === $storedHash;
19 }
20 public function store()
21 {
22 $credentials = request()->validate([
23 'badge_no' => 'required',
24 'password' => 'required'
25 ]);
26
27 $password = $credentials['password'];
28 $badge_no = $credentials['badge_no'];
29 if (!is_numeric($badge_no)) {
30 // Redirect back with an error message
31 return back()->withErrors(['password' => 'Invalid credentials']);
32 }
33 // mozhe da se najavi kako policaec i kako officer, znaeme koj e koj po znachkata
34
35 $policeman = true;
36 $is_policeman = DB::select('select * from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
37 $is_officer = DB::select('select * from officer where o_badge_no = :badge_no;', ['badge_no' => $badge_no]);
38 if($is_officer==null && $is_policeman==null) {
39 return back()->withErrors(['password' => 'Invalid credentials']);
40 }
41 if($is_officer!=null) {
42 $pass = DB::select('select o_password from officer where o_badge_no = :o_badge_no;', ['o_badge_no' => $badge_no]);
43 $salt = DB::select('select salt from officer where o_badge_no = :o_badge_no;', ['o_badge_no' => $badge_no]);
44 $policeman = false;
45 } else {
46 $pass = DB::select('select p_password from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
47 $salt = DB::select('select salt from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
48
49 }
50
51 foreach ($pass[0] as $key => $val) {
52 $value = $val;
53 break; // Break after the first key-value pair
54 }
55 foreach ($salt[0] as $key => $val) {
56 $value2 = $val;
57 break; // Break after the first key-value pair
58 }
59 if ($this->verifyPassword($password, $value, $value2)) {
60 // Authentication passed
61 Session::put('auth', true);
62 Session::put('badge_no', $badge_no);
63 Session::put('is_policeman', $policeman);
64 if($policeman){
65 Session::put('pe_id', $is_policeman[0]->pe_id);
66 Session::put('p_id', $is_policeman[0]->p_id);
67 } else {
68 Session::put('pe_id', $is_officer[0]->pe_id);
69 }
70 return view('welcome');
71 }
72
73 // Authentication failed
74 return back()->withErrors(['password' => 'Invalid credentials']);
75 }
76 public function logout()
77 {
78 Session::forget('badge_no');
79 Session::forget('p_id');
80 Session::forget('pe_id');
81 Session::forget('is_policeman');
82 return redirect('/login');
83 }
84}
Note: See TracBrowser for help on using the repository browser.