Auth::user()-> non-object - authentication

I'm trying to retrieve users First Name from the database so I tried something like this in my provider.php controller but the browser error says "Trying to get property of non-object"
$provider = Auth::user()->email;
return View::make('providers.account')
->with('title', 'Account')
->with('provider', $provider);
Any help?

If you are trying to get information for the logged in user, use Ben Dubuisson's answer.
If you are try to get the information for a specified user, do something like:
$provider = User::find($id)->email;

You need to check if user is logged in:
if (Auth::check())
{
$provider = Auth::user()->email;
return View::make('providers.account')
->with('title', 'Artsgap Account')
->with('provider', $provider);
}
otherwise it will return that error (the object doesn;t exist since there is no logged in user)

Related

How to handle a specific error from Microsoft.AspNetCore.Identity, returned while creating user with password?

How to handle specific error (Microsoft.AspNetCore.Identity.IdentityError) returned during creating user by using (UserManager<TUser>)class. For example, I want to do something if the error is: PasswordTooShort or PasswordRequiresNonAlphanumeric. Now I get information about errors but idk how to handle them.
I don't want to do something like if(IdentityError == "name of error" {do something}), I'm looking for smarter solution... I looked through a lot of topics but cannot find an answer to my question, if you know one, please redirect me...
EDIT: I made something like this:
if (createResult.Errors.Any(x => x.Code == "PasswordTooShort"))
{
var errorContent = "kielbaska";
return RedirectToAction("ShowError", new ErrorFM
{
ErrorContent = errorContent
});
}
But I want to replace error name from brackets with error name from list of available result failed errors, for example something like thisusrManager.ErrorDescriber.PasswordTooShort(just and example)

TypeError: Cannot read property 'users' of undefined error

I have written the code:
function getId(username) {
var infoUrl = "https://www.instagram.com/web/search/topsearch/?context=user&count=0&query=" + username
return parseInt(fetch(infoUrl)['users']);
}
function fetch(url) {
var ignoreError = {
"muteHttpExceptions": true
};
var source = UrlFetchApp.fetch(url, ignoreError).getContentText();
var data = console.log(source);
return data;
}
To get the userID of the username input.
The error corresponds to the line:
return parseInt(fetch(infoUrl)['users']);
I have tried differnt things but I cant get it to work. The url leads to a page looking like this:
{"users": [{"position": 0, "user": {"pk": "44173477683", "username": "mykindofrock", "full_n........
Where the numbers 44173477683 after the "pk": are what I am trying to get as an output.
I hope someone can help as I am very out of my depth, but I guess this is how we learn! :)
I was surprised that the endpoint you provided actually led to a JSON file. I would have thought that to access the Instagram API, you would need register a developer account with Facebook etc. Nevertheless, it does return a JSON by visiting in the browser. I suppose that it just shows the publicly available information on each user.
However, with Apps Script it seems like a different story. I visited:
https://www.instagram.com/web/search/topsearch/?context=user&count=0&query=user
In a browser and chose a random user id. Then I called it from Apps Script with UrlFetchApp:
function test(){
var username = "username7890543216"
var infoUrl = "https://www.instagram.com/web/search/topsearch/?context=user&count=0&query=" + username
var options = {
'muteHttpExceptions': true
}
var result = UrlFetchApp.fetch(infoUrl, options)
console.log(result.getResponseCode())
}
Which returns a 429 response. Which is a "Too Many Requests" response. So if I had to guess, I would say that all requests to this unauthenticated endpoint from Apps Script have been blocked. This is why when replacing the console.log(result.getResponseCode()) with console.log(result.getContentText()), you get a load of HTML (not JSON) part of it which says:
<title>
Page Not Found • Instagram
</title>
Though maybe its IP based. Try and run this code from your end, unless you get a response code of 200, it is likely that you simply can't access this information from Apps Script.
You are setting data to the return value of console.log(source) which is undefined. So no matter what the data is, you will get undefined.
Another thing to avoid is that fetch will not necessarily be hoisted because fetch is a built in function to make API calls.

request.setAttribute is not working with chain.doFilter

I have two servlet ReplayFilter and VideoReplayServlet. From ReplayFilter, I am calling VideoReplayServlet using chain.doFilter. I am able to call VideoReplayServlet from ReplayFilter but I am not able to get userId variable from request object in VideoReplayServlet, which I have already set in request object before calling chain.doFilter. You can find my code below -
In ReplayFilter -
request.setAttribute("userId", userId);
request.setAttribute("uname", "mari");
chain.doFilter(request, response);
In VideoReplayServlet -
String uname = request.getParameter("uname");
String user_Id = request.getParameter("userId");
In VideoReplayServlet replay, I am getting both uname and user_Id null.
Can anybody help me?
I think the issue here is that you are setting it as an attribute and expecting it as a parameter which is contradicting.
Try the below code instead
request.getAttribute("userId", userId); //Note the getAttribute() instead of getParameter()

Eloquent Auth Login not working

I am trying to log in providing an email and password. I have tried hashing and not hashing the password myself. I run dd(Auth::attempt(Input::except('submit'))); and it returns false. Yes the array is correct. Yes that is what's in the database. I followed the attempt() to Illuminate/Auth/Guard.php attempt() The code for that function is below.
public function attempt(array $credentials = array(), $remember = false, $login = true)
{
$this->fireAttemptEvent($credentials, $remember, $login);
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
// If an implementation of UserInterface was returned, we'll ask the provider
// to validate the user against the given credentials, and if they are in
// fact valid we'll log the users into the application and return true.
if ($this->hasValidCredentials($user, $credentials))
{
if ($login) $this->login($user, $remember);
return true;
}
return false;
}
This function has not be modified at all. dd($user); returns the correct information, an instance of User with the attributes pulled from the db. and dd($credentials); returns an array of the post information, an email and password.
dd($this->hasValidCredentials($user, $credentials)); returns boolean false.
I have no idea why. Let me know if more info is required. Thank you!
My password table had a character limit of 20. I'm an idiot. It works now.

google account login for the first time displays error

I am trying to login the user with google account in my application. I am having a problem when the user is logged in for the first time with google account in my app it shows this error:
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, null given
In my controller I have this:
public function loginWithGoogle() {
// get data from input
$code = Input::get('code');
// get google service
$googleService = Artdarek\OAuth\Facade\OAuth::consumer("Google");
if (!empty($code)) {
// This was a callback request from google, get the token
$token = $googleService->requestAccessToken($code);
// Send a request with it
$result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
$user = User::whereEmail($result['email'])->first(['id']);
if (empty($user)) {
$data = new User;
$data->Username = $result['name'];
$data->email = $result['email'];
$data->google_id = $result['id'];
$data->first_name = $result['given_name'];
$data->last_name = $result['family_name'];
$data->save();
}
Auth::login($user);
return Redirect::to('/');
}
// if not ask for permission first
else {
// get googleService authorization
$url = $googleService->getAuthorizationUri();
// return to facebook login url
return Redirect::to((string) $url);
}
}
I know the problem is with Auth::login($user); as insert is performed at the same time with Auth::login($user); and it doesn't find data from database for the first time, but I don't know how to avoid this error and instead redirects to the main page even when the user is logged in for the first time. After this error the user is logged in, but how to avoid this?
Without knowing whether the rest of the code works, you definitely have a problem here:
if (empty($user)) {
$data = new User;
(...)
$data->save();
}
Auth::login($user);
When you're done creating your user, the $user variable is still empty. Your user is actually called $data. You should either rename the variable, or do the login with $data instead. Hopefully, that's enough to make the code work. :)