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

Multiples Profiles Relationship #1168

Closed
betosalvador opened this issue May 10, 2018 · 9 comments
Closed

Multiples Profiles Relationship #1168

betosalvador opened this issue May 10, 2018 · 9 comments

Comments

@betosalvador
Copy link

Hello everyone

I'm working on a system where the user can be a Employer, Employee or a Operator.

Each theese roles must have a model class, where I will implement specific behaviors.

All theese classes must have a login user.

So, I think that this solution could work:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :validatable
  include DeviseTokenAuth::Concerns::User

  belongs_to :profile, polymorphic: true
end


class Employer < ApplicationRecord
  has_one :user, as: :profile, dependent: :destroy
end


class Employee < ApplicationRecord
  has_one :user, as: :profile, dependent: :destroy
end


class Operator < ApplicationRecord
  has_one :user, as: :profile, dependent: :destroy
end

I want to create specifics registrations controllers, for each profile (EmployerController, EmployeeController, OperatorController), because each registration may have different behaviors.

And I want to use the default SigiIn controller for all profiles.

How to configure the routes for this?

Thanks in advance

@MaicolBen
Copy link
Collaborator

@betosalvador Check https://github.com/lynndylanhurley/devise_token_auth/blob/master/docs/usage/routes.md. You have to add the next code for each model:

 mount_devise_token_auth_for 'Employee', at: '/api/v1/employees', controllers: {
    registrations:  'api/v1/employees/registrations',
    sessions:  'api/v1/sessions',
}

As you can see in the example, it's for Employee, I suggest to use a folder for each resource, and the sessions controller isn't in any specific resource folder.

@betosalvador
Copy link
Author

betosalvador commented May 13, 2018

Hello @MaicolBen, thanks for response.

My model Employee don't is a Devise model.

I have just one Devise model: "User".

Do you think that your suggestion solve my case?

hugs

@betosalvador
Copy link
Author

@MaicolBen , just more one question:

I have namespaces configured in my routes, like:

  namespace :api, path: '/' do
    namespace :v1, path: '/', constraints: ApiVersionConstraint.new(version: 1, default: true) do
    end
  end

Do you think better call mount_devise_token_auth_for method inside or outside namespaces?

thanks in advance

@betosalvador
Copy link
Author

I made the suggested test, but the only route generated was

/api/v1/employees/validate_token

could you please open my issue again?

thanks again

@betosalvador betosalvador changed the title Multiples Profiles Relationship Multiples Profiles Relationship (Reopen, please) May 20, 2018
@betosalvador betosalvador changed the title Multiples Profiles Relationship (Reopen, please) Multiples Profiles Relationship May 20, 2018
@betosalvador
Copy link
Author

@MaicolBen

please, reopen my issue
thanks

@MaicolBen
Copy link
Collaborator

Reopening, but it seems more a question than an issue. I need time to setup your problem locally and try a different solution.

@MaicolBen MaicolBen reopened this May 21, 2018
@betosalvador
Copy link
Author

betosalvador commented May 21, 2018

Hello @MaicolBen

thank you very much.

Actually, I created differents controllers for registration of each user type (EmployersControllers, EmployeesControllers, OperatorsControllers).

All this user types has a User record, with the login data, like I said before.

So, I'm using the default route to SignIn all user types (POST "/auth/sign-in").

I need now to return the user role (Employer, Employee or Operator) on SignIn to the fronEnd.

So, that way, I will make some authorizations.

I would like to know if is this a good approach?

Thanks in advance!

@MaicolBen
Copy link
Collaborator

Yes, that's what I said, a controller for each model like Employees::RegistrationsControler, Operators::RegistrationsControler and so on

@betosalvador
Copy link
Author

Hello @MaicolBen
sorry for delay in my response

thanks for your reponse

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