1 | /*
|
2 | * Copyright 2021 Inrupt Inc.
|
3 | *
|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
5 | * of this software and associated documentation files (the "Software"), to deal in
|
6 | * the Software without restriction, including without limitation the rights to use,
|
7 | * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
8 | * Software, and to permit persons to whom the Software is furnished to do so,
|
9 | * subject to the following conditions:
|
10 | *
|
11 | * The above copyright notice and this permission notice shall be included in
|
12 | * all copies or substantial portions of the Software.
|
13 | *
|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
15 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
16 | * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
17 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
18 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
19 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20 | */
|
21 |
|
22 | import { Session } from "./Session";
|
23 |
|
24 | let defaultSession: Session;
|
25 |
|
26 | /**
|
27 | * Obtain the {@link Session} used when not explicitly instantiating one yourself.
|
28 | *
|
29 | * When using the top-level exports {@link fetch}, {@link login}, {@link logout},
|
30 | * {@link handleIncomingRedirect}, {@link onLogin} and {@link onLogout}, these apply to an
|
31 | * implicitly-instantiated {@link Session}.
|
32 | * This function returns a reference to that Session in order to obtain e.g. the current user's
|
33 | * WebID.
|
34 | * @since 1.3.0
|
35 | */
|
36 | export function getDefaultSession(): Session {
|
37 | if (typeof defaultSession === "undefined") {
|
38 | defaultSession = new Session();
|
39 | }
|
40 | return defaultSession;
|
41 | }
|
42 |
|
43 | /**
|
44 | * This function's signature is equal to `window.fetch`, but if the current user is authenticated
|
45 | * (see [[login]] and [[handleIncomingRedirect]), requests made using it will include that user's
|
46 | * credentials. If not, this will behave just like the regular `window.fetch`.
|
47 | *
|
48 | * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch}
|
49 | * @since 1.3.0
|
50 | */
|
51 | export const fetch: Session["fetch"] = (...args) => {
|
52 | const session = getDefaultSession();
|
53 | return session.fetch(...args);
|
54 | };
|
55 |
|
56 | /**
|
57 | * Triggers the login process. Note that this method will redirect the user away from your app.
|
58 | *
|
59 | * @param options Parameter to customize the login behaviour. In particular, two options are mandatory: `options.oidcIssuer`, the user's identity provider, and `options.redirectUrl`, the URL to which the user will be redirected after logging in their identity provider.
|
60 | * @returns This method should redirect the user away from the app: it does not return anything. The login process is completed by [[handleIncomingRedirect]].
|
61 | * @since 1.3.0
|
62 | */
|
63 | export const login: Session["login"] = (...args) => {
|
64 | const session = getDefaultSession();
|
65 | return session.login(...args);
|
66 | };
|
67 |
|
68 | /**
|
69 | * Logs the user out of the application. This does not log the user out of their
|
70 | * Solid identity provider, and should not redirect the user away.
|
71 | *
|
72 | * @since 1.3.0
|
73 | */
|
74 | export const logout: Session["logout"] = (...args) => {
|
75 | const session = getDefaultSession();
|
76 | return session.logout(...args);
|
77 | };
|
78 |
|
79 | /**
|
80 | * Completes the login process by processing the information provided by the Solid identity provider through redirect.
|
81 | *
|
82 | * @param url The URL of the page handling the redirect, including the query parameters — these contain the information to process the login.
|
83 | * @since 1.3.0
|
84 | */
|
85 | export const handleIncomingRedirect: Session["handleIncomingRedirect"] = (
|
86 | ...args
|
87 | ) => {
|
88 | const session = getDefaultSession();
|
89 | return session.handleIncomingRedirect(...args);
|
90 | };
|
91 |
|
92 | /**
|
93 | * Register a callback function to be called when a user completes login.
|
94 | *
|
95 | * The callback is called when {@link handleIncomingRedirect} completes successfully.
|
96 | * @since 1.3.0
|
97 | *
|
98 | * @param callback The function called when a user completes login.
|
99 | */
|
100 | export const onLogin: Session["onLogin"] = (...args) => {
|
101 | const session = getDefaultSession();
|
102 | return session.onLogin(...args);
|
103 | };
|
104 |
|
105 | /**
|
106 | * Register a callback function to be called when a user logs out:
|
107 | *
|
108 | * @param callback The function called when a user completes logout.
|
109 | * @since 1.3.0
|
110 | */
|
111 | export const onLogout: Session["onLogout"] = (...args) => {
|
112 | const session = getDefaultSession();
|
113 | return session.onLogout(...args);
|
114 | };
|