Skip to content

Commit

Permalink
Merge pull request #15 from DSACMS/mg-refresh-tokens-2
Browse files Browse the repository at this point in the history
Refresh expired token
  • Loading branch information
allthesignals authored May 6, 2024
2 parents be5fcc0 + f2eda70 commit 9f52ec5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
27 changes: 27 additions & 0 deletions app/controllers/api/argyle_controller.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions app/javascript/controllers/cbv_flows_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<option value='${value}'>${value}</option>`;
Expand Down Expand Up @@ -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());
Expand Down
8 changes: 8 additions & 0 deletions app/javascript/utilities/argyle.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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);
}
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@
resources :events, only: :create
end
end

namespace :api do
scope :argyle do
post '/tokens' => 'argyle#update_token'
end
end
end
7 changes: 7 additions & 0 deletions spec/requests/api/argyle/tokens_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9f52ec5

Please sign in to comment.