Customize Laravel 8 Passport methods - laravel

The login validation I use is not Laravel's default. How do I customize Passport methods?
The following code I use to validate with web middleware.
$username = $request->username;
$password = strtoupper(md5($request->password));
$system = env("CODE_SYSTEM", 12);
$sql = "SELECT user.validate( '$system' , '$username', '$password')";
$stmt = DB::select(DB::raw($sql));
$result = $stmt[0]->validation;
if ($result == "ok") {
$user = new User();
$id = DB::table('users')->select('id')
->where('username' , $username)->first();
$user = User::find($id->id);
return $user;
}

To customize you need add the validateForPassportPasswordGrant() at User method, example:
public function validateForPassportPasswordGrant($password)
{
$password = strtoupper(md5($request->password));
$system = env("CODE_SYSTEM", 12);
$sql = "SELECT user.validate( '$system' , '$this->username', '$password')";
$stmt = DB::select(DB::raw($sql));
$result = $stmt[0]->validation;
if ($result == "ok") {
return true;
}
return false;
}
If you need to change the user column name where the passport search username:
public function findForPassport($username)
{
return $this->where('the_username_column', $username)->first();
}

Related

Laravel Date assign errorr

I have small problem in app. Backend is Laravel and Front end is Nuxtjs.
When User registered on app,then users must be wait antill Administartor approved.
When Admin approved this user we give them 3 months subscription.
In my code this part is not working.
$user->activated_at = now();
$user->activated_at = date('Y-m-d H:i');
{
$user = User::find($id);
if (is_null($user)) {
return $this->sendError('admin_messages.user_not_found');
}
$user->status = User::ACTIVE;
$user->activated_at = now();
$user->activated_at = date('Y-m-d H:i');
dd($user->activated_at);
$user->save();
Mail::to($user->email)->queue(new ApproveNotificationMail($user));
return $this->sendResponse('admin_messages.user_activated');
}
$user->activated_at = \Carbon\Carbon::now()->format('Y-m-d H:i');
You can use mutator in your User model, to force activated_at format:
public function setActivatedAtAttribute( $value ) {
$this->attributes['activated_at'] = (new Carbon($value))->format('Y-m-d H:i');
}
You can define a format for date columns using:
protected $dateFormat = 'Y-m-d H:i';
Note that you can directly use:
$user->activated_at = \Carbon\Carbon::now()->format('Y-m-d H:i');

Laravel Eloquent for Social Network

I'm building a social network. I have Users and Posts. for like concept I created a table (Likes):
_______________________
|id | post_id | user_id |
| | | |
I want to show people who like a post below each post.
so I need to save a row for each Like
.
My problem is Eloquent and how to save a Like row
$post = Post::find($request->postId);
$like = new Likes();
$user = $request->user();
$post->likes()->save($like, $user);
return 'done';
this code set user_id (Likes tabel) 0
is there a better way for Like Concept?
in your controller use this method :
public function postLikePost(Request $request)
{
$post_id = $request['postId'];
$is_like = $request['isLike'] === 'true' ? true:false;
$update = false;
$post = Post::find($post_id);
if(!$post){
return null;
}
$user = Auth::user();
$like = $user->likes()->where('post_id',$post_id)->first();
if($like){
$liked = $like->likes;
$update = true;
if($liked == $is_like)
{
$like->delete();
return null;
}
} else
{
$like = new Like();
//die($like);
}
$like->likes = $is_like;
$like->user_id = $user->id;
$like->post_id = $post->id;
if($update){
$like->update();
}
else{
$like->save();
}
return null;
}

Laravel Sentry Update the User

