aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMelissa Zhang <mzhang19096@gmail.com>2020-07-09 00:00:28 -0700
committerMelissa Zhang <mzhang19096@gmail.com>2020-07-09 00:00:28 -0700
commit1a73ed731ea17c46ac7823577143047097927326 (patch)
tree2f31bb0dc7710fe1f7749c6ae8eebe788a38750f /src
parent1e988c7c6d927630059645ebee05261619aba7b4 (diff)
check for valid API key, then display extracted hypothes.is username
Diffstat (limited to 'src')
-rw-r--r--src/client/apis/HypothesisAuthenticationManager.tsx6
-rw-r--r--src/client/apis/hypothesis/HypothesisApiUtils.ts25
2 files changed, 23 insertions, 8 deletions
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