diff --git a/app/controllers/api/argyle_controller.rb b/app/controllers/api/argyle_controller.rb new file mode 100644 index 000000000..795f7e375 --- /dev/null +++ b/app/controllers/api/argyle_controller.rb @@ -0,0 +1,27 @@ +class Api::ArgyleController < ApplicationController + skip_before_action :verify_authenticity_token + + USER_TOKEN_ENDPOINT = 'https://api-sandbox.argyle.com/v2/user-tokens'; + + def update_token + cbv_flow = CbvFlow.find(session[:cbv_flow_id]) + new_token = refresh_token(cbv_flow.argyle_user_id) + + render json: { status: :ok, token: new_token['user_token'] } + end + + private + + def refresh_token(argyle_user_id) + res = Net::HTTP.post( + URI.parse(USER_TOKEN_ENDPOINT), + { "user": argyle_user_id }.to_json, + { + "Authorization" => "Basic #{Rails.application.credentials.argyle[:api_key]}", + "Content-Type" => "application/json" + } + ) + + JSON.parse(res.body) + end +end diff --git a/app/javascript/controllers/cbv_flows_controller.js b/app/javascript/controllers/cbv_flows_controller.js index 301ca576a..e1ecb2d26 100644 --- a/app/javascript/controllers/cbv_flows_controller.js +++ b/app/javascript/controllers/cbv_flows_controller.js @@ -2,7 +2,7 @@ import { Controller } from "@hotwired/stimulus" import * as ActionCable from '@rails/actioncable' import metaContent from "../utilities/meta"; -import { loadArgyle, initializeArgyle } from "../utilities/argyle" +import { loadArgyle, initializeArgyle, updateToken } from "../utilities/argyle" function toOptionHTML({ value }) { return ``; @@ -76,7 +76,7 @@ export default class extends Controller { // Unsure what these are for! onDDSSuccess: () => { console.log('onDDSSuccess') }, onDDSError: () => { console.log('onDDSSuccess') }, - onTokenExpired: updateToken => { console.log('onTokenExpired') } + onTokenExpired: updateToken, })) .then(argyle => this.argyle = argyle) .then(() => this.argyle.open()); diff --git a/app/javascript/utilities/argyle.js b/app/javascript/utilities/argyle.js index f0835886c..7a2347208 100644 --- a/app/javascript/utilities/argyle.js +++ b/app/javascript/utilities/argyle.js @@ -1,6 +1,8 @@ import loadScript from 'load-script'; import metaContent from "./meta"; +const ARGYLE_TOKENS_REFRESH = '/api/argyle/tokens'; + export function loadArgyle() { return new Promise((resolve, reject) => { loadScript('https://plugin.argyle.com/argyle.web.v5.js', (err, script) => { @@ -20,3 +22,9 @@ export function initializeArgyle(Argyle, userToken, callbacks) { ...callbacks }); } + +export const updateToken = async updateToken => { + const response = await fetch(ARGYLE_TOKENS_REFRESH, { method: 'post' }).then(response => response.json()); + + updateToken(response.token); +} diff --git a/config/routes.rb b/config/routes.rb index 99fe445dc..6e6b07b92 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,4 +27,10 @@ resources :events, only: :create end end + + namespace :api do + scope :argyle do + post '/tokens' => 'argyle#update_token' + end + end end diff --git a/spec/requests/api/argyle/tokens_spec.rb b/spec/requests/api/argyle/tokens_spec.rb new file mode 100644 index 000000000..7d773c9fc --- /dev/null +++ b/spec/requests/api/argyle/tokens_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe "Api::Argyle::Tokens", type: :request do + describe "GET /index" do + pending "add some examples (or delete) #{__FILE__}" + end +end