Class: V1::AuthorizationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/v1/authorizations_controller.rb

Overview

User authorization, get access token.

Author:

  • PineWong

Instance Method Summary collapse

Instance Method Details

#createUser

Create a access token

POST /v1/authorizations

Parameters:

  • email (String)

    the user email

  • password (String)

    the user password

Returns:

  • (User)

    the token


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/v1/authorizations_controller.rb', line 17

def create
  # Blank Email/Password
  if params[:user].blank? || 
      params[:user][:email].blank? ||
      params[:user][:password].blank?
    
    render json: { error: 'Blank Email/Password!' }, status: :bad_request        
    return
  end
  
  @user = User.find_by(email: params[:user][:email])

  # Invalid Email/Password
  if !@user || !@user.valid_password?(params[:user][:password])
    render json: { error: "Invalid Email/Password!" }, status: :unauthorized        
    return
  end

  render json: access_token, status: :created
end