347 lines
15 KiB
PHP
347 lines
15 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use App\Models\AdminMaster;
|
||
|
|
use App\Models\User;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use Illuminate\Support\Facades\Mail;
|
||
|
|
use App\Mail\SignInOtpMail;
|
||
|
|
use Illuminate\Support\Facades\Session;
|
||
|
|
use Crypt;
|
||
|
|
use Hash;
|
||
|
|
use DateTime;
|
||
|
|
use DateTimeZone;
|
||
|
|
use DateInterval;
|
||
|
|
// use Auth;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
|
||
|
|
class LoginController extends Controller {
|
||
|
|
|
||
|
|
public function create()
|
||
|
|
{
|
||
|
|
|
||
|
|
return view('Admin.sign_in');
|
||
|
|
}
|
||
|
|
public function getOTP()
|
||
|
|
{
|
||
|
|
$data = array();
|
||
|
|
$data['data'] = $_GET['d'];
|
||
|
|
// echo"<pre>"; print_r($data);exit;
|
||
|
|
return view('Admin.two_steps',$data);
|
||
|
|
}
|
||
|
|
|
||
|
|
// public function sendOtp(Request $request)
|
||
|
|
// {
|
||
|
|
// try {
|
||
|
|
// $msg_data = array();
|
||
|
|
// $validationErrors = $this->validateLogin($request);
|
||
|
|
// if (count($validationErrors)) {
|
||
|
|
// \Log::error("Auth Exception: " . implode(", ", $validationErrors->all()));
|
||
|
|
// $msg_data['data'] = array("success" => '0', "message" => implode("\n", $validationErrors->all()));
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// }
|
||
|
|
// $email = trim(strtolower($request->email));
|
||
|
|
// //check admin exist with this email or not
|
||
|
|
// $admin_exits = AdminMaster::where('email_id', $email)->get();
|
||
|
|
// // echo "<pre>";
|
||
|
|
// // print_r($admin_exits[0]->password);
|
||
|
|
// // die;
|
||
|
|
// if (empty($admin_exits)) {
|
||
|
|
// $msg_data['data'] = array(
|
||
|
|
// "success" => '0',
|
||
|
|
// "message" => "Admin does not exist");
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// }
|
||
|
|
// //verifying password from table
|
||
|
|
// $password = md5($request->password);
|
||
|
|
//
|
||
|
|
// // print_r($admin_exit."<br>");
|
||
|
|
// if ($password == $admin_exits[0]->password)
|
||
|
|
// {
|
||
|
|
// $admin_data = AdminMaster::where('email_id', $email)->where('pin', $request->digit_pin)->first();
|
||
|
|
// if (empty($admin_data))
|
||
|
|
// {
|
||
|
|
// \Log::error("Admin not found with this email id and pin is incorrect.");
|
||
|
|
// $msg_data['data'] = array("success" => '0', "message" => "Please enter correct pin!");
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// if ($admin_data->is_active == 1) {
|
||
|
|
// // print_r($admin_data->is_active);
|
||
|
|
// // die;
|
||
|
|
// // \Log::info("Login Successful!");
|
||
|
|
// $enc_data = Crypt::encrypt($email);
|
||
|
|
// // print_r($enc_data);
|
||
|
|
// // die;
|
||
|
|
// //update otp to table
|
||
|
|
// $otp_data = array();
|
||
|
|
// $otp_data['otp'] = $otp = generateRandomOTP();
|
||
|
|
// $checkOtp = AdminMaster::where('email_id', $email)->first();
|
||
|
|
// $update_otp = AdminMaster::find($checkOtp->id); //->update($otp_data);
|
||
|
|
// $update_otp->otp = $otp;
|
||
|
|
// $update_otp->save();
|
||
|
|
// // print_r($update_otp);
|
||
|
|
// // die;
|
||
|
|
//
|
||
|
|
// //send email otp section start
|
||
|
|
// // $body = "Login Verification, please use the verification code to sign in Code " . $otp;
|
||
|
|
// // Mail::send("Admin.auth.send_otp", ['body'=> $body], function ($message) use ($email) {
|
||
|
|
// // $message->from('pradyumn@wdimails.com', 'WOKA');
|
||
|
|
// // $message->to($email, 'WOKA')->subject('Woka Sign in OTP');
|
||
|
|
// // });
|
||
|
|
//
|
||
|
|
// $msg_data['data'] = array("success" => '1', "message" => "OTP has been sent to your mails. " . $otp, "redirect" => "two_step", "email" => $enc_data);
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// } else {
|
||
|
|
// \Log::error("Account Suspended.");
|
||
|
|
// $msg_data['data'] = array("success" => '0', "message" => 'Account is suspended.');
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// }
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// } else
|
||
|
|
// {
|
||
|
|
// $msg_data['data'] = array("success" => '0', "message" => 'Incorrect Password.');
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// }
|
||
|
|
// return response()->json($msg_data);
|
||
|
|
// } catch (\Exception $e) {
|
||
|
|
// \Log::error("Login failed: " . $e->getMessage());
|
||
|
|
// return response()->json(array("success" => '0', "message" => 'Something Went Wrong.'));
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
public function sendOtp(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$msg_data = array();
|
||
|
|
$validationErrors = $this->validateLogin($request);
|
||
|
|
// print_r($validationErrors); exit;
|
||
|
|
if (count($validationErrors)) {
|
||
|
|
\Log::error("Auth Exception: " . implode(", ", $validationErrors->all()));
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => implode("\n", $validationErrors->all()));
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
|
||
|
|
$email = trim(strtolower($request->email));
|
||
|
|
//check admin exist with this email or not
|
||
|
|
$admin_exits = AdminMaster::where('email_id', $email)->first();
|
||
|
|
// print_r($admin_exits); exit;
|
||
|
|
if (empty($admin_exits)){
|
||
|
|
$msg_data['data'] = array(
|
||
|
|
"success" => '0',
|
||
|
|
"message" => "Admin does not exist!");
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
//verifying password from table
|
||
|
|
// print_r($admin_exits->password);exit;
|
||
|
|
//dd( (Hash::check(trim($request->password), $admin_exits->password) )
|
||
|
|
// ,$request->password,$admin_exits->password);
|
||
|
|
$verify_pass = Hash::check($request->password,$admin_exits->password);
|
||
|
|
// print_r($verify_pass); exit;
|
||
|
|
if ($verify_pass){
|
||
|
|
$admin_data = AdminMaster::where([['email_id', $email], ['pin', $request->digit_pin]])->first();
|
||
|
|
if (empty($admin_data)) {
|
||
|
|
\Log::error("Admin not found with this email id and pin is incorrect.");
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => "Please enter correct pin!");
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($admin_data->is_active == 1) {
|
||
|
|
// \Log::info("Login Successful!");
|
||
|
|
$enc_data = Crypt::encrypt($email);
|
||
|
|
|
||
|
|
//update otp to table
|
||
|
|
$now = Carbon::now('Asia/Kolkata')->format('Y-m-d H:i:s');
|
||
|
|
$otp_data = array();
|
||
|
|
// generateRandomOTP();
|
||
|
|
$otp_data['otp'] = $otp = 1234;
|
||
|
|
$checkOtp = AdminMaster::where('email_id', $email)->first();
|
||
|
|
$update_otp = AdminMaster::find($checkOtp->id);//->update($otp_data);
|
||
|
|
$update_otp->otp = $otp;
|
||
|
|
$update_otp->updated_at = $now;
|
||
|
|
$update_otp->save();
|
||
|
|
|
||
|
|
//send email otp section start
|
||
|
|
$body = $otp;
|
||
|
|
$mail = Mail::send("Admin.Templates.sign_in_otp", ['body'=> $body], function ($message) use ($email) {
|
||
|
|
$message->from('getsetfit@gmail.com', 'GSF');
|
||
|
|
$message->to($email, 'GSF')->subject('Gsf Sign in OTP');
|
||
|
|
});
|
||
|
|
if ($mail){
|
||
|
|
$msg_data['data'] = array("success" => '1', "message" => "OTP has been sent to your mail.", "redirect" => "two_step", "email_id" => $enc_data);
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}else{
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => "Failed to send OTP. Please try again later.");
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
\Log::error("Account Suspended.");
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => 'Account is suspended.');
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => 'Incorrect Password!');
|
||
|
|
return response()->json($msg_data);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
\Log::error("Login failed: " . $e->getMessage());
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => 'Something Went Wrong!');
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// * Uses : Validates input login request
|
||
|
|
public function validateLogin(Request $request)
|
||
|
|
{
|
||
|
|
return \Validator::make($request->all(),
|
||
|
|
[
|
||
|
|
'email' => 'required|email|string',
|
||
|
|
'password' => 'required|string',
|
||
|
|
'digit_pin' => 'required|digits:4'
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'email.required' => 'Please enter your email.',
|
||
|
|
'password.required' => 'Please enter your password.',
|
||
|
|
'pin.required' => 'Please enter your pin.',
|
||
|
|
'digit_pin.digits' => 'Pin must be 4 digit only.'
|
||
|
|
])->errors();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function verifyLogin(Request $request)
|
||
|
|
{
|
||
|
|
// print_r($request->all);exit;
|
||
|
|
try{
|
||
|
|
$msg_data = array();
|
||
|
|
$validationErrors = $this->validateVerifyLoginRequest($request);
|
||
|
|
if (count($validationErrors)) {
|
||
|
|
\Log::error("Auth Exception: " . implode(", ", $validationErrors->all()));
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => implode("\n", $validationErrors->all()));
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
|
||
|
|
$email = trim(strtolower(decrypt($request->d)));
|
||
|
|
// print_r($request->otp);
|
||
|
|
// die;
|
||
|
|
// $email = decrypt($request->email);
|
||
|
|
$admin_data = AdminMaster::where([['email_id', $email],['otp', $request->otp]])->first();
|
||
|
|
if(empty($admin_data)){
|
||
|
|
$msg_data['data'] = array("success" => '0', "message" => 'Invalid OTPd.');
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
|
||
|
|
// convert UTc time zone to local
|
||
|
|
$datetime = new DateTime($admin_data->updated_at);
|
||
|
|
$timezone = new DateTimeZone('Asia/Kolkata'); // or whatever zone you're after
|
||
|
|
$datetime->setTimezone($timezone);
|
||
|
|
$update_datetime = $datetime->format('Y-m-d H:i:s');
|
||
|
|
|
||
|
|
//add 5 minut to updated at
|
||
|
|
$minutes_to_add = 5;
|
||
|
|
$time = new DateTime($update_datetime);
|
||
|
|
$time->add(new DateInterval('PT' . $minutes_to_add . 'M'));
|
||
|
|
$timestamp = $time->format('Y-m-d H:i:s');
|
||
|
|
|
||
|
|
//current time
|
||
|
|
$now = Carbon::now('Asia/Kolkata')->format('Y-m-d H:i:s');
|
||
|
|
|
||
|
|
if($admin_data){
|
||
|
|
if($now > $timestamp){
|
||
|
|
$msg_data = array("success" => '0', "message" => 'OTP has been expired.');
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
else{
|
||
|
|
// if(!empty($admin_data)){
|
||
|
|
//creating session if otp is verified
|
||
|
|
$data = array(
|
||
|
|
"id"=>$admin_data->id,
|
||
|
|
"email_id"=>$email,
|
||
|
|
);
|
||
|
|
// print_r($data);
|
||
|
|
// die;
|
||
|
|
// session_start();
|
||
|
|
// $request->session()->put('data',$data);
|
||
|
|
// $sessionDetails = Session::put('data', $data);
|
||
|
|
Session::put('data', $data);
|
||
|
|
$request->session()->put('data',$data);
|
||
|
|
// $sessionDetails= session(['data' => $data]);
|
||
|
|
|
||
|
|
// dd($data,$sessionDetails);
|
||
|
|
// \Session::save();
|
||
|
|
|
||
|
|
// print_r(session('data'));exit;
|
||
|
|
// die;
|
||
|
|
$msg_data['data'] = array("success" => '1', "message" => 'Sign In Successfully.', "redirect" => "dashboard");
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
}else{
|
||
|
|
$msg_data = array("success" => '0', "message" => 'Invalid OTP.');
|
||
|
|
return response()->json($msg_data);
|
||
|
|
}
|
||
|
|
|
||
|
|
}catch (\Exception $e) {
|
||
|
|
\Log::error("Login failed: " . $e->getMessage());
|
||
|
|
return response()->json(array("success" => '0', "message" => 'Something Went Wrong.'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function validateVerifyLoginRequest(Request $request)
|
||
|
|
{
|
||
|
|
return \Validator::make($request->all(),
|
||
|
|
[
|
||
|
|
'otp' => 'required|digits:4',
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'otp.required' => 'Please enter 4 digit OTP.',
|
||
|
|
'otp.digits' => 'OTP must be 4 digit.'
|
||
|
|
])->errors();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function adminPostLogin(Request $request)
|
||
|
|
{
|
||
|
|
// print_r($request->all());
|
||
|
|
// exit;
|
||
|
|
$request->validate([
|
||
|
|
'email' => 'required',
|
||
|
|
'password' => 'required',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$credentials = $request->only('email', 'password');
|
||
|
|
|
||
|
|
$admins = AdminMaster::where('email_id', $request->email)->first();
|
||
|
|
// dd($admins);
|
||
|
|
// $loginInfo = AdminMaster::where('email_id',$request->email)->first();
|
||
|
|
// dd($credentials,Auth::attempt($credentials));
|
||
|
|
// dd(Auth::login($admins));
|
||
|
|
// Auth::attempt(['email_id' => $request->email,'password' => $request->password]);
|
||
|
|
// dd(Auth::attempt(['email_id' => $request->email,'password'=> $request->password]));
|
||
|
|
if ($admins && Auth::guard('admin')->attempt(['email_id' => $request->email,'password'=> $request->password])) {
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// dd(Auth::guard('admin')->attempt(['email_id' => $request->email,'password'=> $request->password]));
|
||
|
|
$user = Auth::guard('admin')->user();
|
||
|
|
// dd($user);
|
||
|
|
// $token = JWTAuth::fromUser($user); // Generate JWT token for the admin
|
||
|
|
|
||
|
|
// Store the token in the session
|
||
|
|
// Session::put('admin_token', $token);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
return response(['success'=>true, 'message'=>"You Have Logged in Successfully"]);
|
||
|
|
// ->withHeaders(['X-admin-token' => $token]);
|
||
|
|
|
||
|
|
// return redirect()->intended('dashboard')->withSuccess('You have Successfully logged in');
|
||
|
|
}else{
|
||
|
|
|
||
|
|
return response(['success'=>false, 'message'=>"Invalid! Email or Password "]);
|
||
|
|
}
|
||
|
|
// return redirect()->route('login')->with('error', 'ddddInvalid email or password.');
|
||
|
|
// return redirect("login");
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|