213 lines
7.0 KiB
PHP
213 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Carbon\Carbon;
|
|
use Validator;
|
|
use App\Models\Admin\manage_admin;
|
|
use App\Mail\Backend\AdminOtp;
|
|
|
|
class AdminLoginController extends Controller {
|
|
|
|
public function index() {
|
|
return view('Admin.sign_in');
|
|
}
|
|
|
|
public function two_steps() {
|
|
return view('Admin.two_steps');
|
|
}
|
|
|
|
public function login(Request $request) {
|
|
$validator = Validator::make($request->all(), [
|
|
'email' => [
|
|
'required',
|
|
'email',
|
|
],
|
|
'password' => [
|
|
'required',
|
|
'min:8'
|
|
],
|
|
], [
|
|
'email.required' => 'Please enter your email address.',
|
|
'email.email' => 'Please enter a valid email address.',
|
|
'password.required' => 'Please enter a password.',
|
|
'password.min' => 'Your password must be at least :min characters.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['error' => $validator->errors(), 'status' => 204]);
|
|
}
|
|
|
|
// try {
|
|
$pin = intval($request->digit_pin);
|
|
|
|
$admin = manage_admin::where('email', $request->email)
|
|
->where('pin', $pin)
|
|
->where('status', 1)
|
|
->first();
|
|
|
|
$credentials = [
|
|
'email' => $request->email,
|
|
'password' => $request->password,
|
|
];
|
|
|
|
if ($admin && Hash::check($request->password, $admin->password)) {
|
|
|
|
$otp = rand(1234, 9999);
|
|
if (isset($admin)) {
|
|
Session::put('email', $request->email);
|
|
$admin->otp = $otp;
|
|
$admin_name = $admin->name;
|
|
$admin->expire_at = Carbon::now('Asia/colombo')->addMinutes(2);
|
|
$admin->update();
|
|
Session::put('expire_at', $admin->expire_at);
|
|
$mailData = [
|
|
'email' => $request->email,
|
|
'name' => $admin_name,
|
|
'otp' => $otp,
|
|
];
|
|
$check = Mail::to($request->email)->send(new AdminOtp($mailData));
|
|
|
|
// dd($mailData);
|
|
}
|
|
$sessionDetails = Session::put('data', $admin);
|
|
|
|
return response()->json(['message' => 'Credentials Verified Successfully', 'type' => 'admin', 'status' => 200]);
|
|
} else {
|
|
return response()->json([
|
|
'status' => 401,
|
|
'message' => 'Invalid Credentials',
|
|
]);
|
|
}
|
|
// } catch (\Exception $e) {
|
|
// return response()->json(['error' => 'Netwrok Error! Please try again after sometime.']);
|
|
// }
|
|
}
|
|
|
|
public function verifyOtp(Request $request) {
|
|
$validator = Validator::make($request->all(), [
|
|
'email' => [
|
|
'required',
|
|
'email',
|
|
],
|
|
'otp' => [
|
|
'required',
|
|
],
|
|
], [
|
|
'email.required' => 'Please enter your email address.',
|
|
'email.email' => 'Please enter a valid email address.',
|
|
'otp.required' => 'Please enter the OTP.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['error' => $validator->errors(), 'status' => 204]);
|
|
}
|
|
|
|
// try {
|
|
$admin = manage_admin::where('email', $request->email)
|
|
->where('otp', $request->otp)
|
|
->where('status', 1)
|
|
->where('expire_at', '>', Carbon::now('Asia/colombo'))
|
|
->first();
|
|
// dd($admin);
|
|
|
|
if ($admin) {
|
|
Auth::guard('admin')->login($admin);
|
|
$admin->update([
|
|
'last_login' => now(),
|
|
]);
|
|
return response()->json([
|
|
'message' => 'Logged in Successfully',
|
|
'data' => [
|
|
'name' => $admin->name,
|
|
],
|
|
'type' => 'admin',
|
|
'status' => 200,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'status' => 401,
|
|
'success' => '0',
|
|
'message' => 'Invalid OTP or expired.',
|
|
]);
|
|
}
|
|
// } catch (\Exception $e) {
|
|
// return response()->json(['error' => 'Network Error! Please try again after some time.']);
|
|
// }
|
|
}
|
|
|
|
public function resendOtp(Request $request) {
|
|
$validator = Validator::make($request->all(), [
|
|
'email' => [
|
|
'required',
|
|
'email',
|
|
],
|
|
], [
|
|
'email.required' => 'Please enter your email address.',
|
|
'email.email' => 'Please enter a valid email address.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['error' => $validator->errors(), 'status' => 204]);
|
|
}
|
|
|
|
try {
|
|
$admin = manage_admin::where('email', $request->email)
|
|
->where('status', 1)
|
|
->first();
|
|
|
|
if ($admin) {
|
|
$otp = rand(1234, 9999);
|
|
|
|
$admin->otp = $otp;
|
|
$admin->expire_at = Carbon::now('Asia/Colombo')->addMinutes(2);
|
|
$admin->update();
|
|
|
|
$mailData = [
|
|
'email' => $request->email,
|
|
'name' => $admin->name,
|
|
'otp' => $otp,
|
|
];
|
|
Mail::to($request->email)->send(new AdminOtp($mailData));
|
|
|
|
return response()->json([
|
|
'message' => 'OTP has been resent to your email address.',
|
|
'status' => 200,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'status' => 401,
|
|
'message' => 'Invalid email address or user does not exist.',
|
|
]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => 'Network Error! Please try again after some time.']);
|
|
}
|
|
}
|
|
|
|
// public function logout()
|
|
// {
|
|
//// if (Auth::guard('admin')->check()) {
|
|
//// Auth::guard('admin')->logout();
|
|
//// }
|
|
// $admin = auth()->guard('admin')->user();
|
|
// if ($admin) {
|
|
// $admin->logout();
|
|
// }
|
|
// return redirect()->route('admin_sign_in');
|
|
//
|
|
// }
|
|
|
|
public function logout() {
|
|
auth()->guard('admin')->logout();
|
|
return redirect('/admin_sign_in'); // Redirect to your login page
|
|
}
|
|
|
|
}
|