2013-02-06 03:16:51 +08:00
|
|
|
class SessionController < ApplicationController
|
|
|
|
|
|
|
|
def create
|
|
|
|
requires_parameter(:login, :password)
|
|
|
|
|
|
|
|
login = params[:login].downcase
|
|
|
|
login = login[1..-1] if login[0] == "@"
|
|
|
|
|
|
|
|
if login =~ /@/
|
|
|
|
@user = User.where(email: login).first
|
|
|
|
else
|
|
|
|
@user = User.where(username_lower: login).first
|
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
if @user.present?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# If the site requires user approval and the user is not approved yet
|
2013-03-05 08:42:44 +08:00
|
|
|
if SiteSetting.must_approve_users? && !@user.approved?
|
2013-03-23 02:08:11 +08:00
|
|
|
render json: {error: I18n.t("login.not_approved")}
|
2013-02-06 03:16:51 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# If their password is correct
|
|
|
|
if @user.confirm_password?(params[:password])
|
2013-02-12 00:18:26 +08:00
|
|
|
if @user.email_confirmed?
|
|
|
|
log_on_user(@user)
|
|
|
|
render_serialized(@user, UserSerializer)
|
|
|
|
return
|
|
|
|
else
|
2013-03-23 02:08:11 +08:00
|
|
|
render json: {error: I18n.t("login.not_activated"), reason: 'not_activated', sent_to_email: @user.email_logs.where(email_type: 'signup').order('created_at DESC').first.try(:to_address), current_email: @user.email}
|
2013-02-12 00:18:26 +08:00
|
|
|
return
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-23 02:08:11 +08:00
|
|
|
render json: {error: I18n.t("login.incorrect_username_email_or_password")}
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def forgot_password
|
|
|
|
requires_parameter(:username)
|
|
|
|
|
|
|
|
user = User.where('username_lower = :username or email = :username', username: params[:username].downcase).first
|
|
|
|
if user.present?
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
|
|
|
Jobs.enqueue(:user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token)
|
|
|
|
end
|
|
|
|
# always render of so we don't leak information
|
2013-03-23 02:08:11 +08:00
|
|
|
render json: {result: "ok"}
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
session[:current_user_id] = nil
|
|
|
|
cookies[:_t] = nil
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|