From 1a73ed731ea17c46ac7823577143047097927326 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Thu, 9 Jul 2020 00:00:28 -0700 Subject: check for valid API key, then display extracted hypothes.is username --- .../apis/HypothesisAuthenticationManager.tsx | 6 ++++-- src/client/apis/hypothesis/HypothesisApiUtils.ts | 25 ++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/client/apis/HypothesisAuthenticationManager.tsx b/src/client/apis/HypothesisAuthenticationManager.tsx index cffb87227..b299f233e 100644 --- a/src/client/apis/HypothesisAuthenticationManager.tsx +++ b/src/client/apis/HypothesisAuthenticationManager.tsx @@ -6,6 +6,7 @@ import { Opt } from "../../fields/Doc"; import { Networking } from "../Network"; import "./HypothesisAuthenticationManager.scss"; import { Scripting } from "../util/Scripting"; +import { Hypothesis } from "./hypothesis/HypothesisApiUtils"; const prompt = "Paste authorization code here..."; @@ -44,12 +45,13 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> this.disposer = reaction( () => this.authenticationCode, async authenticationCode => { - if (authenticationCode) { + const userProfile = authenticationCode && await Hypothesis.fetchUser(authenticationCode); + if (userProfile && userProfile.userid !== null) { this.disposer?.(); Networking.PostToServer("/writeHypothesisAccessToken", { authenticationCode }); runInAction(() => { this.success = true; - this.credentials = response; + this.credentials = Hypothesis.extractUsername(userProfile.userid); // extract username from profile }); this.resetState(); resolve(authenticationCode); diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index fe35f5831..ab83630a9 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -14,7 +14,7 @@ export namespace Hypothesis { * Searches for annotations made by @param username that * contain @param searchKeyWord */ - export const searchAnnotation = async (username: String, searchKeyWord: String) => { + export const searchAnnotation = async (username: string, searchKeyWord: string) => { const base = 'https://api.hypothes.is/api/search'; const request = base + `?user=acct:${username}@hypothes.is&text=${searchKeyWord}`; console.log("DASH Querying " + request); @@ -26,6 +26,19 @@ export namespace Hypothesis { } }; + export const fetchUser = async (apiKey: string) => { + const response = await fetch('https://api.hypothes.is/api/profile', { + headers: { + 'Authorization': `Bearer ${apiKey}`, + }, + }); + if (response.ok) { + return response.json(); + } else { + throw new Error('DASH: Error in fetchUser GET request'); + } + }; + // Find the most recent placeholder annotation created, and return its ID export const getPlaceholderId = async (username: String, searchKeyWord: String) => { const getResponse = await Hypothesis.searchAnnotation(username, searchKeyWord); @@ -54,9 +67,9 @@ export namespace Hypothesis { return `https://hyp.is/${annotationId}/${baseUrl}`; }; - // export const checkValidApiKey = async (apiKey: string) => { - // const response = await fetch("https://api.hypothes.is/api/profile", { - - // }); - // }; + // Extract username from Hypothe.is's userId format + export const extractUsername = (userid: string) => { + const exp: RegExp = /(?<=\:)(.*?)(?=\@)/; + return exp.exec(userid)![0]; + }; } \ No newline at end of file -- cgit v1.2.3-70-g09d2