source: app/Http/Controllers/SessionsController.php@ cf84baa

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

Added querries half done

  • Property mode set to 100644
File size: 1.8 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
16 $credentials = request()->validate([
17 'badge_no' => 'required',
18 'password' => 'required'
19 ]);
20 $password = $credentials['password'];
21 $badge_no = $credentials['badge_no'];
22 $policeman = true;
23 $exists = DB::select('select * from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
24 $pass = DB::select('select p_password from policeman where badge_no = :badge_no;', ['badge_no' => $badge_no]);
25 if($exists == null) {
26 $exists = DB::select('select * from officer where o_badge_no = :badge_no;', ['badge_no' => $badge_no]);
27 $pass = DB::select('select o_password from officer where o_badge_no = :badge_no;', ['badge_no' => $badge_no]);
28 $policeman = false;
29 }
30 if($exists == null) {
31 return back()->withErrors(['badge_no' => 'Invalid credentials']);
32 }
33
34 foreach ($pass[0] as $key => $val) {
35 $value = $val;
36 break; // Break after the first key-value pair
37 }
38
39
40 if ($value == $password) {
41 // Authentication passed
42 Session::put('badge_no', $badge_no);
43 Session::put('is_policeman', $policeman);
44 return redirect()->intended('/');
45 }
46
47 // Authentication failed
48 return back()->withErrors(['password' => 'Invalid credentials']);
49 }
50
51 public function logout()
52 {
53 Session::forget('badge_no');
54 Session::forget('is_policeman');
55 return redirect('/login');
56 }
57}
Note: See TracBrowser for help on using the repository browser.