How to implement Pow to login with email or mobile and provide role mapping?

Hi all,
I have implemented Pow for authentication. I am able to successfully register and create session using email and password.

However, I have the following scenario.

  1. I need to implement Pow auth in such a way that users can login/register with email or mobile. With any one of them as mandatory.
    How do I set a schema in such a way that just any one of them is mandatory?
    Also, how do I ensure I will be able to login with either email or mobile? Pow does not allow me to login with mobile number.

  2. I have multiple tables for different roles and each table captures the user profiles belonging to that role. When user logs in based on users table, I need to retrieve the profile info specific to that user from the corresponding table based on user’s role.

Whats the best way to define a mapping between users table and multiple tables that contain the user profile info specific to each role?

Thanks,
Rajasekhar.

How do I set a schema in such a way that just any one of them is mandatory?

You need to edit your migration, to allow for email to be null, and then I would use a check constraint, email is not null or phone_number is not null.

Pow does not allow me to login with mobile number.

You can implement your own authentication function in a custom context to change that, I would take a look here pow/lib/pow/ecto/context.ex at main · pow-auth/pow · GitHub

A combination of a custom authentication and get_by function would probably be enough for your case.

2 Likes

Thanks, Schultzer. I will check this out.