Skip to content

Commit 51d2dcd

Browse files
Versioning the create user endpoint
1 parent f81f546 commit 51d2dcd

File tree

9 files changed

+63
-42
lines changed

9 files changed

+63
-42
lines changed

app/commands/authenticate_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def initialize(email, password)
77
end
88

99
def call
10-
JsonWebToken.encode(payload: {user_id: user.id}, secret_key: Rails.application.secrets.secret_key_base) if user
10+
JsonWebToken.encode(payload: { user_id: user.id }, secret_key: Rails.application.secrets.secret_key_base) if user
1111
end
1212

1313
private
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class GenerateVerifyToken
2-
prepend SimpleCommand
3-
4-
def initialize(user_id)
5-
@user_id = user_id
6-
end
7-
8-
def call
9-
JsonWebToken.encode(payload: {user_id: @user_id}, secret_key: Rails.application.secrets.secret_key_email)
10-
end
2+
prepend SimpleCommand
3+
4+
def initialize(user_id)
5+
@user_id = user_id
6+
end
7+
8+
def call
9+
JsonWebToken.encode(payload: { user_id: @user_id }, secret_key: Rails.application.secrets.secret_key_email)
10+
end
1111
end
12-

app/commands/json_web_token.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class JsonWebToken
22
class << self
33
def encode(payload:, exp: 24.hours.from_now, secret_key:)
4-
puts secret_key
54
payload[:exp] = exp.to_i
65
JWT.encode(payload, secret_key)
76
end

app/controllers/authentication_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ def confirm_email
2222
render json: { status: 200, message: "User confirmed" }.to_json
2323
rescue JWT::DecodeError => e
2424
render json: { status: 401, message: "Invalid token" }.to_json
25-
end
25+
end
2626
end
27-
28-
2927
end

app/controllers/users_controller.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,15 @@ def show
1717
end
1818

1919
# POST /users V1
20-
# def create
21-
# @user = User.new(user_params)
22-
# if @user.save
23-
# @token = AuthenticateUser.call(@user.email, @user.password)
24-
25-
# @result = { token: @token.result }
26-
27-
# response.set_header("auth_token", @token.result)
28-
# render json: @result, status: :created
29-
# else
30-
# render json: @user.errors, status: :unprocessable_entity
31-
# end
32-
# end
33-
34-
# POST /users V2
3520
def create
3621
@user = User.new(user_params)
3722
if @user.save
38-
@token = GenerateVerifyToken.call(@user.id)
39-
UserMailer.with(user: @user, token: @token).verify_email.deliver_now!
40-
render json: @token, status: :created
23+
@token = AuthenticateUser.call(@user.email, @user.password)
24+
25+
@result = { token: @token.result }
26+
27+
response.set_header("auth_token", @token.result)
28+
render json: @result, status: :created
4129
else
4230
render json: @user.errors, status: :unprocessable_entity
4331
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require "rest-client"
2+
class V1::UsersController < ApplicationController
3+
include ValidationsHelper
4+
include UsersDoc
5+
6+
skip_before_action :authenticate_request, only: [:create, :all]
7+
8+
before_action only: [:show, :update, :destroy] do
9+
set_user
10+
validate_user(:id, 0)
11+
end
12+
13+
# POST /users
14+
def create
15+
@user = User.new(user_params)
16+
if @user.save
17+
@token = GenerateVerifyToken.call(@user.id)
18+
UserMailer.with(user: @user, token: @token).verify_email.deliver_now!
19+
render json: @token, status: :created
20+
else
21+
render json: @user.errors, status: :unprocessable_entity
22+
end
23+
end
24+
25+
private
26+
def set_user
27+
@user = User.find(params[:id])
28+
end
29+
30+
def user_params
31+
params.require(:user).permit(:name, :email, :password, :password_confirmation)
32+
end
33+
end

config/environment.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
Rails.application.initialize!
66

77
ActionMailer::Base.smtp_settings = {
8-
:user_name => 'apikey',
9-
:password => '',
10-
:domain => 'gmail.com',
11-
:address => 'smtp.sendgrid.net',
12-
:port => 587,
13-
:authentication => :plain,
14-
:enable_starttls_auto => true
15-
}
8+
user_name: "apikey",
9+
password: "",
10+
domain: "gmail.com",
11+
address: "smtp.sendgrid.net",
12+
port: 587,
13+
authentication: :plain,
14+
enable_starttls_auto: true
15+
}

config/environments/development.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# Show full error reports.
1313
config.consider_all_requests_local = true
14-
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } # Talvez não funcione
14+
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
1515

1616
# Enable/disable caching. By default caching is disabled.
1717
if Rails.root.join("tmp/caching-dev.txt").exist?

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
post "projects/:id/reopen_issue", to: "issues#reopen_issue"
3636
post "/projects/:id/issues/graphic", to: "issues#issue_graphic_data"
3737

38+
namespace :v1 do
39+
post "/users", to: "users#create"
40+
end
41+
3842
resources :users, shallow: true do
3943
resources :projects do
4044
resources :grades

0 commit comments

Comments
 (0)