简体   繁体   中英

Using Ruby on rails can you have multiple user types using sessions and authentication (gem bcrypt)?

Sorry if the question is worded strangely, but this is the scenario I am dealing with here:

I am attempting to make a basic web app using rails for an assigned project and chose to create an online version of a pharmacy basically. There will be more complication later, but for now I believe I just need to make a Patient model, controller, routes, and views, and want to have login authentication using a gem called bcrypt, as well as a Doctor that does all the same stuff but they would discretely access their own view pages and only be able to after authentication. I am using a session controller and have session views as well. It's getting complicated for me at this point since I am pretty new to programming and I have not been able to find an answer to the question of whether one would make it all happen through just "Users" (how all examples and tutorials do it), or through separate user types as I am currently attempting to set it up (ie 'Patients' and 'Doctors'). I really appreciate any help. I will do my best to clarify more if needed.

Using 'Devise' gem (and its extensions) you can do all these things including multiple user types. By the way, devise is actually using bcrypt in its own implementation.

https://github.com/heartcombo/devise

I do appreciate the response to the question, however I was specifically attempting to not use Devise for this project. We ended up abstracting the sessions such that either type of user could have a session, authentication, and authorization in order to make it secure based on which route was selected basically. That way, we did not need to make a separate session view and controller for the separate user types. The most important thing was this bit of code in the SessionsController:

def create
if params[:patient]
  @patient = Patient.find_by(email: params[:email])
  if @patient && @patient.authenticate(params[:password])
    session[:patient_id] = @patient.id
    redirect_to @patient
  else
    render 'patients/login'
  end
elsif params[:doctor]
  @doctor = Doctor.find_by(email: params[:email])
  if @doctor && @doctor.authenticate(params[:password])
    session[:doctor_id] = @doctor.id
    redirect_to @doctor
  else
    render 'doctors/login'
  end
end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM