Class: DeviseTokenAuth::SessionsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/devise_token_auth/sessions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 6

def create
  @user = resource_class.find_by_email(resource_params[:email])

  if @user and valid_params? and @user.valid_password?(resource_params[:password]) and @user.confirmed?
    # create client id
    @client_id = SecureRandom.urlsafe_base64(nil, false)
    @token     = SecureRandom.urlsafe_base64(nil, false)

    @user.tokens[@client_id] = {
      token: BCrypt::Password.create(@token),
      expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
    }
    @user.save

    render json: {
      data: @user.as_json(except: [
        :tokens, :confirm_success_url, :reset_password_redirect_url, :created_at, :updated_at
      ])
    }

  elsif @user and not @user.confirmed?
    render json: {
      success: false,
      errors: [
        "A confirmation email was sent to your account at #{@user.email}. "+
        "You must follow the instructions in the email before your account "+
        "can be activated"
      ]
    }, status: 401

  else
    render json: {
      errors: ["Invalid login credentials. Please try again."]
    }, status: 401
  end
end

#destroyObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 43

def destroy
  # remove auth instance variables so that after_filter does not run
  user = remove_instance_variable(:@user) if @user
  client_id = remove_instance_variable(:@client_id) if @client_id
  remove_instance_variable(:@token) if @token

  if user and client_id and user.tokens[client_id]
    user.tokens.delete(client_id)
    user.save!

    render json: {
      success:true
    }, status: 200

  else
    render json: {
      errors: ["User was not found or was not logged in."]
    }, status: 404
  end
end

#resource_paramsObject



68
69
70
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 68

def resource_params
  params.permit(devise_parameter_sanitizer.for(:sign_in))
end

#valid_params?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/controllers/devise_token_auth/sessions_controller.rb', line 64

def valid_params?
  resource_params[:password] && resource_params[:email]
end