source: app/Http/Controllers/SessionsController.php@ 768f473

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

Final features

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