Current File : /home/tradevaly/public_html/app/Http/Controllers/Frontend/AuthenticationController.php
<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Carbon\Carbon;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\Company;
use Mail;
use Illuminate\Support\Facades\Notification;
use App\Notifications\UserVerification;
use App\Notifications\UserRegister;
use App\Notifications\ForgetCode;
use Illuminate\Support\Facades\Hash;


class AuthenticationController extends Controller
{
    //  register function start
     public function store(Request $request)
    {
        if(User::where('email', $request->email)->first() != null){
             Toastr::error('Email Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back();
        }
        if(User::where('username', $request->username)->first() != null){
             Toastr::error('Username Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back();
        }
        
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required',
            'password' => 'required',
            'category_id' => 'required',
            'username' => 'required',
            'type' => 'required',
            'country_id' => 'required',
            'company' => 'required',
            'g-recaptcha-response' => 'required',
        ]);
        $password = $request->password;
        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->category_id = $request->category_id;
        $user->username = $request->username;
        $user->type = $request->type;
        $user->image = 'image.jpg';
        $user->password = bcrypt($password);
        $user->verify_code = rand(999,9999);
        $user->country_id = $request->country_id;
        $user->package_id = 1;
        $user->next_pay = Carbon::now()->addYear(10);
        $user->save();
        if($user->save()){
            $user->password = $password;
            $company = new Company();
            $company->user_id = $user->id;
            $company->name = $request->company;
            $company->image = 'image.jpg';
            $company->phone = $request->phone;
            if($company->save()){
                $user->user_password = $request->password;
                Notification::send($user, new UserRegister($user));
                Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new UserRegister($user));
                Notification::send($user, new UserVerification($user));
                 if($request->admin_user_create){
                         Toastr::success('Registration Successfull');
                      return back();
                    }

                Auth::loginUsingId($user->id);
                Toastr::success('Registration Successfull Please Check Your Email !! For Login', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return view('thankyou');
            }
        }
    }
    public function register(){
        if(Auth::check()){
            return redirect('/user/dashboard');
        }
        else{ 
        return view('frontend.pages.auth.register');
        }
    }
    // register funciton end
     
    // Verification function start
    
    public function verify(){
         if(Auth::check() && Auth::user()->email_verified_at == null){
             return view('frontend.pages.auth.verification');
         }
         return redirect('/user/dashboard');
         
    }
    
    public function resend(){
       if(Auth::check() && Auth::user()->email_verified_at == null){
          $user = User::findOrFail(Auth::id());
          $user->verify_code = rand(999,9999);
          $user->save();
          
          Notification::send($user, new UserVerification($user));
          Toastr::success('Verification Code Send Succefully!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
        return view('frontend.pages.auth.verification');  
        }
         Toastr::success('Something Went Wrong!!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
         return back();
         
    }
    
    public function verify_check(Request $request){
        $request->validate(['code' => 'required']);
        $user = User::find(Auth::id());
        if($user->verify_code == $request->code){
            $user->email_verified_at = Carbon::now();
            $user->verify_code = null;
            $user->save();
            return redirect('user/dashboard');
        }
        Toastr::error('Invalid Verification Code!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
         return back();
    }
    // verification Function end
    
    // Login Function start here
    public function login(){
        if(Auth::check()){
            return redirect()->route('user.dashboard');
        }
        return view('frontend.pages.auth.login');
    }
     public function login_switch(Request $request)
    {
        $request->validate([
            'email' => ['required','email'],
            'password' => 'required',
        ]);
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            return redirect()->route('user.dashboard')
                        ->withSuccess('You have Successfully loggedin');
        }
  
        return redirect("login")->withSuccess('Oppes! You have entered invalid credentials');
    }
    // login function end
    
    // Forget password
    public function forget(){
        return view('frontend.pages.auth.forget');
    }
    public function forget_code($email){
        $user = User::where('email',$email)->where('verify_code','!=',null)->firstOrFail();
        return view('frontend.pages.auth.forget_code',compact('email'));
    }
    // Forget password
    public function forget_codesend(Request $request){
        $user = User::where('email',$request->email)->firstOrFail();
        if($user){
            $user->verify_code = rand(999,9999);
            $user->save();
          Notification::send($user, new ForgetCode($user));
          Toastr::success('Verification Code Send Succefully!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
        return redirect()->route('front.user.forget.code',$user->email);
        }
    }
    public function code_check(Request $request){
        $request->validate([
            'email' => ['required','email'],
            'code' => 'required',
        ]);
        $user = User::where('email',$request->email)->firstOrFail();
        
          if($user->verify_code == $request->code){
            $user->verify_code = null;
            $user->password_forget_time = Carbon::now()->addMinutes(30);
            $user->save();
            $time = Carbon::parse($user->password_forget_time)->format('Y-M-d-H-i-s-A');
            $email = $user->email;
            $id = $user->id;
             Toastr::success('Verify Successfully!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
             return redirect()->route('front.user.forget.new_password', ['email' => $email, 'time' => $time, 'id' => $id]);
        }
         Toastr::error('Verification Code Does not Match!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
         
        return back();
    }
     public function new_password($email,$time,$id){
          $user = User::where('email',$email)->where('id',$id)->first();
         
            
         if($user){
                   if(Carbon::now()->lt($user->password_forget_time)){
                    return  view('frontend.pages.auth.forget_new_password',compact('email','id'));
                   }
                   else{
                         Toastr::error('Password Change time expires try again!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
                         return redirect()->route('front.user.forget');
                  }
          }
          Toastr::error('Something went wrong please try again!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
         return redirect()->route('front.user.login');
    }
     public function change_password(Request $request,$email){
         $request->validate([
        'id' => 'required',
        'new_password' => 'required|string|min:8',
        'confirm_password' => 'required|same:new_password',
    ]);
    $user = User::where('email',$email)->where('id',$request->id)->first();
    if($user){
        $user->password = Hash::make($request->new_password);
        $user->password_forget_time = null;
        $user->save();
         Toastr::success('Password Change Successfully!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
         return redirect()->route('front.user.login');
    }
    Toastr::error('Something went wrong please try again!!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
    return back();

    
    }
}