laravel sociallite in boilerplate - laravel

I have used laravel boilerplate for login.
I have worked using virtual host and put information on basis of virtualhost.
I have 2 tables:
1. users
2. social_logins
Actually i am authenticating from users table. I need to store information in users and then link it with social_logins user_id i.e, store information in table 2.
The main problem here is it doesn't store data in table. This line is not working:
return $this->getAuthorizationFirst($provider);
I have put redirect so it will redirect to given route but doesn't store data in database.
if I catch callback in controller I can catch the information.
$user = Socialite::driver('facebook')->user();
I have done normally using Socialite plugin in normal laravel, but i don't have any idea in Boilerplate.

Actual lifecycle of this loginThirdParty trait is:
1 The first time this is hit, request is empty
2 It's redirected to the provider and then back here, where request is populated
3 So it then continues creating the user
So If the provider is not an acceptable, this trait kick back. Hopefully you need to recheck your .env for FACEBOOK_CLIENT_ID, FACEBOOK_CLIENT_SECRET and CALLBACK_URL.

Related

How to get number of session in laravel project?

I have a basic laravel project which have login, logout and some others basic public pages.
I would like to count all session for the current time(now time). Any session that should be count login user or visit any public pages.
From this project, I would like to know how many session is running?
What I understand from your question is, you want a feature in your application where you can count number of user that are logged in to the application.
If that's the case then as stated above you can hack a way around by adding a bool column in users table or whatever table you are using to store users, and whenever any user logs in, you change that column value to 1 and when the user logs out you change the column value to 0.
This way you can count the users by using that column where the column value is true or 1.
Hope this solves your problem.

Larvel Scout scoped to Logged in user via Auth::user() not working | Issues or technique wrong

Basically I have installed Laravel Scout with TNT Search, all of the configs are correct and they work, I even got a nice search bar to work with an ajax workflow
But the results from the models are from the entire database collection where as my requirement is only to show results of record of records created by the user who is currently logged in session
The models are related via Eloquent Relationships, they work when access normally via the Auth::user() method but the scout search seems to not be attached to any of the models via the Auth::user() example:
App\CourtCase::search($request->q)->get();
The above works but will return any result in the model (and underlying table) without any regards to if the record is owned or related to the user logged in. My intention is however
Auth::user()->courtcase()->search($request->q)->get();
The error i get is the following
message Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::search()
exception BadMethodCallException
file /home/vagrant/code/legistant/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php
Clearly this is because Laravel Scout is not attached or available via the Auth::user() method, I can write code to filter the results after they are returned, but imagine the over head of results before being scope by user, it seems like a waste of all that querying, is there a way to scope to user then use Laravel scout search similar to Auth::user()
Can modify the Auth behavior to attach scout, will it be overwritten using a composer update of Laravel?
So as of 05/05/2019
Laravel Scout does not have a method of scoping queries to Auth user related models, because honestly it is a quite difficult logic to process, especially because the relationships are arbitrary and the keys can be different, a scoping function built internally or externally would have the same computational cost, there is no optimizing here
$results = App\CourtCase::search($request->q)->where('lawyer_id', Auth::id())->get();
This is how I solved it, by attaching a where condition to the search result where it will only show details matching the key, if you are modifying existing code of an application, remember initially scout might show cached results that don't match the filter, which confused me until I refreshed the code
Better code is welcome

How to save a form into different tables

I am new with laravel
I used the default Authentication and I added one other input that I wanted it to be saved on an other table how can I do that
If you're talking about how to save registration form input into different table (not to users) when user is registering, add something like this to create() method in app\Http\Auth\RegisterController.php:
Profile::create(['custom_column' => $data->custom_form_field]);
I think in this situation will be better to use connections in model. If this data is about user of course. Also there is important thing about submit method(simple submit or AJAX).

How to authenticated multiple table in laravel 5.2

I have a two tables in database
1. users(default user table)
2. suspend_users(only two column:- id, email)
now i want to first check in the "suspend_user" table user email exist or not if user email exist in the "suspend table" authentication failed message shows.
now i do not understand that how to check second table(suspend_users) first and how to check multiple table authenticate
I dont understand exactly what you mean, you can check first if the mail exists on the suspend_table and after check login:
example:
if (SuspendedUser::findBy(['email' => $user_email])) {
// go back to what else... throw expetion etc....
}
if (Auth::attempt(email, pass)) {} // check login
you can easily authenticate multiple table in laravel 5.1 and above.
http://blog.sarav.co/multiple-authentication-in-laravel/

Storing remember_token to a separate table than users?

is it possible to set another table & column to store my remember tokens? I know that the framework tries to automatically find a remember_token column in my "users" model, but I want to store it separately from users. Is there a way to configure my default tokens table? Thank you
P.S - I'm using laravel 5
First, you need to create separate model for storing remember tokens and define a relationship on your User model like so
public function rememberToken() {
return $this->hasOne('RememberToken');
}
Then you need to override methods on your User model, originally defined in Authenticatable trait. Override getRememberToken() and setRememberToken() methods. You will also need to override getRememberTokenName() as it is used in where clause in EloquentUserProvider::retrieveByToken() see EloquentUserProvider line 60. In order for this to work properly you probably have to add global scope to your User model to join remember_tokens table on every query, and return 'remember_tokens.token' from getRememberTokenName() method.
Think twice as it seems more trouble than it is worth. Why would you want to store your tokens separately anyway?
I believe the way Laravel works uses a seperate column in the users table to store a single remember_me token. From my understanding it seems that logging out resets this token regardless of storing the token in a cookie (correct me if I'm wrong).
https://github.com/laravel/ideas/issues/971
If you log in with remember_me checked on your personal computer,
then again on your phone,
and maybe again with any other device,
then finally the act of signing out on any device using the remember_me token or not will reset this token in the DB.
If Laravel had a separate table, able to remember each device, it might solve the problem.

Resources