vendredi 13 juillet 2018

Using two different things two check if user is allowed to login in Laravel

I have two different types of users in my laravel application 'S' and 'D', for type D I need to send a verification mail upon register, and for type 'S' I need to manually approve the user first and then send him the verification mail. So user type 'D' should be approved by default. So I did this in my Auth\RegisterController:

 protected function create(array $data)
{ 
    if($data['u_type'] == 'D'){
        $user = User::create([
            'name' => $data['name'],
            'lname' => $data['lname'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'u_type'=> $data['u_type'],
            'token' => str_random(25),
            'avatar'=> str_random(5),
            'approved'=>true
        ]);
    }else
    {
        $user = User::create([
            'name' => $data['name'],
            'lname' => $data['lname'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'u_type'=> $data['u_type'],
            'token' => str_random(25),
            'avatar'=> str_random(5),
            'approved'=>false
        ]);
    }

The problem occurs when I register, both of the user types have approved set to null,what am I doing wrong. And please also check the following function.This function should check both the conditions if user logs in its in LoginController

    protected function authenticated(Request $request){

    $user = User::where('email','=', $request->email)->first();
    if($user->token!=null){
      if($user->approved == 0){
        Auth::logout();
        return back()->with('info','Account not approved yet, please 
         contact admin');   
      }
      Auth::logout();
      return back()->with('info','Account not verified, please check 
       your email '.$user->email);
    }
    else{
      Auth::login($user);
      return redirect()->back();
    }
  }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire