daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Apr 26th, 2018 226 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Auth;
  4.  
  5.  
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8.  
  9. use App\Http\Requests;
  10. use App\Http\Controllers\Controller;
  11. use Illuminate\Foundation\Auth\ResetsPasswords;
  12. use DB;
  13. use Hash;
  14. use Carbon;
  15. use App\User;
  16. use Ramsey\Uuid\Uuid;
  17. use Alert;
  18. use Mail;
  19.  
  20. class PasswordController extends Controller
  21. {
  22.     /*
  23.     |--------------------------------------------------------------------------
  24.     | Password Reset Controller
  25.     |--------------------------------------------------------------------------
  26.     |
  27.     | This controller is responsible for handling password reset requests
  28.     | and uses a simple trait to include this behavior. You're free to
  29.     | explore this trait and override any methods you wish to tweak.
  30.     |
  31.     */
  32.  
  33.     use ResetsPasswords;
  34.  
  35.     /**
  36.      * Create a new password controller instance.
  37.      *
  38.      * @return void
  39.      */
  40.     public function __construct()
  41.     {
  42.         $this->middleware('guest');
  43.     }
  44.  
  45.         public function index()
  46.       {
  47.         return view('auth/forget');
  48.       }
  49.  
  50.   public function sendPasswordResetToken(Request $request)
  51. {
  52.     $user = User::where('email', $request->email)->first();
  53.     if ( !$user ) return redirect()->back()->withErrors(['error' => '404']);
  54.  
  55.     //create a new token to be sent to the user.
  56.     DB::table('password_resets')->insert([
  57.         'email' => $request->email,
  58.         'token' => str_random(60), //change 60 to any length you want
  59.         'created_at' => Carbon\Carbon::now()
  60.     ]);
  61.  
  62.     $tokenData = DB::table('password_resets')
  63.     ->where('email', $request->email)->first();
  64.  
  65.    $token = $tokenData->token;
  66.    $email = $request->email; // or $email = $tokenData->email;
  67.  
  68.     $data = array(
  69.       'email' => $email,
  70.       'token' => $token,
  71.     );
  72.     Mail::send('mail.reset-password',$data, function($message) use ($request) {
  73.         $message->to($request['email']);
  74.         $message->from('no-reply@mootivo.com', 'Admin Mootivo');
  75.         $message->subject('[MOOTIVO] Reset Password');
  76.     });
  77.    
  78.   Alert::success('Kami mengirimkan email untuk melakukan reset password', 'Email terkirim!');
  79.   return redirect('login');
  80. }
  81.  
  82.    public function showPasswordResetForm($token)
  83.  {
  84.      $tokenData = DB::table('password_resets')
  85.      ->where('token', $token)->first();
  86.  
  87.      if ( !$tokenData ) return redirect()->to('home');
  88.      return view('auth.reset')->with(
  89.             ['token' => $token, 'email' => $tokenData->email]);
  90.  }
  91.  
  92.    public function resetPassword(Request $request, $token)
  93.  {
  94.      $this->validate($request, [
  95.        'token' => 'required',
  96.        'email' => 'required|email',
  97.        'password' => 'required|confirmed',
  98.    ]);
  99.  
  100.      $password = $request->password;
  101.      $tokenData = DB::table('password_resets')
  102.      ->where('token', $token)->first();
  103.  
  104.      $user = User::where('email', $tokenData->email)->first();
  105.      if ( !$user ) return redirect()->to('home'); //or wherever you want
  106.  
  107.      $user->password = Hash::make($password);
  108.      $user->update(); //or $user->save();
  109.      
  110.      //do we log the user directly or let them login and try their password for the first time ? if yes
  111.      Auth::login($user);
  112.  
  113.     // If the user shouldn't reuse the token later, delete the token
  114.     DB::table('password_resets')->where('email', $user->email)->delete();
  115.  
  116.       Alert::success('Selamat anda sudah kembali ke Member Area!', 'Password Berhasil Direset!');
  117.        return redirect('dashboard');
  118.     //redirect where we want according to whether they are logged in or not.
  119.  }
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top