2012-02-25 22:43:17 +00:00
|
|
|
class SessionsController < ApplicationController
|
|
|
|
|
|
|
|
def create
|
2014-10-12 18:36:31 +00:00
|
|
|
user = login_service.validate(params[:token].to_s.strip)
|
2012-02-25 22:43:17 +00:00
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
if user
|
|
|
|
self.current_user = user
|
2014-10-17 15:28:22 +00:00
|
|
|
redirect_to_profile(user)
|
2013-08-19 15:08:13 +00:00
|
|
|
else
|
2014-10-12 18:36:31 +00:00
|
|
|
render :error
|
2012-03-05 23:25:21 +00:00
|
|
|
end
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
self.current_user = nil
|
2014-10-12 18:36:31 +00:00
|
|
|
redirect_to root_path, notice: "See you later!"
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-10-12 18:36:31 +00:00
|
|
|
def login_service
|
|
|
|
EmailLoginService.new
|
2012-03-10 13:58:10 +00:00
|
|
|
end
|
|
|
|
|
2014-10-17 15:28:22 +00:00
|
|
|
def redirect_to_profile(user)
|
|
|
|
if user.username
|
|
|
|
redirect_back_or_to profile_path(user), notice: login_notice(user)
|
|
|
|
else
|
|
|
|
redirect_to new_username_path, notice: login_notice(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-12 18:36:31 +00:00
|
|
|
def login_notice(user)
|
|
|
|
if user.first_login?
|
2014-12-17 15:46:03 +00:00
|
|
|
"Welcome to asciinema!"
|
2014-10-12 18:36:31 +00:00
|
|
|
else
|
|
|
|
"Welcome back!"
|
|
|
|
end
|
2013-10-19 16:25:30 +00:00
|
|
|
end
|
|
|
|
|
2012-02-25 22:43:17 +00:00
|
|
|
end
|