Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh expired token #15

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, this is an instance of a method that I'll add to the Argyle Service

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
Loading