summaryrefslogtreecommitdiff
path: root/app/helpers
diff options
context:
space:
mode:
authornfoy <nfoy@purdue.edu>2014-04-03 16:15:46 -0400
committernfoy <nfoy@purdue.edu>2014-04-03 16:15:46 -0400
commit5169363d6698fb87372efcb7ace552b89953584e (patch)
treeff42d819be510dfe929b4cd9dbc1cd25f51c9785 /app/helpers
parent2b79a033262dfe610eb22b7f6b3614db9cb134b1 (diff)
parent9f19d0e16d7920e07255c0fbe596c518d1aa415f (diff)
Merge branch 'master' of https://github.com/LukeShu/leaguer
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/remote_usernames_helper.rb2
-rw-r--r--app/helpers/sessions_helper.rb110
2 files changed, 53 insertions, 59 deletions
diff --git a/app/helpers/remote_usernames_helper.rb b/app/helpers/remote_usernames_helper.rb
new file mode 100644
index 0000000..3240c4f
--- /dev/null
+++ b/app/helpers/remote_usernames_helper.rb
@@ -0,0 +1,2 @@
+module RemoteUsernamesHelper
+end
diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb
index 046ca6f..54e4b0d 100644
--- a/app/helpers/sessions_helper.rb
+++ b/app/helpers/sessions_helper.rb
@@ -1,73 +1,65 @@
module SessionsHelper
-
def sign_in(user)
- #create a new remember token
- remember_token = User.new_remember_token
- #place token inside of the browser
- cookies.permanent[:remember_token] = remember_token
- #save the hashed token to the database
- user.update_attribute(:remember_token,
- User.hash(remember_token))
- #set the current user to be the given user
- self.current_user = user
- end
+ @session = Session.new(user: user)
+ raw_token = @session.create_token
+ @session.save # FIXME: error handling
-# The curret_user=(user) is the conversion of self.current_user = user
- def current_user=(user)
+ @token = Session.hash_token(raw_token)
+ cookies.permanent[:remember_token] = raw_token
+
+ #set the current user to be the given user
@current_user = user
end
-# sets the @current_user instance virable to the user corresponding
-# to the remember token, but only if @current_user is undefined
-# since the remember token is hashed, we need to hash the cookie
-# to find match the remember token
- def current_user
- remember_token = User.hash(cookies[:remember_token])
- @current_user ||= User.find_by(remember_token: remember_token)
- end
+ # sets the @current_user instance virable to the user corresponding
+ # to the remember token, but only if @current_user is undefined
+ # since the remember token is hashed, we need to hash the cookie
+ # to find match the remember token
+ def current_user
+ @token ||= Session.hash_token(cookies[:remember_token])
+ @session ||= Session.find_by(token: @token)
+ @current_user ||= (@session.nil? ? nil : @session.user)
+ end
# checks if someone is currently signed in
def signed_in?
!current_user.nil?
end
- def sign_out
- current_user.update_attribute(:remember_token, User.hash(User.new_remember_token))
- cookies.delete(:remember_token)
- self.current_user = nil
- end
-
-=begin
-
-This is for anyone that cares about how long a user is signed
-in:
-
-Currently I have a user to be signed in forever unless they
-log out (cookies.permanent....).
-
-If you want to change that, change line 7 to this:
-
-cookies[:remember_token] = { value: remember_token,
- expires: 20.years.from_now.utc }
-
-which will expire the cookie in 20 years from its date of
-creation.
-
-Oddly enough, this line above is equivalent to the:
-
-cookies.permanent
-
-This is just a short cut for this line since most people
-create permanent cookies these days.
-
-Other times are:
-
-10.weeks.from_now
-
-5.days.ago
-
-etc...
-
-=end
+ def sign_out
+ if signed_in?
+ @session.destroy
+ end
+ @current_user = nil
+ cookies.delete(:remember_token)
+ end
+ # This is for anyone that cares about how long a user is signed
+ # in:
+ #
+ # Currently I have a user to be signed in forever unless they
+ # log out (cookies.permanent....).
+ #
+ # If you want to change that, change line 7 to this:
+ #
+ # cookies[:remember_token] = { value: remember_token,
+ # expires: 20.years.from_now.utc }
+ #
+ # which will expire the cookie in 20 years from its date of
+ # creation.
+ #
+ # Oddly enough, this line above is equivalent to the:
+ #
+ # cookies.permanent
+ #
+ # This is just a short cut for this line since most people
+ # create permanent cookies these days.
+ #
+ # Other times are:
+ #
+ # 10.weeks.from_now
+ #
+ # 5.days.ago
+ #
+ # etc...
end