Class: RailsJwtApi::UsersController

Inherits:
ApplicationController show all
Includes:
Controllers::Authentication
Defined in:
app/controllers/rails_jwt_api/users_controller.rb

Instance Method Summary collapse

Methods included from Controllers::Authentication

#decode, #encode

Instance Method Details

#createObject

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.full_messages}, status: :unauthorized
  end
end

#sign_inObject



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 
  @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