2014-02-20 22:00:44 +00:00
|
|
|
module WardenAuthentication
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-04-04 17:46:55 +00:00
|
|
|
def ensure_authenticated!
|
|
|
|
warden.authenticate!(scope: warden_scope) unless warden.authenticated?(warden_scope)
|
|
|
|
end
|
|
|
|
|
2014-02-20 22:00:44 +00:00
|
|
|
def current_user
|
2015-04-04 17:46:55 +00:00
|
|
|
warden.authenticate(scope: warden_scope) unless warden.authenticated?(warden_scope)
|
|
|
|
warden.user(warden_scope)
|
2014-02-20 22:00:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_user=(user)
|
|
|
|
if user
|
2015-04-04 17:46:55 +00:00
|
|
|
warden.set_user(user, scope: warden_scope)
|
2014-02-20 22:00:44 +00:00
|
|
|
cookies[:auth_token] =
|
|
|
|
{ value: user.auth_token, expires: 1.year.from_now }
|
|
|
|
else
|
2015-04-04 17:46:55 +00:00
|
|
|
warden.logout(warden_scope)
|
2014-02-20 22:00:44 +00:00
|
|
|
cookies.delete(:auth_token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def warden
|
|
|
|
request.env['warden']
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|