I have a problem with Sentry. The problem is probably that under $user = Sentry::getUserProvider()->findById($id);it is not only finding one user, but many users. Thus it cannot recognize the method user->save.
How can I solve this problem?
I am trying to build a form to edit my user details.
Thank you.
My form looks like this:
{{ Form::open(array('url' => 'profile/useredit')) }}
{{ Form::text('address',null) }}
<br>
{{Form::submit('Submit', array('class' => 'btn btn-default'))}}
{{ Form::close() }}
/**
* Edit the user profile under profile/user
*
* #return View
*/
public function postUseredit(){
try {
$id= Session::get(Config::get('sentry::sentry.session.user'));
// Get the user information
$user = Sentry::getUserProvider()->findById($id);
} catch (UserNotFoundException $e) {
// Prepare the error message
$error = Lang::get('users/message.user_not_found', compact('id'));
// Redirect to the user management page
return Redirect::route('users')->with('error', $error);
}
try {
// Update the user
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
$user->email = Input::get('email');
$user->dob = Input::get('dob');
$user->bio = Input::get('bio');
$user->gender = Input::get('gender');
$user->country = Input::get('country');
$user->state = Input::get('state');
$user->city = Input::get('city');
$user->address = Input::get('address');
$user->postal = Input::get('postal');
$user->activated = Input::get('activate')?1:0;
/*
// is new image uploaded?
if ($file = Input::file('pic'))
{
$fileName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension() ?: 'png';
$folderName = '/uploads/users/';
$destinationPath = public_path() . $folderName;
$safeName = str_random(10).'.'.$extension;
$file->move($destinationPath, $safeName);
//delete old pic if exists
if(File::exists(public_path() . $folderName.$user->pic))
{
File::delete(public_path() . $folderName.$user->pic);
}
//save new file path into db
$user->pic = $safeName;
}
*/
/*
// Get the current user groups
$userGroups = $user->groups()->lists('group_id', 'group_id');
// Get the selected groups
$selectedGroups = Input::get('groups', array());
// Groups comparison between the groups the user currently
// have and the groups the user wish to have.
$groupsToAdd = array_diff($selectedGroups, $userGroups);
$groupsToRemove = array_diff($userGroups, $selectedGroups);
// Assign the user to groups
foreach ($groupsToAdd as $groupId) {
$group = Sentry::getGroupProvider()->findById($groupId);
$user->addGroup($group);
}
// Remove the user from groups
foreach ($groupsToRemove as $groupId) {
$group = Sentry::getGroupProvider()->findById($groupId);
$user->removeGroup($group);
}
*/
// Was the user updated?
if ($user->save()) {
// Prepare the success message
$success = Lang::get('users/message.success.update');
// Redirect to the user page
return Redirect::route('profile/user', $id)->with('success', $success);
}
// Prepare the error message
$error = Lang::get('users/message.error.update');
} catch (LoginRequiredException $e) {
$error = Lang::get('users/message.user_login_required');
}
// Redirect to the user page
return Redirect::route('profile/user', $id)->withInput()->with('error', $error);
}
Instead of
$user = Sentry::getUserProvider()->findById($id);
try this
$user = Sentry::findUserById($id);

Session always returns null facebook php sdk

Hi I'm trying to get data from a user from facebook after he logs in whit facebook. But my session variable is always null .I'm currently using the laravel 4 framework. The code that you are seeing is being called as the callback function from facebook login.
$code = Input::get('code');
if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');
$facebook = new Facebook(Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) return Redirect::to('/')->with('message', 'There was an error');
$me = $facebook->api('/me');
$profile = Profile::whereUid($uid)->first();
if (empty($profile)) {
$user = new User;
$user->name = $me['first_name'].' '.$me['last_name'];
$user->email = $me['email'];
$user->save();
$profile = new Profile();
$profile->uid = $uid;
return $uid;
$profile->username = $me['name'];
$profile = $user->profiles()->save($profile);
}
$profile->access_token = $facebook->getAccessToken();
$profile->save();
$user = $profile->user;
Auth::login($user);
FacebookSession::setDefaultApplication(
Config::get('facebook.appId'),
Config::get('facebook.secret')
);
$helper = new FacebookRedirectLoginHelper('http://projectweb.app:8000/');
$session = $helper->getSessionFromRedirect();
if(isset($session))
{
$request = new FacebookRequest($session,'GET','/me');
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::classname());
$name = $graph->getName();
return $name;
}
else
{
return 'no sesssion';
}
You better try using a Laravel specific Package for that, here it is one, where you also have the details about installing and using it.
https://github.com/SammyK/LaravelFacebookSdk

Laravel Operators AND / && - How to Use Them?

I am trying to use AND as well as && in this code block:
public function show($id)
{
$user = Auth::user()->id;
$userEmail = Auth::user()->email;
$share = Share::where('sbj_id', $id)->first();
if ($share->sbj_id == $id && $share->email == $userEmail ) {
$items = Item::where('project_id', $id)
->orderBy("created_at", "desc")
->groupBy('label_name')
->get();
$sbj = Sbj::find($id);
$allSbj = Sbj::where('user_id', $user)->orderBy("created_at", "desc")->get();
$labels = Label::all();
$pages = Page::where('project_id', $id)->orderBy('created_at', 'desc')->get();
$people = Friend::where('sbj_id', $id)->orderBy("created_at", "desc")->get();
$myPeeps = Contact::where('my_id', $user)->get();
return View::make('sbjs.show', compact('sbj', 'pages', 'labels', 'people', 'myPeeps', 'allSbj', 'items'));
} else {
return Redirect::action ('SbjsController#index');
}
}
However, I am looking online to see if && or AND is supported in Lavavel in this context but no success. My app is taking the user to the ELSE action.
Any ideas?
I just added the Eloquent statement to the IF conditional and it worked fine...not sure why it was not evaluating.

Resources