Skip to content
Advertisement

Loopback passport-facebook for multiple user models

I’m using passport to authenticate users for my application using Facebook. In my application, I have 2 different users: customers and professionals (transitioning to “experts”).

I followed the Loopback docs to implement the loopback-component-passport package and passport-facebook and have it working for my customers.

Because I use custom User models, I followed this SO Post on how to get that fixed (note: I still use the build-in AccessToken model).

Angular Frontend

I use an Angular frontend. To redirect the user back to my frontend, I created a bootscript that looks like this:

JavaScript

When I authenticate as a customer, I should be redirected to https://example.com/auth/customer/success When I authenticate as a professional/expert, I should be redirected to https://example.com/auth/expert/success.

Setup a second passport-facebook

I have to load the configuration for both the customer and professional/expert which I’m doing in server.js. I created 2 providers.json files (providers.customer.json and provider.professional.json) containing their specific info.

This is how I load both configurations (perhaps a little messy):

JavaScript

The actual problem

I have 2 different apps in Facebook (1 for customers, 1 for professionals/experts). When I authenticate using localhost:3000/auth/customer/facebook or localhost:3000/auth/expert/facebook I see that both apps are used for the correct endpoint. But no matter what endpoint I use, after authentication I’m always redirected to http://example.com/expert/auth/facebook

So my question is: How can I fix this issue so that customers are redirected to the customer endpoint and experts/professionals are redirected to their expert endpoint?

Additional information

  • Registration works fine, I can find customers in customer table and expert in expert table in my database

For reference: providers.customer.json

JavaScript

For reference: providers.professional.json

JavaScript

Advertisement

Answer

So I was able to fix this by using the PassPortConfigurator#configureProvider method. Instead of using the JSON files to setup the facebook authentication, I did it in JS.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement