From ff74bac8025462da9942580cdb47270177cf81a8 Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Sun, 13 Oct 2019 16:46:22 -0300
Subject: [PATCH 01/10] Adding endpoint for email verification
---
app/commands/authenticate_user.rb | 2 +-
app/commands/authorize_api_request.rb | 2 +-
app/commands/generate_verify_token.rb | 12 +++++++++
app/commands/json_web_token.rb | 9 ++++---
app/controllers/authentication_controller.rb | 16 ++++++++++++
app/controllers/users_controller.rb | 26 ++++++++++++++-----
app/mailers/user_mailer.rb | 6 +++++
config/routes.rb | 2 ++
config/secrets.yml | 2 +-
...3155716_add_authenticated_field_to_user.rb | 5 ++++
db/schema.rb | 3 ++-
11 files changed, 70 insertions(+), 15 deletions(-)
create mode 100644 app/commands/generate_verify_token.rb
create mode 100644 db/migrate/20191013155716_add_authenticated_field_to_user.rb
diff --git a/app/commands/authenticate_user.rb b/app/commands/authenticate_user.rb
index bfa0d71..d27b0d5 100644
--- a/app/commands/authenticate_user.rb
+++ b/app/commands/authenticate_user.rb
@@ -7,7 +7,7 @@ def initialize(email, password)
end
def call
- JsonWebToken.encode(user_id: user.id) if user
+ JsonWebToken.encode(payload: {user_id: user.id}, secret_key: Rails.application.secrets.secret_key_base) if user
end
private
diff --git a/app/commands/authorize_api_request.rb b/app/commands/authorize_api_request.rb
index 54b1df1..66152a7 100644
--- a/app/commands/authorize_api_request.rb
+++ b/app/commands/authorize_api_request.rb
@@ -18,7 +18,7 @@ def user
end
def decoded_auth_token
- @decoded_auth_token ||= JsonWebToken.decode(http_auth_header)
+ @decoded_auth_token ||= JsonWebToken.decode(token: http_auth_header, secret_key: Rails.application.secrets.secret_key_base)
end
def http_auth_header
diff --git a/app/commands/generate_verify_token.rb b/app/commands/generate_verify_token.rb
new file mode 100644
index 0000000..9b0f15a
--- /dev/null
+++ b/app/commands/generate_verify_token.rb
@@ -0,0 +1,12 @@
+class GenerateVerifyToken
+ prepend SimpleCommand
+
+ def initialize(user_id)
+ @user_id = user_id
+ end
+
+ def call
+ JsonWebToken.encode(payload: {user_id: @user_id}, secret_key: Rails.application.secrets.secret_key_email)
+ end
+end
+
\ No newline at end of file
diff --git a/app/commands/json_web_token.rb b/app/commands/json_web_token.rb
index ba4e9ed..1849864 100644
--- a/app/commands/json_web_token.rb
+++ b/app/commands/json_web_token.rb
@@ -1,12 +1,13 @@
class JsonWebToken
class << self
- def encode(payload, exp = 24.hours.from_now)
+ def encode(payload:, exp: 24.hours.from_now, secret_key:)
+ puts secret_key
payload[:exp] = exp.to_i
- JWT.encode(payload, Rails.application.secrets.secret_key_base)
+ JWT.encode(payload, secret_key)
end
- def decode(token)
- body = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
+ def decode(token:, secret_key:)
+ body = JWT.decode(token, secret_key)[0]
HashWithIndifferentAccess.new body
rescue
nil
diff --git a/app/controllers/authentication_controller.rb b/app/controllers/authentication_controller.rb
index b3ca520..9b8ede3 100644
--- a/app/controllers/authentication_controller.rb
+++ b/app/controllers/authentication_controller.rb
@@ -10,4 +10,20 @@ def authenticate
render json: { error: command.errors } , status: :unauthorized
end
end
+
+ def confirm_email
+ begin
+ token = params[:token]
+ decoded_token = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
+ current_user = User.find(decoded_token["user_id"])
+ email = current_user.email
+ current_user.confirmation_token = true
+ current_user.save
+ render json: { status: 200, message: "User confirmed" }.to_json
+ rescue JWT::DecodeError => e
+ render json: { status: 401, message: "Invalid token" }.to_json
+ end
+ end
+
+
end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index e04f37b..30a10b1 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -16,16 +16,28 @@ def show
render json: @user
end
- # POST /users
+ # POST /users V1
+ # def create
+ # @user = User.new(user_params)
+ # if @user.save
+ # @token = AuthenticateUser.call(@user.email, @user.password)
+
+ # @result = { token: @token.result }
+
+ # response.set_header("auth_token", @token.result)
+ # render json: @result, status: :created
+ # else
+ # render json: @user.errors, status: :unprocessable_entity
+ # end
+ # end
+
+ # POST /users V2
def create
@user = User.new(user_params)
if @user.save
- @token = AuthenticateUser.call(@user.email, @user.password)
-
- @result = { token: @token.result }
-
- response.set_header("auth_token", @token.result)
- render json: @result, status: :created
+ @token = GenerateVerifyToken.call(@user.id)
+ # UserMailer.with(user: user).recover_password_email.deliver_now
+ render json: @token, status: :created
else
render json: @user.errors, status: :unprocessable_entity
end
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 365a73d..aa93a00 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -6,4 +6,10 @@ def recover_password_email
@uri = ENV["PASSWORD_RESET_ADDRESS"].gsub(//, @user.reset_password_token)
mail(to: @user.email, subject: "Falko password recovery")
end
+
+ def verify_email
+ @email = params[:email]
+ @token = params[:token]
+ # @url
+ end
end
diff --git a/config/routes.rb b/config/routes.rb
index 8528c8c..973c387 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -6,6 +6,8 @@
post "request_github_token", to: "users#request_github_token"
post "remove_github_token", to: "users#remove_github_token"
+ post "verify_token", to: "authentication#confirm_email"
+
post "password/forgot", to: "passwords#forgot"
post "password/reset", to: "passwords#reset"
get "password/validate_token", to: "passwords#validate_token"
diff --git a/config/secrets.yml b/config/secrets.yml
index e57954b..d90a875 100644
--- a/config/secrets.yml
+++ b/config/secrets.yml
@@ -19,10 +19,10 @@
development:
secret_key_base: fbaf4d96ad5701178ae81fc6158701d7117a0e212845183f90429489101c4e3dadce2a63ef87f228b2dd2aa7109dda79c73a9386bac2bcd85237d9984d68cf5c
+ secret_key_email: vasco
test:
secret_key_base: 4b182d9b7c7d4c04e3229e2d0294952305f2c475cd73884316b3bf6230e95f00a53364702860ea59ecb6c88746ef52aa92043f0ab3bac84d4ecab1e1a8e0e0f4
-
# Do not keep production secrets in the unencrypted secrets file.
# Instead, either read values from the environment.
# Or, use `bin/rails secrets:setup` to configure encrypted secrets
diff --git a/db/migrate/20191013155716_add_authenticated_field_to_user.rb b/db/migrate/20191013155716_add_authenticated_field_to_user.rb
new file mode 100644
index 0000000..5c314de
--- /dev/null
+++ b/db/migrate/20191013155716_add_authenticated_field_to_user.rb
@@ -0,0 +1,5 @@
+class AddAuthenticatedFieldToUser < ActiveRecord::Migration[5.1]
+ def up
+ add_column :users, :confirmation_token, :boolean, default: false
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index c788f10..71ebfaf 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20190418161604) do
+ActiveRecord::Schema.define(version: 20191013155716) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -105,6 +105,7 @@
t.string "access_token"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
+ t.boolean "confirmation_token", default: false
end
add_foreign_key "grades", "projects"
From 1025e480ccee4fea3cf6e49332a290bd8381ab8e Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Mon, 14 Oct 2019 16:47:17 -0300
Subject: [PATCH 02/10] Sending email without token
---
app/controllers/users_controller.rb | 2 +-
app/mailers/application_mailer.rb | 2 +-
app/mailers/user_mailer.rb | 10 ++++++++--
app/views/layouts/verify_email.html.erb | 10 ++++++++++
app/views/layouts/verify_email.text.erb | 5 +++++
app/views/user_mailer/verify_email.html.erb | 10 ++++++++++
app/views/user_mailer/verify_email.text.erb | 5 +++++
config/environments/development.rb | 1 +
8 files changed, 41 insertions(+), 4 deletions(-)
create mode 100644 app/views/layouts/verify_email.html.erb
create mode 100644 app/views/layouts/verify_email.text.erb
create mode 100644 app/views/user_mailer/verify_email.html.erb
create mode 100644 app/views/user_mailer/verify_email.text.erb
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 30a10b1..4bfad2c 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -36,7 +36,7 @@ def create
@user = User.new(user_params)
if @user.save
@token = GenerateVerifyToken.call(@user.id)
- # UserMailer.with(user: user).recover_password_email.deliver_now
+ UserMailer.with(user: @user).verify_email.deliver_now!
render json: @token, status: :created
else
render json: @user.errors, status: :unprocessable_entity
diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb
index 3c34c81..d88720e 100644
--- a/app/mailers/application_mailer.rb
+++ b/app/mailers/application_mailer.rb
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
- layout "mailer"
+ layout "verify_email"
end
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index aa93a00..c1b47a7 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -1,5 +1,6 @@
class UserMailer < ApplicationMailer
default from: "noreply@falko.com"
+ layout "verify_email"
def recover_password_email
@user = params[:user]
@@ -8,8 +9,13 @@ def recover_password_email
end
def verify_email
- @email = params[:email]
- @token = params[:token]
+ # @email = params[:email]
+ # @token = params[:token]
+ @user = params[:user]
+ # puts "user = ", user[:email]
+ @email = @user[:email]
+ # puts "user = ", email
# @url
+ mail to: @email, subject: "Email confirmation token"
end
end
diff --git a/app/views/layouts/verify_email.html.erb b/app/views/layouts/verify_email.html.erb
new file mode 100644
index 0000000..6bec646
--- /dev/null
+++ b/app/views/layouts/verify_email.html.erb
@@ -0,0 +1,10 @@
+Email Confirmation
+
+
+ Hi <%= @email %>,
+
+
+
+ Confirm your email <%= @token %>.
+ :)
+
\ No newline at end of file
diff --git a/app/views/layouts/verify_email.text.erb b/app/views/layouts/verify_email.text.erb
new file mode 100644
index 0000000..e4bd2ac
--- /dev/null
+++ b/app/views/layouts/verify_email.text.erb
@@ -0,0 +1,5 @@
+Email Confirmation
+
+ Hi <%= @email %>,
+ Confirm your email <%= @token %>.
+ :)
diff --git a/app/views/user_mailer/verify_email.html.erb b/app/views/user_mailer/verify_email.html.erb
new file mode 100644
index 0000000..6bec646
--- /dev/null
+++ b/app/views/user_mailer/verify_email.html.erb
@@ -0,0 +1,10 @@
+Email Confirmation
+
+
+ Hi <%= @email %>,
+
+
+
+ Confirm your email <%= @token %>.
+ :)
+
\ No newline at end of file
diff --git a/app/views/user_mailer/verify_email.text.erb b/app/views/user_mailer/verify_email.text.erb
new file mode 100644
index 0000000..e4bd2ac
--- /dev/null
+++ b/app/views/user_mailer/verify_email.text.erb
@@ -0,0 +1,5 @@
+Email Confirmation
+
+ Hi <%= @email %>,
+ Confirm your email <%= @token %>.
+ :)
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 8146fea..62ddcc2 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -11,6 +11,7 @@
# Show full error reports.
config.consider_all_requests_local = true
+ config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } # Talvez não funcione
# Enable/disable caching. By default caching is disabled.
if Rails.root.join("tmp/caching-dev.txt").exist?
From 587b20cbff4626b5b5f294624ce6ac6630163ae3 Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Wed, 23 Oct 2019 00:20:00 -0300
Subject: [PATCH 03/10] Adding token to confirmation email
---
app/controllers/users_controller.rb | 2 +-
app/mailers/user_mailer.rb | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 4bfad2c..10cbabe 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -36,7 +36,7 @@ def create
@user = User.new(user_params)
if @user.save
@token = GenerateVerifyToken.call(@user.id)
- UserMailer.with(user: @user).verify_email.deliver_now!
+ UserMailer.with(user: @user, token: @token).verify_email.deliver_now!
render json: @token, status: :created
else
render json: @user.errors, status: :unprocessable_entity
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index c1b47a7..16dd299 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -14,8 +14,11 @@ def verify_email
@user = params[:user]
# puts "user = ", user[:email]
@email = @user[:email]
+ userToken = params[:token]
+ @token = userToken.result
# puts "user = ", email
# @url
+ puts "token = ", @token
mail to: @email, subject: "Email confirmation token"
end
end
From cc74f2145efe63223de1cf7b418be0b5ef3c5f03 Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Wed, 23 Oct 2019 23:23:35 -0300
Subject: [PATCH 04/10] Adding sendgrid config
---
config/environment.rb | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/config/environment.rb b/config/environment.rb
index cac5315..59c9c99 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -3,3 +3,13 @@
# Initialize the Rails application.
Rails.application.initialize!
+
+ActionMailer::Base.smtp_settings = {
+ :user_name => 'apikey',
+ :password => '',
+ :domain => 'gmail.com',
+ :address => 'smtp.sendgrid.net',
+ :port => 587,
+ :authentication => :plain,
+ :enable_starttls_auto => true
+}
\ No newline at end of file
From 68634c053f67cba103c2a564c688cffb34d00210 Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Thu, 24 Oct 2019 00:11:18 -0300
Subject: [PATCH 05/10] V1 of the sending mail
---
app/controllers/authentication_controller.rb | 4 ++--
app/mailers/user_mailer.rb | 10 ++--------
app/views/layouts/verify_email.html.erb | 3 ++-
app/views/layouts/verify_email.text.erb | 10 ++++++++--
app/views/user_mailer/verify_email.html.erb | 3 ++-
app/views/user_mailer/verify_email.text.erb | 10 ++++++++--
config/routes.rb | 2 +-
7 files changed, 25 insertions(+), 17 deletions(-)
diff --git a/app/controllers/authentication_controller.rb b/app/controllers/authentication_controller.rb
index 9b8ede3..3eb41ed 100644
--- a/app/controllers/authentication_controller.rb
+++ b/app/controllers/authentication_controller.rb
@@ -14,8 +14,8 @@ def authenticate
def confirm_email
begin
token = params[:token]
- decoded_token = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
- current_user = User.find(decoded_token["user_id"])
+ decoded_token = JWT.decode(token, Rails.application.secrets.secret_key_email)[0]
+ current_user = User.find(decoded_token["user_id"].to_i)
email = current_user.email
current_user.confirmation_token = true
current_user.save
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 16dd299..af399d1 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -9,16 +9,10 @@ def recover_password_email
end
def verify_email
- # @email = params[:email]
- # @token = params[:token]
@user = params[:user]
- # puts "user = ", user[:email]
@email = @user[:email]
- userToken = params[:token]
- @token = userToken.result
- # puts "user = ", email
- # @url
- puts "token = ", @token
+ user_token = params[:token]
+ @token = user_token.result
mail to: @email, subject: "Email confirmation token"
end
end
diff --git a/app/views/layouts/verify_email.html.erb b/app/views/layouts/verify_email.html.erb
index 6bec646..62edbbf 100644
--- a/app/views/layouts/verify_email.html.erb
+++ b/app/views/layouts/verify_email.html.erb
@@ -5,6 +5,7 @@
- Confirm your email <%= @token %>.
+ Confirm your email http://localhost:3000/verify_token/?token=<%= @token %>
+
:)
\ No newline at end of file
diff --git a/app/views/layouts/verify_email.text.erb b/app/views/layouts/verify_email.text.erb
index e4bd2ac..62edbbf 100644
--- a/app/views/layouts/verify_email.text.erb
+++ b/app/views/layouts/verify_email.text.erb
@@ -1,5 +1,11 @@
-Email Confirmation
+Email Confirmation
+
Hi <%= @email %>,
- Confirm your email <%= @token %>.
+
+
+
+ Confirm your email http://localhost:3000/verify_token/?token=<%= @token %>
+
:)
+
\ No newline at end of file
diff --git a/app/views/user_mailer/verify_email.html.erb b/app/views/user_mailer/verify_email.html.erb
index 6bec646..62edbbf 100644
--- a/app/views/user_mailer/verify_email.html.erb
+++ b/app/views/user_mailer/verify_email.html.erb
@@ -5,6 +5,7 @@
- Confirm your email <%= @token %>.
+ Confirm your email http://localhost:3000/verify_token/?token=<%= @token %>
+
:)
\ No newline at end of file
diff --git a/app/views/user_mailer/verify_email.text.erb b/app/views/user_mailer/verify_email.text.erb
index e4bd2ac..62edbbf 100644
--- a/app/views/user_mailer/verify_email.text.erb
+++ b/app/views/user_mailer/verify_email.text.erb
@@ -1,5 +1,11 @@
-Email Confirmation
+Email Confirmation
+
Hi <%= @email %>,
- Confirm your email <%= @token %>.
+
+
+
+ Confirm your email http://localhost:3000/verify_token/?token=<%= @token %>
+
:)
+
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index 973c387..e073b13 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -6,7 +6,7 @@
post "request_github_token", to: "users#request_github_token"
post "remove_github_token", to: "users#remove_github_token"
- post "verify_token", to: "authentication#confirm_email"
+ get "verify_token", to: "authentication#confirm_email"
post "password/forgot", to: "passwords#forgot"
post "password/reset", to: "passwords#reset"
From f81f54634aa575cb7d100cf25441d3369d431712 Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Thu, 24 Oct 2019 00:28:52 -0300
Subject: [PATCH 06/10] Changing secret key email
---
config/secrets.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/config/secrets.yml b/config/secrets.yml
index d90a875..461360d 100644
--- a/config/secrets.yml
+++ b/config/secrets.yml
@@ -19,7 +19,7 @@
development:
secret_key_base: fbaf4d96ad5701178ae81fc6158701d7117a0e212845183f90429489101c4e3dadce2a63ef87f228b2dd2aa7109dda79c73a9386bac2bcd85237d9984d68cf5c
- secret_key_email: vasco
+ secret_key_email: fd701599009e745a0eaf501075679588344bb385e979a77f01223f87918a1fae9fa306d97216422ead69c3767a360f1f12587d44ba9b9ad04b3a28acf71c65bb
test:
secret_key_base: 4b182d9b7c7d4c04e3229e2d0294952305f2c475cd73884316b3bf6230e95f00a53364702860ea59ecb6c88746ef52aa92043f0ab3bac84d4ecab1e1a8e0e0f4
From 51d2dcd5a6cd9450ec85201225fd5854da44790f Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Thu, 24 Oct 2019 10:58:50 -0300
Subject: [PATCH 07/10] Versioning the create user endpoint
---
app/commands/authenticate_user.rb | 2 +-
app/commands/generate_verify_token.rb | 19 ++++++-----
app/commands/json_web_token.rb | 1 -
app/controllers/authentication_controller.rb | 4 +--
app/controllers/users_controller.rb | 24 ++++----------
app/controllers/v1/users_controller.rb | 33 ++++++++++++++++++++
config/environment.rb | 16 +++++-----
config/environments/development.rb | 2 +-
config/routes.rb | 4 +++
9 files changed, 63 insertions(+), 42 deletions(-)
create mode 100644 app/controllers/v1/users_controller.rb
diff --git a/app/commands/authenticate_user.rb b/app/commands/authenticate_user.rb
index d27b0d5..5684892 100644
--- a/app/commands/authenticate_user.rb
+++ b/app/commands/authenticate_user.rb
@@ -7,7 +7,7 @@ def initialize(email, password)
end
def call
- JsonWebToken.encode(payload: {user_id: user.id}, secret_key: Rails.application.secrets.secret_key_base) if user
+ JsonWebToken.encode(payload: { user_id: user.id }, secret_key: Rails.application.secrets.secret_key_base) if user
end
private
diff --git a/app/commands/generate_verify_token.rb b/app/commands/generate_verify_token.rb
index 9b0f15a..a049c0f 100644
--- a/app/commands/generate_verify_token.rb
+++ b/app/commands/generate_verify_token.rb
@@ -1,12 +1,11 @@
class GenerateVerifyToken
- prepend SimpleCommand
-
- def initialize(user_id)
- @user_id = user_id
- end
-
- def call
- JsonWebToken.encode(payload: {user_id: @user_id}, secret_key: Rails.application.secrets.secret_key_email)
- end
+ prepend SimpleCommand
+
+ def initialize(user_id)
+ @user_id = user_id
+ end
+
+ def call
+ JsonWebToken.encode(payload: { user_id: @user_id }, secret_key: Rails.application.secrets.secret_key_email)
+ end
end
-
\ No newline at end of file
diff --git a/app/commands/json_web_token.rb b/app/commands/json_web_token.rb
index 1849864..de9b844 100644
--- a/app/commands/json_web_token.rb
+++ b/app/commands/json_web_token.rb
@@ -1,7 +1,6 @@
class JsonWebToken
class << self
def encode(payload:, exp: 24.hours.from_now, secret_key:)
- puts secret_key
payload[:exp] = exp.to_i
JWT.encode(payload, secret_key)
end
diff --git a/app/controllers/authentication_controller.rb b/app/controllers/authentication_controller.rb
index 3eb41ed..e5e53ad 100644
--- a/app/controllers/authentication_controller.rb
+++ b/app/controllers/authentication_controller.rb
@@ -22,8 +22,6 @@ def confirm_email
render json: { status: 200, message: "User confirmed" }.to_json
rescue JWT::DecodeError => e
render json: { status: 401, message: "Invalid token" }.to_json
- end
+ end
end
-
-
end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 10cbabe..1822e14 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -17,27 +17,15 @@ def show
end
# POST /users V1
- # def create
- # @user = User.new(user_params)
- # if @user.save
- # @token = AuthenticateUser.call(@user.email, @user.password)
-
- # @result = { token: @token.result }
-
- # response.set_header("auth_token", @token.result)
- # render json: @result, status: :created
- # else
- # render json: @user.errors, status: :unprocessable_entity
- # end
- # end
-
- # POST /users V2
def create
@user = User.new(user_params)
if @user.save
- @token = GenerateVerifyToken.call(@user.id)
- UserMailer.with(user: @user, token: @token).verify_email.deliver_now!
- render json: @token, status: :created
+ @token = AuthenticateUser.call(@user.email, @user.password)
+
+ @result = { token: @token.result }
+
+ response.set_header("auth_token", @token.result)
+ render json: @result, status: :created
else
render json: @user.errors, status: :unprocessable_entity
end
diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb
new file mode 100644
index 0000000..6cacfe5
--- /dev/null
+++ b/app/controllers/v1/users_controller.rb
@@ -0,0 +1,33 @@
+require "rest-client"
+class V1::UsersController < ApplicationController
+ include ValidationsHelper
+ include UsersDoc
+
+ skip_before_action :authenticate_request, only: [:create, :all]
+
+ before_action only: [:show, :update, :destroy] do
+ set_user
+ validate_user(:id, 0)
+ end
+
+ # POST /users
+ def create
+ @user = User.new(user_params)
+ if @user.save
+ @token = GenerateVerifyToken.call(@user.id)
+ UserMailer.with(user: @user, token: @token).verify_email.deliver_now!
+ render json: @token, status: :created
+ else
+ render json: @user.errors, status: :unprocessable_entity
+ end
+ end
+
+ private
+ def set_user
+ @user = User.find(params[:id])
+ end
+
+ def user_params
+ params.require(:user).permit(:name, :email, :password, :password_confirmation)
+ end
+end
diff --git a/config/environment.rb b/config/environment.rb
index 59c9c99..2d93214 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -5,11 +5,11 @@
Rails.application.initialize!
ActionMailer::Base.smtp_settings = {
- :user_name => 'apikey',
- :password => '',
- :domain => 'gmail.com',
- :address => 'smtp.sendgrid.net',
- :port => 587,
- :authentication => :plain,
- :enable_starttls_auto => true
-}
\ No newline at end of file
+ user_name: "apikey",
+ password: "",
+ domain: "gmail.com",
+ address: "smtp.sendgrid.net",
+ port: 587,
+ authentication: :plain,
+ enable_starttls_auto: true
+}
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 62ddcc2..9ecd98f 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -11,7 +11,7 @@
# Show full error reports.
config.consider_all_requests_local = true
- config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } # Talvez não funcione
+ config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
# Enable/disable caching. By default caching is disabled.
if Rails.root.join("tmp/caching-dev.txt").exist?
diff --git a/config/routes.rb b/config/routes.rb
index e073b13..dce0ed3 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -35,6 +35,10 @@
post "projects/:id/reopen_issue", to: "issues#reopen_issue"
post "/projects/:id/issues/graphic", to: "issues#issue_graphic_data"
+ namespace :v1 do
+ post "/users", to: "users#create"
+ end
+
resources :users, shallow: true do
resources :projects do
resources :grades
From 14604af0bec96a3d70d7df697743c06215c2cc6c Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Thu, 24 Oct 2019 11:22:59 -0300
Subject: [PATCH 08/10] Fixing rubocop offenses
---
app/controllers/v1/users_controller.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb
index 6cacfe5..800dcba 100644
--- a/app/controllers/v1/users_controller.rb
+++ b/app/controllers/v1/users_controller.rb
@@ -10,7 +10,7 @@ class V1::UsersController < ApplicationController
validate_user(:id, 0)
end
- # POST /users
+ # POST /users
def create
@user = User.new(user_params)
if @user.save
From 73d32b719dc6dc2ddef8f12f7a6e9d7f6318c03a Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Fri, 15 Nov 2019 21:28:32 -0300
Subject: [PATCH 09/10] adding exception handling to the UsersController show
method
---
app/controllers/users_controller.rb | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 1822e14..0ef2fd4 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -12,8 +12,14 @@ class UsersController < ApplicationController
# GET /users/1
def show
- @user = User.find(params[:id].to_i)
- render json: @user
+ begin
+ @user = User.find(params[:id].to_i)
+ render json: @user
+ rescue ActiveRecord::RecordNotFound => e
+ render json: {
+ error: e.to_s
+ }, status: :not_found
+ end
end
# POST /users V1
From 6e206315df62ec1a302b3a3fb554dd1d51d66bd5 Mon Sep 17 00:00:00 2001
From: Guilherme Siqueira
Date: Sat, 16 Nov 2019 12:44:49 -0300
Subject: [PATCH 10/10] adding exception handling to the UsersController
set_user method
---
app/controllers/users_controller.rb | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 0ef2fd4..24d486f 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -92,7 +92,13 @@ def destroy
private
def set_user
- @user = User.find(params[:id])
+ begin
+ @user = User.find(params[:id])
+ rescue ActiveRecord::RecordNotFound => e
+ render json: {
+ error: e.to_s
+ }, status: :not_found
+ end
end
def user_params