Class: Api::V1::AuthenticationController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/people/api/v1/authentication_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject

POST /api/1/authentication/authenticate



60
61
62
63
64
65
66
# File 'app/controllers/people/api/v1/authentication_controller.rb', line 60

def authenticate
  if authenticate_password
    render :json => {errors: "Email and/or Password is incorrect"}, status: :unauthorized 
    return
  end
  render json: {}
end

#loginObject

POST /api/1/authentication/login



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/people/api/v1/authentication_controller.rb', line 29

def 
  errors = "Email and/or Password is incorrect"
  user = ::People::V1::User.find_by(email: get_email)
  if is_locked?(user)
    return
  end
  if user && user.authenticate(params[:user][:password])
    (user,user.tokens.create)
  else
    if unsuccessful_login?(user)
      errors = "Account is locked due to too many failed login attempts"
    end
    render :json => {errors: errors}, status: :unauthorized 
  end
end

#login_statusObject

GET /api/1/authentication/login_status



54
55
56
57
# File 'app/controllers/people/api/v1/authentication_controller.rb', line 54

def 
  # Will give an unauthorized if not logged in
  render json: {}
end

#logoutObject

POST /api/1/authentication/logout



46
47
48
49
50
51
# File 'app/controllers/people/api/v1/authentication_controller.rb', line 46

def logout
  #Destroy the current token
  token = current_token
  token.destroy
  render json: {}
end

#registerObject

POST /api/1/authentication/register



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/people/api/v1/authentication_controller.rb', line 13

def register
  #Create a new user
  user = ::People::V1::User.new(user_params)
  if user.save
    #If the user is saved, return a token
    (user,user.tokens[0])
    #Send confirmation email here
    ::People::AccountMailer.welcome_email(user).deliver
    ::People::AccountMailer.email_confirmation(user).deliver
  else
    #Return an error if not saved
    render :json => {errors: user.errors.full_messages}, status: :unprocessable_entity
  end
end