-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.ts
119 lines (95 loc) · 3.16 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { IPersonProfileData } from "./Interface";
import { handleIncomingRedirect, login, fetch, getDefaultSession, logout } from '@inrupt/solid-client-authn-browser'
import Router from 'next/router'
export function isSuccessfulStatusCode(statusCode:number) {
return Math.floor(statusCode / 100) === 2;
}
export async function createSolidContainer(url:string, name:string) {
console.log('WE are creatign container for the app');
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'text/turtle',
Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"',
Slug: name
}
});
console.log(response);
if (!isSuccessfulStatusCode(response.status))
throw new Error(
`Failed creating container at ${url}, returned status code ${response.status}`
);
}
// export async function readSolidDocument(url:string | string) {
// try {
// const response = await fetch(url, {
// headers: { Accept: 'text/turtle' }
// });
// if (!isSuccessfulStatusCode(response.status)) return null;
// const data = await response.text();
// console.log(data);
// const parser = new N3.Parser({ baseIRI: url });
// return parser.parse(data);
// } catch (error) {
// return null;
// }
// }
// export async function findUserStorage(url:string) :Promise<string>{
// url = url.replace(/#.*$/, '');
// url = url.endsWith('/') ? url + '../' : url + '/../';
// // url = new URL(url);
// const response = await fetch(url.href);
// if (
// response.headers.get(
// 'Link'?.includes('<http://www.w3.org/ns/pim/space#storage>; rel="type"')
// )
// )
// return url.href;
// if (url.pathname === '/') return url.href;
// return findUserStorage(url.href);
// }
export async function performLogin() {
const loginURLhref = getLogInURL();
if (!loginURLhref)
return null;
login({
oidcIssuer: loginURLhref,
redirectUrl: window.location.href,
clientName: 'Solid Period Tracker'
})
await handleIncomingRedirect({ restorePreviousSession: true });
const session = getDefaultSession();
console.log("Performed login, SESSION IS");
console.log(session.info.isLoggedIn);
}
function getLogInURL() {
const url = prompt("What is your Solid login URL? Example: https://timeasolidweb.solidweb.org/");
if (!url)
return null;
const loginURL = new URL(url);
loginURL.hash = '';
loginURL.pathname = '';
return loginURL.href;
}
export async function performLogout() {
await logout();
Router.reload();
}
// export const fetchUserProfile = async (
// webId: string
// ): Promise<IPersonProfileData> => {
// const profileQuads = await readSolidDocument(webId);
// const userName = profileQuads.find(
// (quad) => quad.predicate.value === 'http://xmlns.com/foaf/0.1/name'
// );
// const userStorage = profileQuads.find(
// (quad) =>
// quad.predicate.value === 'http://www.w3.org/ns/pim/space#storage'
// );
// return {
// webId: webId,
// name: userName?.object.value || 'Anonymous',
// storageUrl: userStorage?.object.value || (await findUserStorage(webId)),
// error
// };
// };