Class: TokenAuthBox::AuthController

Inherits:
ApplicationController show all
Includes:
Middleware
Defined in:
app/controllers/token_auth_box/auth_controller.rb

Instance Method Summary collapse

Methods included from Middleware

#ensure_signed_in

Instance Method Details

#loginObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/token_auth_box/auth_controller.rb', line 24

def 
  data = user_params
  user = User.find_by(email: data["email"])
    .try(:authenticate, data["password"])

  if !user
    render json: 'no', status: :unauthorized
  else 
    user_data = user_response_data(user)
    token = TokenService.gen_token user_data
    render json: { user: user_data, token: token }
  end
end

#middleObject



8
9
10
# File 'app/controllers/token_auth_box/auth_controller.rb', line 8

def middle
  render json: :ok
end

#registerObject



12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/token_auth_box/auth_controller.rb', line 12

def register
  user = User.new(user_params)
  if user.save
    data = user_response_data(user)
    token = TokenService.gen_token data

    render json: { user: data, token: token }
  else
    render json: user.errors.messages
  end
end

#verifyObject



38
39
40
41
42
43
44
45
46
# File 'app/controllers/token_auth_box/auth_controller.rb', line 38

def verify
  token = get_header
  begin
    user_data = JWT.decode(token, nil, false).first
    render json: user_data.to_json
  rescue
    render json: 'oopsie', status: :unauthorized
  end
end