|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/navinclasses.studylms.in/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Otpsession;
use App\Models\User;
use DB;
use Illuminate\Support\Facades\Session;
use Postal\Client;
use App\Helpers\Frontend;
class FrontendAuthController extends Controller
{
public function sign_up(Request $request)
{
// dd($request->session()->all());
$admin_id = 2;
$data['title'] = 'Join Navin Classes';
$data['frontMenu'] = 'home';
return view('register', $data);
}
public function sign_up_continue(Request $request)
{
//dd($request);
$rules = array(
'name' => "required",
'phone' => "required|unique:users|numeric",
'email' => "required|unique:users|email",
'password' => 'min:6|required_with:cpassword|same:cpassword',
'cpassword' => 'min:6'
);
$messages = array(
'name.required' => 'Please enter a name.',
'email.required' => 'Please enter a email.',
'email.unique' => 'Email already registered',
'phone.required' => 'Please enter a mobile.',
'phone.unique' => 'Phone already registered.',
'password.required' => 'Please enter a password.',
'cpassword.required_with' => 'Please enter a confirm password.',
'password.same' => 'The password and confirm password must match.',
'password.min' => 'The password must 6 characters.',
'cpassword.min' => 'The confirm password must 6 characters.',
);
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
}
// New Code
$phone = $request->phone;
$OTP_DATA = Frontend::send_otp($phone);
if (empty($OTP_DATA)) {
return redirect('account-verification')->with('error', 'OTP failed, Please try again.');
}
// Send Mail
$TO_EMAIL = $request->email;
$SUBJECT = "Navin Classes | Verification";
$MAIL_OTP = rand(111111, 999999);
$HTML = "Dear Student, use OTP code $MAIL_OTP to verify your account, Navin Classes";
$resMail = Frontend::sendMailPostal($TO_EMAIL, $SUBJECT, $HTML);
if (!empty($resMail->result->message_id)) {
$data = array(
'message_id' => $resMail->result->message_id,
'email' => $TO_EMAIL,
'otp' => $MAIL_OTP
);
$OTP_DATA['email'] = $TO_EMAIL;
$OTP_DATA['email_message_id'] = $resMail->result->message_id;
$OTP_SESSION = Otpsession::create($data);
$hashed = Hash::make($request->password);
$OTP_DATA['name'] = $request->name;
$OTP_DATA['password_hash'] = $hashed;
Session::put('otp_data', $OTP_DATA);
return redirect('account-verification')->with('success', 'OTP send to your phone and email!');
}
// End New Code
return redirect('account-verification')->with('error', 'OTP failed, Please try again.');
}
// Login Form
public function sign_in(Request $request)
{
$admin_id = 2;
$data['title'] = 'Sign in';
$data['frontMenu'] = 'home';
return view('login', $data);
}
// Post Login form
public function sign_in_continue(Request $request)
{
$rules = array(
'emailorphone' => 'required',
'password' => 'required',
);
$messages = array(
'emailorphone.required' => 'Please enter a valid Email or Phone.',
'password.min' => 'Please enter valid password.'
);
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
}
$mobilePattern = "/^[7-9][0-9]{9}$/";
$data = array();
if (preg_match($mobilePattern, $request->emailorphone)) {
$data = array(
'phone' => $request->emailorphone,
'password' => $request->password,
);
} else {
$data = array(
'email' => $request->emailorphone,
'password' => $request->password,
);
}
if (Auth::guard('web')->attempt($data)) {
// Check Email or Phone Exist or not
$res = User::where(['email' => $request->emailorphone])->orWhere(['phone' => $request->emailorphone])->first();
$sessionData = ['id' => $res->id, 'user_unique_id' => $res->user_unique_id, 'username' => $res->name, 'email' => $res->email, 'phone' => $res->phone];
$request->session()->put('login_data', $sessionData);
return redirect('/');
} else {
return back()->with('error', 'invalid Email Or Password!');
}
}
public function account_verification(Request $request)
{
$admin_id = 2;
$data['title'] = 'Account Verification';
$data['frontMenu'] = 'home';
$data['userData'] = Session::get('otp_data');
if (empty($data['userData'])) {
return redirect('sign-up')->with('error', 'OTP session expired, Please try again.');
}
return view('account_verification', $data);
}
public function account_verification_continue(Request $request)
{
$rules = array(
'name' => "required",
'phone' => "required|numeric",
'phone_message_id' => "required",
'phone_otp' => "required|min:6",
'email' => "required|email",
'email_message_id' => "required",
'email_otp' => "required|min:6",
'password_hash' => 'required',
);
$messages = array(
'phone_otp.required' => 'Please enter a phone OTP.',
'phone_otp.min' => 'The OTP must 6 characters.',
'email_otp.required' => 'Please enter a email OTP.',
'email_otp.min' => 'The OTP must 6 characters.',
);
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
}
$phone = $request->phone;
$phoneOTP = $request->phone_otp;
$phone_message_id = $request->phone_message_id;
$email = $request->email;
$emailOTP = $request->email_otp;
$email_message_id = $request->email_message_id;
$name = $request->name;
$password_hash = $request->password_hash;
// Phone OTP Check
$checkMobileOTP = Otpsession::where(['message_id' => $phone_message_id, 'phone' => $phone, 'otp' => $phoneOTP])->count();
if ($checkMobileOTP < 1) {
return redirect('account-verification')->with('error', 'OTP not matched!, please try again');
}
// Email OTP Check
$checkEmailOTP = Otpsession::where(['message_id' => $email_message_id, 'email' => $email, 'otp' => $emailOTP])->count();
if ($checkEmailOTP < 1) {
return redirect('account-verification')->with('error', 'OTP not matched!, please try again');
}
// Register student
if ($checkEmailOTP > 0 && $checkMobileOTP > 0) {
$studentdata = array(
'user_unique_id' => uniqid(),
'ip_address' => $_SERVER['REMOTE_ADDR'],
'admin_id' => '2',
'name' => $name,
'email' => $email,
'phone' => $phone,
'password' => $password_hash,
);
$res = User::create($studentdata);
if (empty($res)) {
return redirect('account-verification')->with('error', 'Something went wrong!, please try again');
}
Session::put('otp_data', array());
return redirect('sign-up')->with('success', 'Register Successfully!');
}
return redirect('account-verification')->with('error', 'OTP expired!, please try again');
}
public function forgot_password(Request $request)
{
$admin_id = 2;
$data['title'] = 'Forgot password';
$data['frontMenu'] = 'home';
return view('forgot_password', $data);
}
public function forgot_password_continue(Request $request)
{
$rules = array(
'email' => "required|exists:users|email",
);
$messages = array(
'email.required' => 'Please enter a email OTP.',
'email.exists' => 'Your email not registered with us, please sign up.',
);
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
}
$TO_EMAIL = $request->email;
$_TOKEN = md5(uniqid());
$dataToken = array(
'remember_token' => $_TOKEN
);
$userData = User::where(['email' => $TO_EMAIL, 'status' => 1])->update($dataToken);
if ($userData) {
$SUBJECT = "Navin Classes | Forgot Password Link";
$URL = url('/');
$HTML = "Dear Student, Follow the instruction to reset password<br><br><a href='" . $URL . "/reset-password-link/" . $_TOKEN . "'>Click here to reset your password</a>";
// Send Mail
Frontend::sendMailPostal($TO_EMAIL, $SUBJECT, $HTML);
return redirect('forgot-password')->with('success', 'We sent you a message at: ' . $TO_EMAIL . ' Follow the link in that message to reset your password');
}
return redirect('forgot-password')->with('error', 'Account is deactivated, please contant us');
}
public function reset_password_link($_token)
{
$admin_id = 2;
$data['title'] = 'Reset your password';
$data['frontMenu'] = 'home';
$checkToken = User::where(['remember_token' => $_token])->count();
if ($checkToken < 1) {
return redirect('forgot-password')->with('error', 'Link expired, please try again');
}
$data['token'] = $_token;
return view('reset_password_link', $data);
}
public function reset_password_link_continue(Request $request)
{
$rules = array(
'remember_token' => "required|exists:users",
'password' => 'min:6|required_with:re_password|same:re_password',
're_password' => 'min:6'
);
$messages = array(
'remember_token.required' => 'Token is expired, please try again.',
'remember_token.exists' => 'Invalid Token, please try again.',
'password.required' => 'Please enter a password.',
're_password.required_with' => 'Please enter a confirm password.',
'password.same' => 'The password and confirm password must match.',
'password.min' => 'The password must 6 characters.',
're_password.min' => 'The confirm password must 6 characters.',
);
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
}
$hashed = Hash::make($request->password);
$data = array(
'password' => $hashed,
'remember_token' => null
);
$res = User::where(['remember_token' => $request->remember_token])->update($data);
if ($res) {
return redirect('sign-in')->with('success', 'Password reset successfully.');
}
return redirect('sign-in')->with('error', 'Invalid password reset link, please try again');
}
public function logout(Request $req)
{
Auth::logout();
$req->session()->invalidate();
$req->session()->regenerateToken();
return redirect('/sign-in');
}
}