how to logout from laravel 5.6 - laravel

I want to log out after register. how do I do that?
This is my register controller:
protected function create(array $data)
{
$user = User::create([
'firstname' => $data['firstname'],
'secondname' => $data['secondname'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'address' => $data['address'],
'mobileno' => $data['mobileno'],
'type' => $data['type'],
]);
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
Mail::to($user->email)->send(new VerifyMail($user));
return $user;
return redirect('/login')->with('status', 'We sent you an activation code. Check your email and click on the link to verify.');
}

You can change the url :-
protected $redirectTo = '/where/you/want/to/redirect';
after registration in app/Http/Controller/Auth/RegisterController.php
and for logout:-
public function __construct()
{
$this->middleware('guest')->except('logout');
}

Add this two line in your controller and check
public function __construct()
{
$this->middleware('auth');
}

Related

Auto login after register in laravel 5.7

I want when I finished register it redirects me to the dashboard page (Auto login after the register).
this is the user register code :
public function store(Request $request)
{
$this->validator($request->all())->validate();
$apprenant = Apprenant::create([
'nom' => $request['nom'],
'prenom' => $request['prenom'],
'email' => $request['email'],
'niveau' => $request['niveau'],
'password' => Hash::make($request['password']),
]);
return redirect('/apprenant/dashboard');
}
But when I finished registring it redirects me to the login page
Hi if you are create a guard then run this code for after register login
Auth::guard(guard_name)->loginUsingId($id);
// example
public function register(Request $req)
{
$user = new User; // define here your model
$user->name = $req->name;
$user->email = $req->email;
$user->password = Hash::make($req->password);
if($user->save()){
Auth::guard(guard_name)->loginUsingId($user->id);
}
}
// logout overite
public function logout()
{
Auth::guard('guard_name')->logout();
return redirect("login_path");
}
When creating a new user, create() method, should return a new model object.
Use Auth::loginUsingId($apprenant->id); before redirecting to dashboard:
public function store(Request $request)
{
$this->validator($request->all())->validate();
$apprenant = Apprenant::create([
'nom' => $request['nom'],
'prenom' => $request['prenom'],
'email' => $request['email'],
'niveau' => $request['niveau'],
'password' => Hash::make($request['password']),
]);
Auth::loginUsingId($apprenant->id);
return redirect('/apprenant/dashboard');
}
source
https://laravel.com/docs/5.7/authentication#other-authentication-methods
Write this line before redirecting to the dashboard:
\Auth::login($apprenant);
or just,
auth()->login($apprenant);
That means, you code will look like:
public function store(Request $request)
{
$this->validator($request->all())->validate();
$apprenant = Apprenant::create([
'nom' => $request['nom'],
'prenom' => $request['prenom'],
'email' => $request['email'],
'niveau' => $request['niveau'],
'password' => Hash::make($request['password']),
]);
//login the user
\Auth::login($apprenant);
return redirect('/apprenant/dashboard');
}

Send confirmation mail with user registration

I try to register user with send connfirmation mail at the samme time.but the page is reloaded there is no data stored in db and also not sending the confirmation email.
RegisterController.php
protected function create(array $data)
{
return User::create([
'title' => $data['title'],
'fname' => $data['fname'],
'lname' => $data['lname'],
'company' => $data['company'],
'email' => $data['email'],
'phone' => $data['phone'],
'people' => $data['people'],
'sdate' => $data['sdate'],
'notes' => $data['note'],
'password' => Hash::make($data['password']),
]);
}
public function register(Request $request)
{
//dd($request->all());
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
Mail::to($user->email)->send(new ConfirmationEmail($user));
return back()->with('status','Please confirm your email address...');
// $this->guard()->login($user);
// return $this->registered($request, $user)
// ?: redirect($this->redirectPath());
}
//confirmation email code
public function confirmEmail($token)
{
User::whereToken($token)->firstOrFail()->confirmEmail();
return redirect('/user/login')->with('status', 'You are now confirmed. Please login.');
}

Laravel 5.5 Api Registration issue

I have been trying to get this api up and running and keep on experiencing this error when I test in Postman.
1. api.php
Route::group(['middleware' => ['api','cors']], function () {
Route::post('auth/register', 'Auth\RegisterController#create');
});
2. RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}}
Postman configuration set to Post, the body is set to raw and JSON(application/json) Below is the postman json code.
{
"name": "Walter White",
"email": "wwhite#someemaildomain.net",
"password": "testpassword"
}
Below is the error
Too few arguments to function App\Http\Controllers\Auth\RegisterController::create(), 0 passed and exactly 1 expected in file C:\xampp\examplestuff
In order to fix your registration you should change your route definition to:
Route::group(['middleware' => ['api','cors']], function () {
Route::post('auth/register', 'Auth\RegisterController#register');
});
I assume your RegisterController is using the trait RegistersUsers. This trait is providing the register method, which is using the RegisterController::create method to create the new user itself.
Pass Request to your create function like:
protected function create(Request $request){...
and access your data like this:
$request->name
you have to do validation also plz check below answer
protected function create(Request $request)
{
$data = $request->json()->all();
$validator = Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
if ($validator->fails()) {
foreach ($validator->messages()->getMessages() as $field_name => $message){
$messages[] = $message[0];
}
$messages = $messages;
$message = implode(',',$messages);
$response = $messages;
return $response;
}else{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}

laravel auth register insert data into two tables

i have my default registration for auth controller. i want to register also the emp_id created from users table to employee table. once registered.
my RegisterController
use App\User;
use App\Employee
public function count_users(){
$count = User::count();
return date('y').'-'.sprintf('%04d',$count);
}
protected function create(array $data)
{
return User::create([
'emp_id' => $this->count_users(),
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return Employee::create([
'emp_id' => $this->count_users()
]);
}
Please check following line in your code:
return User::create([ .....
Above line creates the user and returns the created user. Any code below "return" is not being called.
Please try following code:
use App\User;
use App\Employee
public function count_users(){
$count = User::count();
return date('y').'-'.sprintf('%04d',$count);
}
protected function create(array $data)
{
$emp_id = $this->count_users();
$user = User::create([
'emp_id' => $emp_id,
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Employee::create([
'emp_id' => $emp_id
]);
return $user;
}

Laravel 5.2 add one more condition to login in standard Auth controller

I'm trying to add one condition to laravel standard auth. I have found a lot of forums with this question. Most of them make change in vendor folder, what I don't want to do. I also found a way of adding credentials(Request $request) function in AuthController.php, but I have no luck. It looks like this:
protected function credentials(Request $request)
{
return array_merge($request->only($this->username(), 'password'),
['Type' => 1]);
}
Does anyone solve this issue or can advice me what to do? Thanks
Below is the function your can use in AuthController to override the login function:
public function login(Request $request)
{
$validator = Validator::make(
$request->all(),
array(
'user_name' => 'required',
'password' => 'required',
),
array(
)
);
if($validator->fails()){
return redirect()->back()->withInput()->withErrors($validator->errors(),'invalid_credentials');
}
if (!\Auth::validate(['user_name' => $request->user_name, 'password' => $request->password])) {
return redirect()->back()->withInput($request->only('user_name'))->withErrors([
'user_name' => 'Incorrect Username or Password',
],'invalid_credentials');
}
$credentials = array('user_name' => $request->user_name, 'password' => $request->password);
if (\Auth::attempt($credentials, true)){
/* Check your user type condition */
if(\Auth::User()->type == '1'){
return redirect()->intended($this->redirectPath());
}
else{
\Auth::logout();
return redirect()->back()->withInput($request->only('user_name'))->withErrors([
'user_name' => 'Your type validation message',
],'invalid_credentials');
}
}
return redirect()->back()->withInput($request->only('user_name'))->withErrors([
'user_name' => 'Incorrect email address or password',
],'invalid_credentials');
}
Hope this helps you.. :)

Resources