Class: Sis::Core::UsersController
- Inherits:
-
ApplicationController
- Object
- ActionController::API
- ApplicationController
- Sis::Core::UsersController
- Defined in:
- app/controllers/sis/core/users_controller.rb
Instance Attribute Summary
Attributes inherited from ApplicationController
Instance Method Summary collapse
- #create ⇒ Object
- #generate_recovery_token ⇒ Object
- #login ⇒ Object
- #reset_password ⇒ Object
- #sign_in ⇒ Object
Instance Method Details
#create ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'app/controllers/sis/core/users_controller.rb', line 18 def create user = User.new(user_params) if user.save render json: { success: true, data: user } else render json: { success: false, errors: user.errors } end end |
#generate_recovery_token ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/sis/core/users_controller.rb', line 38 def generate_recovery_token user = User.where(email: params[:email], secret_question_answer: params[:secret_question_answer]) if user.count.positive? user = user[0] @service.generate_password_token(user) render json: { success: true, token: user.reset_password_token, email: user.email } else render json: { success: false, errors: ['User not found !'] } end end |
#login ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/sis/core/users_controller.rb', line 27 def login user = User.find_by_email(params[:email]) if User&.find_by_email(params[:email]) && user.authenticate(params[:password]) token = JsonWebToken.encode({ id: user.id, first_name: user.first_name, last_name: user.last_name, roles: user.user_roles.map(&:name) }) render json: { success: true, jwt: token } else render json: { success: false, errors: ['Invalid username or password !'] } end end |
#reset_password ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'app/controllers/sis/core/users_controller.rb', line 49 def reset_password token = params[:token].to_s user = User.find_by_reset_password_token(token) if user && @service.password_token_valid?(user.id) @service.reset_password(user.id, params[:password]) render json: { success: true } else render json: { success: false, errors: ['Link not valid or expired. Try generating a new link !'] } end end |
#sign_in ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'app/controllers/sis/core/users_controller.rb', line 6 def sign_in applicant = Applicant&.find_by_email(params[:email]) if Applicant&.find_by_email(params[:email]) && applicant.authenticate(params[:password]) token = JsonWebToken.encode({ id: applicant.id, first_name: applicant.first_name, middle_name: applicant.middle_name, last_name: applicant.last_name, email: applicant.email }) render json: { success: true, jwt: token } else render json: { success: false, errors: ['Invalid username or password !'] } end end |