msgphp / symfony-demo-app

A Symfony demo application with basic user management
https://github.com/msgphp/msgphp
MIT License
0 stars 0 forks source link

Multiple email login #60

Closed crabnky closed 6 years ago

crabnky commented 6 years ago

Hi again :)

Is it possible that user has few emails and can log in using any of his emails? One account - few emails, any can be used for login.

ro0NL commented 6 years ago

Hi @crabnky,

Sure, that's exactly what the demo does. You can login using your primary username (email), or any of the user's secondary emails.

See https://github.com/msgphp/symfony-demo-app/blob/master/src/Entity/User/UserEmail.php + https://github.com/msgphp/symfony-demo-app/blob/master/config/packages/msgphp.php#L24-L26 and you should be good to go :)

See https://github.com/msgphp/symfony-demo-app/blob/master/src/Entity/User/User.php#L33 which provides User::getEmails().

crabnky commented 6 years ago

Awesome! :)

Trying to implement it today. :)

Thanks much!

crabnky commented 6 years ago

Hmm, what if I don't need usernames for users? On login page I can see the query joining the users with usernames table... I don't need this table, I want to log in using email... User do not have usernames at all in system I'm working on...

Edit: I added username_lookup to config: 'username_lookup' => [ ['target' => UserEmail::class, 'field' => 'email', 'mapped_by' => 'user'], ]

ro0NL commented 6 years ago

I don't need this table

You do, it enforces a unique constraint on all usernames in system.

A username can be e-mail, nickname, etc. So if you only aggregate email values, then all your usernames will be emails. This is up to you eventually.

User do not have usernames

But they do have emails, which will represent a username.

crabnky commented 6 years ago

OK, I'm looking at this: https://user-images.githubusercontent.com/1047696/45264216-62e35000-b439-11e8-9c04-f835f46a857b.png I can see username table, user_email table and credential_email field in user table... I thought that user_email is a table holding user emails used to log in...

ro0NL commented 6 years ago

user_email is just a store of (secondary) user email adresses - no context (secondary if you use credential.email for a user's primary email).

By configuring username_lookup we aggregate (duplicate if you like) username values from arbitrary sources in a single table. This happens automatically in UsernameListener.

Then you use UniqueUsername as a constraint on user_email.email to validate against the username table.

Basically the username table is disposable and can be synchronized using user:synchronize-usernames.

crabnky commented 6 years ago

I see, this makes sense. Thanks for patience and explanation. :) The problem is that I'm working on existing database, and I'm little limited. :[ I have one table with users and second holding user emails, telephone numbers and faxes. Of course one user can have many entries in the second table. I have to allow user to log in with any of his emails.

crabnky commented 6 years ago

Hi again...

I want to configure Username class (for example to change the table name), but I'm not allowed...

ro0NL commented 6 years ago

hi @crabnky ,

The Username entity is managed by MsgPHP, you don't need to (and you cant) customize it.

E.g. it's not an abstract / mapped super class for you to extend. See https://msgphp.github.io/docs/reference/entities/#entities_2

I have one table with users and second holding user emails, telephone numbers and faxes.

The username_lookup config currently only works with Doctrine entities. And those entities should map the User it belongs to.

So it would be something like:

msgphp_user:
  username_lookup:
    - 
      target: AnyDoctrineEntity::class
      field: 'any_fieldname_returning_the_username_value'
      mapped_by: 'any_fieldname_returning_the_user'
use Msgphp\User\Entity\User;

class AnyDoctrineEntity {
  public function getAnyFieldnameReturningTheUsernameValue(): string {}
  public function getAnyFieldnameReturningTheUser(): User {}
}
crabnky commented 6 years ago

Yeah, but I have some restrictions while working with the database. This is a pain. For example every table has the 'table_' prefix. Don't ask why. :) Same with id fields. That's why I wanted to customize Username entity.

ro0NL commented 6 years ago

That's why I wanted to customize Username entity.

Because you want it to have a custom table name?

crabnky commented 6 years ago

Yes, at least. Not sure about fields, as I don't have user table - I have contact table, so the user_id is a contact_id for me, but this is smaller issue. Sorry for bothering you with this, maybe I should write my own user provider and login form.

ro0NL commented 6 years ago

See https://github.com/msgphp/msgphp/issues/196 :)

crabnky commented 6 years ago

Huh, thanks! :)