Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Username / nickname not saved in database #14

Closed
syphernl opened this issue Jul 18, 2014 · 9 comments
Closed

Username / nickname not saved in database #14

syphernl opened this issue Jul 18, 2014 · 9 comments

Comments

@syphernl
Copy link
Contributor

After a successful login an entry is inserted into oauth_identities and users. However, the username field remains empty. I have tried renaming "username" to "nickname" in the table definition but this didn't have any result.

This is being returned by the userdata service and the field is correctly configured in the provider

@adamwathan
Copy link
Owner

The package won't save any data to your user by default, it assumes your username/email field is being used for accounts that are signed up for manually, not using OAuth. Since the username field is used as login credentials for regular accounts, it would be a little confusing for OAuth-created accounts to have usernames, at least by default.

You can easily do this though by passing a closure to your OAuth::login call like so:

OAuth::login('facebook', function($user, $details) {
    $user->username = $details->nickname;
    $user->save();
});

@syphernl
Copy link
Contributor Author

Ah, I must've created the User table on auto-pilot then since it contained a username field which was not being filled in :-) Thanks!

@syphernl
Copy link
Contributor Author

It is odd though.. responses that are being returned via the userdata call cannot be used in Auth::user()->stuff. For instance, my oAuth server returns a username but Auth::user()->username does not return anything (probably because it clashes with the table column)

@adamwathan
Copy link
Owner

Auth::user()->username won't work unless you actually save that manually. The stuff in the $details variable here isn't actually saved by default, it's just made available for you in case you want to save any of it...

OAuth::login('facebook', function($user, $details) {
    $user->username = $details->nickname;
    $user->save();
});

So if you want to have access to the nickname after the account is created, you have to save it manually somewhere else, like $user->username or you could create a profiles table or similar where you also save the first name, last name, email address, etc. that comes in the $details object.

If you just make this call:

OAuth::login('facebook');

...none of the extra details will be saved anywhere. The extra callback is just in case you do want to save that data (most of the time you would want to save something) but it's impossible for me to guess what you want to save or where you want to save it, so it's just made available in that callback so you can persist that extra meta data yourself wherever and in whatever format you want.

By default it only saves their unique ID from the external service and the access token, and even the access token actually doesn't really need to be saved since it's not used later unless you are making extra API calls yourself.

@syphernl
Copy link
Contributor Author

Hmm. the $user is always NULL. Details are filled in, creating a new user based on that (and saving) works (the user shows up in the DB) but then there is a FatalErrorException caused by UserStore.php:

Call to a member function save() on a non-object

@syphernl
Copy link
Contributor Author

This may be a documentation error. If the closure returns the $user it works fine, without errors. However, the user is only being logged in the first time. Logging out and in again (with existing or new data) doesn't work.

Edit: Hmm something is calling the logout route after being logged in. Could be the Redirect::intended.. investigating..

@adamwathan
Copy link
Owner

You shouldn't have to return from the closure, this is where it's being called and the return value is not used: https://github.com/adamwathan/eloquent-oauth/blob/master/src/AdamWathan/EloquentOAuth/OAuthManager.php#L45

Is the $user getting passed in to the closure NULL? If there's a way you can create a simple reproducible situation where the user ends up NULL I can investigate. I know someone else had an issue once where their id column on the user table was not an integer and that created problems.

Can you show me what your login call looks like?

@syphernl
Copy link
Contributor Author

The problem turned out to be Redirect::intended()

Login => Redirect to / => Logout => Redirect to / => Login => Redirect to /logout => Redirect to /

Seems to work fine. I only have to figure out how I can temporarily store the user data in the User rather than having to save it in the DB. But that should be probably be safe enough as it will replace the existing data with new if it has changed.

@adamwathan
Copy link
Owner

Cool glad it's working as expected!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants