-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #2047 implement JWT on email confirmation
- Loading branch information
1 parent
366b66d
commit e14f847
Showing
7 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
app/controllers/spree/api/v2/storefront/anonymous_order_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Spree | ||
module Api | ||
module V2 | ||
module Storefront | ||
class AnonymousOrderController < Spree::Api::V2::BaseController | ||
def show_anonymous_order | ||
token = params[:token] | ||
order = order_jwt_token(token) | ||
|
||
if order | ||
render_serialized_payload { serialize_resource(order) } | ||
else | ||
render json: { error: 'Invalid or expired token' }, status: :unauthorized | ||
end | ||
end | ||
|
||
def resource_serializer | ||
Spree::V2::Storefront::OrderSerializer | ||
end | ||
|
||
private | ||
|
||
def order_jwt_token(token) | ||
decoded_token = SpreeCmCommissioner::OrderJwtToken.decode(token) | ||
|
||
order_number = decoded_token['order_number'] | ||
return nil unless order_number | ||
|
||
order = Spree::Order.find_by(number: order_number) | ||
return nil unless order | ||
|
||
decoded_token = SpreeCmCommissioner::OrderJwtToken.decode(token, order&.token) | ||
return nil unless decoded_token | ||
|
||
order | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module SpreeCmCommissioner | ||
class OrderJwtToken | ||
def self.encode(order) | ||
payload = { order_number: order.number, user_id: order.user_id, exp: 1.hour.from_now.to_i } | ||
JWT.encode(payload, order.token, 'HS256') | ||
end | ||
|
||
def self.decode(token, secret = nil) | ||
JWT.decode(token, secret, secret.present?, { algorithm: 'HS256' }).first | ||
rescue JWT::DecodeError | ||
nil | ||
end | ||
end | ||
end |