Class: RailsJwtApi::UsersController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- RailsJwtApi::UsersController
- Includes:
- Controllers::Authentication
- Defined in:
- app/controllers/rails_jwt_api/users_controller.rb
Instance Method Summary collapse
-
#create ⇒ Object
gem ‘rails_jwt_api’, path:‘/Users/ispirett/RubymineProjects/engines/rails_jwt’.
- #sign_in ⇒ Object
Methods included from Controllers::Authentication
Instance Method Details
#create ⇒ Object
gem ‘rails_jwt_api’, path:‘/Users/ispirett/RubymineProjects/engines/rails_jwt’
10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/controllers/rails_jwt_api/users_controller.rb', line 10 def create @user = User.new(user_params) if @user.save token = encode(user_id: @user.id) # refactor to allow users to configure time = Time.now + RailsJwtApi.token_expiration.to_i render json: {status: 'success', token: token,user: @user.details, exp: time.strftime('%m %d %y %H:%M')}, status: :ok else render json: {status: :failed, msg: @user.errors.}, status: :unauthorized end end |
#sign_in ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/controllers/rails_jwt_api/users_controller.rb', line 22 def sign_in @user = User.find_by(email: params[:email].downcase) if @user&.authenticate(params[:password]) #=> create new token from JwtAuth token = encode(user_id: @user.id) time = Time.now + 1.week.from_now.to_i #=> create or update user to jwt model if @logged_user = Jwt.find_by(user_id: @user.id) @logged_user.update_attribute(:token, token) else Jwt.create(user_id: @user.id, token: token) end render json: {status: :success,user: @user.details, token:token, exp: time.strftime('%m-%d-%y %H:%M')},status: :ok else render json: {status: :failed, msg: 'email or password is incorrect'}, status: :unauthorized end end |