<?php
namespace App\Http\Controllers\User;
use Exception;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Constants\GlobalConst;
use App\Models\UserAuthorization;
use App\Http\Controllers\Controller;
use App\Traits\ControlDynamicInputFields;
use App\Providers\Admin\BasicSettingsProvider;
use Illuminate\Validation\ValidationException;
use App\Notifications\User\Auth\SendAuthorizationCode;
class AuthorizationController extends Controller
{
use ControlDynamicInputFields;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function showMailFrom($token)
{
$page_title = "Mail Authorization";
$user_authorize = UserAuthorization::where("token",$token)->first();
$resend_time = 0;
if(Carbon::now() <= $user_authorize->created_at->addMinutes(GlobalConst::USER_PASS_RESEND_TIME_MINUTE)) {
$resend_time = Carbon::now()->diffInSeconds($user_authorize->created_at->addMinutes(GlobalConst::USER_PASS_RESEND_TIME_MINUTE));
}
$email = $user_authorize->user->email;
return view('user.auth.authorize.verify-mail',compact("page_title","token","resend_time","email"));
}
/**
* Verify authorization code.
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function mailVerify(Request $request,$token)
{
$request->merge(['token' => $token]);
$request->validate([
'token' => "required|string|exists:user_authorizations,token",
'code.*' => "required|integer",
]);
$code = implode("",$request->code);
$otp_exp_sec = BasicSettingsProvider::get()->otp_exp_seconds ?? GlobalConst::DEFAULT_TOKEN_EXP_SEC;
$auth_column = UserAuthorization::where("token",$request->token)->where("code",$code)->first();
if(!$auth_column) return back()->with(['error' => ['invalid Token!']]);
if($auth_column->created_at->addSeconds($otp_exp_sec) < now()) {
$this->authLogout($request);
return redirect()->route('index')->with(['error' => ['Session expired. Please try again']]);
}
try{
$auth_column->user->update([
'email_verified' => true,
]);
$auth_column->delete();
}catch(Exception $e) {
$this->authLogout($request);
return redirect()->route('index')->with(['error' => ['Something went wrong! Please try again']]);
}
return redirect()->intended(route("user.dashboard"))->with(['success' => ['Account successfully verified']]);
}
/**
* Method for resend mail using token
*/
public function mailResend($token) {
$user_authorize = UserAuthorization::where("token",$token)->first();
if(!$user_authorize) return back()->with(['error' => ['Request token is invalid']]);
if(Carbon::now() <= $user_authorize->created_at->addMinutes(GlobalConst::USER_PASS_RESEND_TIME_MINUTE)) {
throw ValidationException::withMessages([
'code' => 'You can resend verification code after '.Carbon::now()->diffInSeconds($user_authorize->created_at->addMinutes(GlobalConst::USER_PASS_RESEND_TIME_MINUTE)). ' seconds',
]);
}
$resend_code = generate_random_code();
try{
$user_authorize->update([
'code' => $resend_code,
'created_at' => now(),
]);
$data = $user_authorize->toArray();
try{
$user_authorize->user->notify(new SendAuthorizationCode((object) $data));
}catch(Exception $e){}
}catch(Exception $e) {
throw ValidationException::withMessages([
'code' => "Something went wrong! Please try again.",
]);
}
return redirect()->route('user.authorize.mail',$token)->with(['success' => ['Mail Resend Success!']]);
}
public function authLogout(Request $request) {
auth()->guard("web")->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
}
public function showGoogle2FAForm() {
$page_title = "Authorize Google Two Factor";
return view('user.auth.authorize.verify-google-2fa',compact('page_title'));
}
public function google2FASubmit(Request $request) {
$request->validate([
'code*' => "required|integer",
]);
$code = implode($request->code);
$user = auth()->user();
if(!$user->two_factor_secret) {
return back()->with(['warning' => ['Your secret key not stored properly. Please contact with system administrator']]);
}
if(google_2fa_verify($user->two_factor_secret,$code)) {
$user->update([
'two_factor_verified' => true,
]);
return redirect()->intended(route('user.dashboard'));
}
return back()->with(['warning' => ['Failed to login. Please try again']]);
}
}
Contact Section
We’d love to hear from you! Whether you have questions, feedback, or need support, our team is here to help. Reach out to us via email, phone, or visit our office. We’re committed to providing you with exceptional service and ensuring your experience with NFC Pay is seamless and satisfying. Let’s connect!