Class: Intra::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/intra/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Session

Returns a new instance of Session.



3
4
5
6
7
8
# File 'lib/intra/session.rb', line 3

def initialize(env)
  @env = env
  @current_user = nil
  @cookies = nil
  check_timeout
end

Instance Method Details



10
11
12
13
14
15
16
17
# File 'lib/intra/session.rb', line 10

def add_cookie_to_headers(headers)
  if current_user&.remember_token
    value = cookie_value
    value['remember_token'] = current_user.remember_token
    options = cookie_options.merge value: value.to_json
    Rack::Utils.set_cookie_header! headers, INTRA_COOKIE, options
  end
end

#check_timeoutObject



45
46
47
# File 'lib/intra/session.rb', line 45

def check_timeout
  sign_out if timed_out?
end

#current_userObject



19
20
21
22
23
24
# File 'lib/intra/session.rb', line 19

def current_user
  if remember_token.present?
    @current_user ||= user_model.find_by(remember_token: remember_token)
  end
  @current_user
end

#sign_in(user) ⇒ Object



26
27
28
# File 'lib/intra/session.rb', line 26

def (user)
  @current_user = user
end

#sign_outObject



30
31
32
33
34
35
# File 'lib/intra/session.rb', line 30

def sign_out
  current_user.reset_remember_token! if signed_in?

  @current_user = nil
  cookies.delete INTRA_COOKIE
end

#signed_in?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/intra/session.rb', line 37

def signed_in?
  current_user.present?
end

#signed_out?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/intra/session.rb', line 41

def signed_out?
  !signed_in?
end