-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4e9af2f
commit 1d40c05
Showing
9 changed files
with
62 additions
and
64 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/controllers/spree/api/v2/storefront/anonymous_line_item_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,39 @@ | ||
module Spree | ||
module Api | ||
module V2 | ||
module Storefront | ||
class AnonymousLineItemController < Spree::Api::V2::BaseController | ||
def show_anonymous_line_item | ||
token = params[:token] | ||
line_item = line_item_jwt_token(token) | ||
if line_item | ||
render_serialized_payload { serialize_resource(line_item) } | ||
else | ||
render json: { error: 'Invalid or expired token' }, status: :unauthorized | ||
end | ||
end | ||
|
||
def resource_serializer | ||
Spree::V2::Storefront::LineItemSerializer | ||
end | ||
|
||
private | ||
|
||
def line_item_jwt_token(token) | ||
decoded_token = SpreeCmCommissioner::LineItemJwtToken.decode(token) | ||
|
||
line_item_id = decoded_token['line_item_id'] | ||
|
||
line_item = Spree::LineItem.find(line_item_id) | ||
return nil unless line_item | ||
|
||
decoded_token = SpreeCmCommissioner::LineItemJwtToken.decode(token, line_item&.order&.token) | ||
return nil unless decoded_token | ||
|
||
line_item | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
41 changes: 0 additions & 41 deletions
41
app/controllers/spree/api/v2/storefront/anonymous_order_controller.rb
This file was deleted.
Oops, something went wrong.
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 LineItemJwtToken | ||
def self.encode(line_item) | ||
payload = { order_number: line_item.order.number, line_item_id: line_item.id, exp: 1.hour.from_now.to_i } | ||
JWT.encode(payload, line_item.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 |
This file was deleted.
Oops, something went wrong.