Class: MiniAuth::SessionGuard

Inherits:
Guard
  • Object
show all
Defined in:
lib/mini_auth/session_guard.rb

Constant Summary collapse

REMEMBER_DURATION =
86400 * 30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Guard

#guest?, #logged_in?

Constructor Details

#initialize(name, model, request, login_url, remember_time = REMEMBER_DURATION) ⇒ SessionGuard

Returns a new instance of SessionGuard.

Parameters:

  • name (String)
  • model (Class)
  • request (ActionDispatch::Request)
  • login_url (String)
  • remember_time (Integer) (defaults to: REMEMBER_DURATION)


19
20
21
22
23
24
25
# File 'lib/mini_auth/session_guard.rb', line 19

def initialize(name, model, request, , remember_time = REMEMBER_DURATION)
  @name = name
  @model = model
  @request = request
  @login_url = 
  @remember_time = remember_time
end

Instance Attribute Details

#login_urlObject (readonly)

Returns the value of attribute login_url.



8
9
10
# File 'lib/mini_auth/session_guard.rb', line 8

def 
  @login_url
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/mini_auth/session_guard.rb', line 8

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/mini_auth/session_guard.rb', line 8

def name
  @name
end

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/mini_auth/session_guard.rb', line 8

def request
  @request
end

#sessionObject (readonly)

Returns the value of attribute session.



8
9
10
# File 'lib/mini_auth/session_guard.rb', line 8

def session
  @session
end

Class Method Details

.hash_password(plaintext) ⇒ Object



10
11
12
# File 'lib/mini_auth/session_guard.rb', line 10

def self.hash_password(plaintext)
  BCrypt::Password.create(plaintext, cost: 12)
end

Instance Method Details

#attempt!(credentials, remember = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mini_auth/session_guard.rb', line 27

def attempt!(credentials, remember = false)
  user = @model.where(credentials.except(:password)).first
  return false if user.blank?

  unless BCrypt::Password.new(user.password).is_password?(credentials[:password])
    return false
  end

  login!(user, remember)
  true
end

#auth_urlObject



72
73
74
# File 'lib/mini_auth/session_guard.rb', line 72

def auth_url
  
end

#login!(user, remember = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mini_auth/session_guard.rb', line 39

def login!(user, remember = false)
  @user = user
  @request.reset_session
  @request.session[session_storage_key] = user.id

  if remember
    @user.remember_token = SecureRandom.base58(48)
    @user.save!

    @request.cookie_jar.encrypted.signed[:remember_me] = {
      value: @user.remember_token,
      expires: (Time.now + @remember_time).utc,
      httponly: true
    }
  end

  @user
end

#logout!Object



58
59
60
61
62
63
64
65
66
# File 'lib/mini_auth/session_guard.rb', line 58

def logout!
  @user = nil

  @request.session.delete(session_storage_key)
  @request.cookie_jar.delete(:remember_me)
  @request.reset_session

  nil
end

#userObject



68
69
70
# File 'lib/mini_auth/session_guard.rb', line 68

def user
  @user ||= fetch_session_user
end