4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/generators/happy_seed/api/templates/app/controllers/api/v1/user_tokens_controller.rb', line 4
def create
respond_to do |format|
user = User.where('LOWER(email) = ?', user_params[:email].try(:downcase)).first
if user.present?
if user.valid_password?(user_params[:password])
if user.active_for_authentication?
user_token = user.user_tokens.create
if user_token.persisted?
format.json do
render json: { user_token: user_token_hash(user_token, user: true) }, status: :ok
end
else
format.json do
render json: { errors: user_token.errors }, status: :unprocessable_entity
end
end
else
format.json do
render json: { errors: { user: 'is locked' } }, status: :forbidden
end
end
else
format.json do
render json: { errors: { password: 'is invalid' } }, status: :forbidden
end
end
else
format.json do
render json: { errors: { email: 'not found' } }, status: :not_found
end
end
end
end
|