bobmyles
1156
2
Laravel

How do I change the default register and login controllers in laravel 5.3?

Posted 2 years ago by bobmyles

I would want to have this scenario that when a user signs up, an activation token is generated and sent to the email address specified.

On the users table, I have $table->boolean('is_activated')->default(0); that should be updated to 1 if the email link is clicked by the user. Hence the user is able to login as by the login controller below.

My Routes:

Auth::routes();
Route::get('/user/activation/{token}', 'Auth\[email protected]');

Login Controller:

 <?php
 namespace App\Http\Controllers\Auth;

 use App\Http\Controllers\Controller;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;
 use Illuminate\Foundation\Auth\ThrottlesLogins;
 use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
 {
  use AuthenticatesUsers;
   public function authenticate()
    {
    if (\Auth::attempt(['email' => $email, 'password' => $password, 'is_activated' => 1])) 
    {
     // The user is active, not suspended, and exists.
           // Logic that determines where to send the user
            if (\Auth::user()->org_name == 'Dibon') 
            {
               return redirect()->intended('/main-organization/dashboard');
            }
            else
            {
               return redirect()->intended('/organization/dashboard');
            } 
    }
               }

public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}

However, for some reasons the application kind of by passes this authenticate() method. I can login even without the is_activated being 1. Meaning the user can log in even with out clicking the token link.

I can't tell where I am making the mistake.

My register controller is as shown below:

 <?php

 namespace App\Http\Controllers\Auth;
 use App\User;
 use Validator;
 use App\Http\Controllers\Controller;
 use Illuminate\Foundation\Auth\RegistersUsers;
 use Illuminate\Http\Request;
 use DB;
 use Mail;

class RegisterController extends Controller
{

use RegistersUsers;
protected function redirectTo()
{
  if (\Auth::user()->org_name == 'Dibon') 
    {
       return redirect()->intended('/main-organization/dashboard');
    }

    else{
       return redirect()->intended('/organization/dashboard');
   }    
}

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'phone' => 'required|min:6',
        'password' => 'required|min:6|confirmed',
        'org_name' => 'required|max:255',
    ]);
}

protected function create(array $data)
{  
$check = DB::table('users')->where('email', $data['email'])->first();
      if ($check === null) 
      { 
       return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'phone' => $data['phone'],
        'password' => bcrypt($data['password']),
        'org_name' => $data['org_name'],
    ]);

      return redirect('/main-organization/view-users')->with('message', 'The Organization has been created!');
      } 

     else //org already exist  {
        return redirect('/main-organization/view-users')->with('status', 'The User Email Already Exist!');
      }
  }    
public function register(Request $request) {
  $input = $request->all();
  $validator = $this->validator($input);

  if ($validator->passes()){
    $user = $this->create($input)->toArray();
    $user['link'] = str_random(30);

    DB::table('table_user_activations')->insert(['id_user'=>$user['id'],'token'=>$user['link']]);

    Mail::send('emails.activation', $user, function($message) use ($user){
      $message->to($user['email']);
      $message->subject('Office - Activation Code');
    });
    return redirect()->to('login')->with('success',"We sent activation code. Please check your mail.");
  }
  return back()->with('errors',$validator->errors());
}

public function userActivation($token){
  $check = DB::table('table_user_activations')->where('token',$token)->first();
  if(!is_null($check)){
    $user = User::find($check->id_user);
    if ($user->is_activated ==1){
      return redirect()->to('login')->with('success',"user are already actived.");

    }
    $user->update(['is_activated' => 1]);
    DB::table('table_user_activations')->where('token',$token)->delete();
    return redirect()->to('login')->with('success',"user active successfully.");
  }
  return redirect()->to('login')->with('Warning',"your token is invalid");
}
 }

Also, for some reasons the register controller fails to update the is_activated' => 1 on the users table. I have struggled but I can not tell where I am making the mistake.

Would anyone kindly let me know? Thank you.

Please sign in or create an account to participate in this conversation.