tiny-essentials 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/FUNDING.yml +4 -0
- package/.github/workflows/node.js.yml +31 -0
- package/LICENSE +674 -0
- package/README.md +67 -0
- package/dist/TinyEssentials.min.js +1 -0
- package/dist/_virtual/_commonjsHelpers.cjs +7 -0
- package/dist/_virtual/common.cjs +5 -0
- package/dist/_virtual/index.cjs +9 -0
- package/dist/_virtual/index2.cjs +5 -0
- package/dist/_virtual/trace.cjs +5 -0
- package/dist/legacy/crypto/decrypt.cjs +57 -0
- package/dist/legacy/crypto/decrypt.d.mts +18 -0
- package/dist/legacy/crypto/decrypt.mjs +47 -0
- package/dist/legacy/crypto/default.cjs +23 -0
- package/dist/legacy/crypto/default.d.mts +35 -0
- package/dist/legacy/crypto/default.mjs +19 -0
- package/dist/legacy/crypto/encrypt.cjs +54 -0
- package/dist/legacy/crypto/encrypt.d.mts +19 -0
- package/dist/legacy/crypto/encrypt.mjs +46 -0
- package/dist/legacy/crypto/index.cjs +11 -0
- package/dist/legacy/crypto/index.d.mts +4 -0
- package/dist/legacy/crypto/index.mjs +6 -0
- package/dist/legacy/firebase/cookieSession.cjs +121 -0
- package/dist/legacy/firebase/cookieSession.d.mts +56 -0
- package/dist/legacy/firebase/cookieSession.mjs +114 -0
- package/dist/legacy/firebase/database/index.cjs +11 -0
- package/dist/legacy/firebase/database/index.d.mts +7 -0
- package/dist/legacy/firebase/database/index.mjs +7 -0
- package/dist/legacy/firebase/database/presence.cjs +131 -0
- package/dist/legacy/firebase/database/presence.d.mts +9 -0
- package/dist/legacy/firebase/database/presence.mjs +111 -0
- package/dist/legacy/firebase/database/saveAsync.cjs +147 -0
- package/dist/legacy/firebase/database/saveAsync.d.mts +54 -0
- package/dist/legacy/firebase/database/saveAsync.mjs +147 -0
- package/dist/legacy/firebase/databaseEscape.cjs +42 -0
- package/dist/legacy/firebase/databaseEscape.d.mts +11 -0
- package/dist/legacy/firebase/databaseEscape.mjs +37 -0
- package/dist/legacy/firebase/databaseLogger.cjs +258 -0
- package/dist/legacy/firebase/databaseLogger.d.mts +7 -0
- package/dist/legacy/firebase/databaseLogger.mjs +234 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.cjs +71 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.d.mts +38 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.mjs +64 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.cjs +47 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.d.mts +17 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.mjs +43 -0
- package/dist/legacy/firebase/discord/api/getToken.cjs +67 -0
- package/dist/legacy/firebase/discord/api/getToken.d.mts +35 -0
- package/dist/legacy/firebase/discord/api/getToken.mjs +63 -0
- package/dist/legacy/firebase/discord/api/getUser.cjs +58 -0
- package/dist/legacy/firebase/discord/api/getUser.d.mts +27 -0
- package/dist/legacy/firebase/discord/api/getUser.mjs +54 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.cjs +57 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.d.mts +26 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.mjs +53 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.cjs +56 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.d.mts +25 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.mjs +52 -0
- package/dist/legacy/firebase/discord/api/index.cjs +24 -0
- package/dist/legacy/firebase/discord/api/index.d.mts +19 -0
- package/dist/legacy/firebase/discord/api/index.mjs +20 -0
- package/dist/legacy/firebase/discord/api/refreshToken.cjs +77 -0
- package/dist/legacy/firebase/discord/api/refreshToken.d.mts +45 -0
- package/dist/legacy/firebase/discord/api/refreshToken.mjs +73 -0
- package/dist/legacy/firebase/discord/api/revokeToken.cjs +61 -0
- package/dist/legacy/firebase/discord/api/revokeToken.d.mts +29 -0
- package/dist/legacy/firebase/discord/api/revokeToken.mjs +58 -0
- package/dist/legacy/firebase/discord/config.cjs +21 -0
- package/dist/legacy/firebase/discord/config.d.mts +27 -0
- package/dist/legacy/firebase/discord/config.mjs +17 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.cjs +11 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.d.mts +7 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.mjs +7 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.cjs +110 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.d.mts +4 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.mjs +107 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.cjs +100 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.d.mts +4 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.mjs +97 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.cjs +76 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.d.mts +23 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.mjs +67 -0
- package/dist/legacy/firebase/discord/get/cookie-session.cjs +32 -0
- package/dist/legacy/firebase/discord/get/cookie-session.d.mts +20 -0
- package/dist/legacy/firebase/discord/get/cookie-session.mjs +28 -0
- package/dist/legacy/firebase/discord/get/credentials.cjs +24 -0
- package/dist/legacy/firebase/discord/get/credentials.d.mts +19 -0
- package/dist/legacy/firebase/discord/get/credentials.mjs +20 -0
- package/dist/legacy/firebase/discord/get/errorValidator.cjs +60 -0
- package/dist/legacy/firebase/discord/get/errorValidator.d.mts +28 -0
- package/dist/legacy/firebase/discord/get/errorValidator.mjs +55 -0
- package/dist/legacy/firebase/discord/get/index.cjs +17 -0
- package/dist/legacy/firebase/discord/get/index.d.mts +13 -0
- package/dist/legacy/firebase/discord/get/index.mjs +13 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.cjs +37 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.d.mts +22 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.mjs +30 -0
- package/dist/legacy/firebase/discord/http/index.cjs +15 -0
- package/dist/legacy/firebase/discord/http/index.d.mts +11 -0
- package/dist/legacy/firebase/discord/http/index.mjs +11 -0
- package/dist/legacy/firebase/discord/http/login.cjs +127 -0
- package/dist/legacy/firebase/discord/http/login.d.mts +21 -0
- package/dist/legacy/firebase/discord/http/login.mjs +123 -0
- package/dist/legacy/firebase/discord/http/logout.cjs +155 -0
- package/dist/legacy/firebase/discord/http/logout.d.mts +22 -0
- package/dist/legacy/firebase/discord/http/logout.mjs +143 -0
- package/dist/legacy/firebase/discord/http/redirect.cjs +188 -0
- package/dist/legacy/firebase/discord/http/redirect.d.mts +24 -0
- package/dist/legacy/firebase/discord/http/redirect.mjs +178 -0
- package/dist/legacy/firebase/discord/http/refreshToken.cjs +133 -0
- package/dist/legacy/firebase/discord/http/refreshToken.d.mts +24 -0
- package/dist/legacy/firebase/discord/http/refreshToken.mjs +121 -0
- package/dist/legacy/firebase/discord/index.cjs +19 -0
- package/dist/legacy/firebase/discord/index.d.mts +15 -0
- package/dist/legacy/firebase/discord/index.mjs +15 -0
- package/dist/legacy/firebase/discord/template/cookie-session.cjs +1430 -0
- package/dist/legacy/firebase/discord/template/cookie-session.d.mts +22 -0
- package/dist/legacy/firebase/discord/template/cookie-session.mjs +1159 -0
- package/dist/legacy/firebase/domainRedirect.cjs +35 -0
- package/dist/legacy/firebase/domainRedirect.d.mts +12 -0
- package/dist/legacy/firebase/domainRedirect.mjs +27 -0
- package/dist/legacy/firebase/escape.cjs +75 -0
- package/dist/legacy/firebase/escape.d.mts +17 -0
- package/dist/legacy/firebase/escape.mjs +58 -0
- package/dist/legacy/firebase/getDBAsync.cjs +33 -0
- package/dist/legacy/firebase/getDBAsync.d.mts +10 -0
- package/dist/legacy/firebase/getDBAsync.mjs +26 -0
- package/dist/legacy/firebase/getDBData.cjs +21 -0
- package/dist/legacy/firebase/getDBData.d.mts +1 -0
- package/dist/legacy/firebase/getDBData.mjs +17 -0
- package/dist/legacy/firebase/getDBValue.cjs +17 -0
- package/dist/legacy/firebase/getDBValue.d.mts +9 -0
- package/dist/legacy/firebase/getDBValue.mjs +14 -0
- package/dist/legacy/firebase/index.cjs +261 -0
- package/dist/legacy/firebase/index.d.mts +51 -0
- package/dist/legacy/firebase/index.mjs +234 -0
- package/dist/legacy/firebase/isEmulator.cjs +14 -0
- package/dist/legacy/firebase/isEmulator.d.mts +8 -0
- package/dist/legacy/firebase/isEmulator.mjs +10 -0
- package/dist/legacy/firebase/logger.cjs +137 -0
- package/dist/legacy/firebase/logger.d.mts +7 -0
- package/dist/legacy/firebase/logger.mjs +125 -0
- package/dist/legacy/firebase/mySQL.cjs +26 -0
- package/dist/legacy/firebase/mySQL.d.mts +10 -0
- package/dist/legacy/firebase/mySQL.mjs +22 -0
- package/dist/legacy/firebase/mysqlConnector/create.cjs +119 -0
- package/dist/legacy/firebase/mysqlConnector/create.d.mts +36 -0
- package/dist/legacy/firebase/mysqlConnector/create.mjs +109 -0
- package/dist/legacy/firebase/mysqlConnector/index.cjs +23 -0
- package/dist/legacy/firebase/mysqlConnector/index.d.mts +11 -0
- package/dist/legacy/firebase/mysqlConnector/index.mjs +16 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.cjs +75 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.d.mts +31 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.mjs +63 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.cjs +91 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.d.mts +33 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.mjs +87 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.cjs +28 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.d.mts +12 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.mjs +23 -0
- package/dist/legacy/firebase/transactionDBAsync.cjs +33 -0
- package/dist/legacy/firebase/transactionDBAsync.d.mts +11 -0
- package/dist/legacy/firebase/transactionDBAsync.mjs +27 -0
- package/dist/legacy/get/countObj.cjs +24 -0
- package/dist/legacy/get/countObj.d.mts +11 -0
- package/dist/legacy/get/countObj.mjs +21 -0
- package/dist/legacy/get/decimalColor.cjs +60 -0
- package/dist/legacy/get/decimalColor.d.mts +12 -0
- package/dist/legacy/get/decimalColor.mjs +54 -0
- package/dist/legacy/get/objType.cjs +43 -0
- package/dist/legacy/get/objType.d.mts +19 -0
- package/dist/legacy/get/objType.mjs +40 -0
- package/dist/legacy/get/pagination.cjs +100 -0
- package/dist/legacy/get/pagination.d.mts +25 -0
- package/dist/legacy/get/pagination.mjs +91 -0
- package/dist/legacy/get/queryUrlByName.cjs +31 -0
- package/dist/legacy/get/queryUrlByName.d.mts +20 -0
- package/dist/legacy/get/queryUrlByName.mjs +28 -0
- package/dist/legacy/get/queryUrlJSON.cjs +54 -0
- package/dist/legacy/get/queryUrlJSON.d.mts +23 -0
- package/dist/legacy/get/queryUrlJSON.mjs +57 -0
- package/dist/legacy/get/super_string_filter.cjs +97 -0
- package/dist/legacy/get/super_string_filter.d.mts +25 -0
- package/dist/legacy/get/super_string_filter.mjs +83 -0
- package/dist/legacy/get/versionCheck.cjs +49 -0
- package/dist/legacy/get/versionCheck.d.mts +17 -0
- package/dist/legacy/get/versionCheck.mjs +38 -0
- package/dist/legacy/http/HTTP-1.0.cjs +151 -0
- package/dist/legacy/http/HTTP-1.0.d.mts +8 -0
- package/dist/legacy/http/HTTP-1.0.mjs +136 -0
- package/dist/legacy/http/auth.cjs +70 -0
- package/dist/legacy/http/auth.d.mts +41 -0
- package/dist/legacy/http/auth.mjs +63 -0
- package/dist/legacy/http/check_domain.cjs +84 -0
- package/dist/legacy/http/check_domain.d.mts +10 -0
- package/dist/legacy/http/check_domain.mjs +82 -0
- package/dist/legacy/http/csrfTokenAnalyze.cjs +46 -0
- package/dist/legacy/http/csrfTokenAnalyze.d.mts +21 -0
- package/dist/legacy/http/csrfTokenAnalyze.mjs +40 -0
- package/dist/legacy/http/domainValidator.cjs +98 -0
- package/dist/legacy/http/domainValidator.d.mts +30 -0
- package/dist/legacy/http/domainValidator.mjs +82 -0
- package/dist/legacy/http/errorsCallback.cjs +55 -0
- package/dist/legacy/http/errorsCallback.d.mts +29 -0
- package/dist/legacy/http/errorsCallback.mjs +48 -0
- package/dist/legacy/http/fetch/json.cjs +48 -0
- package/dist/legacy/http/fetch/json.d.mts +22 -0
- package/dist/legacy/http/fetch/json.mjs +41 -0
- package/dist/legacy/http/fetch/text.cjs +47 -0
- package/dist/legacy/http/fetch/text.d.mts +21 -0
- package/dist/legacy/http/fetch/text.mjs +40 -0
- package/dist/legacy/http/fileCache.cjs +79 -0
- package/dist/legacy/http/fileCache.d.mts +35 -0
- package/dist/legacy/http/fileCache.mjs +70 -0
- package/dist/legacy/http/getDomainURL.cjs +59 -0
- package/dist/legacy/http/getDomainURL.d.mts +18 -0
- package/dist/legacy/http/getDomainURL.mjs +54 -0
- package/dist/legacy/http/userIP.cjs +59 -0
- package/dist/legacy/http/userIP.d.mts +21 -0
- package/dist/legacy/http/userIP.mjs +48 -0
- package/dist/legacy/index.cjs +122 -0
- package/dist/legacy/index.d.mts +89 -0
- package/dist/legacy/index.mjs +92 -0
- package/dist/legacy/libs/arraySortPositions.cjs +30 -0
- package/dist/legacy/libs/arraySortPositions.d.mts +16 -0
- package/dist/legacy/libs/arraySortPositions.mjs +27 -0
- package/dist/legacy/libs/capitalize.cjs +21 -0
- package/dist/legacy/libs/capitalize.d.mts +13 -0
- package/dist/legacy/libs/capitalize.mjs +17 -0
- package/dist/legacy/libs/convertBytes.cjs +64 -0
- package/dist/legacy/libs/convertBytes.d.mts +17 -0
- package/dist/legacy/libs/convertBytes.mjs +55 -0
- package/dist/legacy/libs/custom_module_loader.cjs +82 -0
- package/dist/legacy/libs/custom_module_loader.d.mts +5 -0
- package/dist/legacy/libs/custom_module_loader.mjs +79 -0
- package/dist/legacy/libs/dice.cjs +35 -0
- package/dist/legacy/libs/dice.d.mts +5 -0
- package/dist/legacy/libs/dice.mjs +31 -0
- package/dist/legacy/libs/markdown.cjs +85 -0
- package/dist/legacy/libs/markdown.d.mts +75 -0
- package/dist/legacy/libs/markdown.mjs +78 -0
- package/dist/legacy/libs/percentage.cjs +22 -0
- package/dist/legacy/libs/percentage.d.mts +4 -0
- package/dist/legacy/libs/percentage.mjs +19 -0
- package/dist/legacy/libs/regex/getLetter.cjs +22 -0
- package/dist/legacy/libs/regex/getLetter.d.mts +12 -0
- package/dist/legacy/libs/regex/getLetter.mjs +19 -0
- package/dist/legacy/libs/replaceAsync.cjs +32 -0
- package/dist/legacy/libs/replaceAsync.d.mts +15 -0
- package/dist/legacy/libs/replaceAsync.mjs +25 -0
- package/dist/legacy/libs/rule3.cjs +58 -0
- package/dist/legacy/libs/rule3.d.mts +5 -0
- package/dist/legacy/libs/rule3.mjs +54 -0
- package/dist/legacy/libs/userLevel.cjs +134 -0
- package/dist/legacy/libs/userLevel.d.mts +69 -0
- package/dist/legacy/libs/userLevel.mjs +123 -0
- package/dist/legacy/momentjs/getAge.cjs +28 -0
- package/dist/legacy/momentjs/getAge.d.mts +8 -0
- package/dist/legacy/momentjs/getAge.mjs +22 -0
- package/dist/legacy/momentjs/index.cjs +9 -0
- package/dist/legacy/momentjs/index.d.mts +3 -0
- package/dist/legacy/momentjs/index.mjs +3 -0
- package/dist/legacy/momentjs/timeDuration.cjs +29 -0
- package/dist/legacy/momentjs/timeDuration.d.mts +9 -0
- package/dist/legacy/momentjs/timeDuration.mjs +23 -0
- package/dist/legacy/socket.io/antiFlood/index.cjs +9 -0
- package/dist/legacy/socket.io/antiFlood/index.d.mts +3 -0
- package/dist/legacy/socket.io/antiFlood/index.mjs +3 -0
- package/dist/legacy/socket.io/antiFlood/install.cjs +32 -0
- package/dist/legacy/socket.io/antiFlood/install.d.mts +14 -0
- package/dist/legacy/socket.io/antiFlood/install.mjs +27 -0
- package/dist/legacy/socket.io/antiFlood/verify.cjs +87 -0
- package/dist/legacy/socket.io/antiFlood/verify.d.mts +15 -0
- package/dist/legacy/socket.io/antiFlood/verify.mjs +73 -0
- package/dist/legacy/socket.io/cookie-session.cjs +25 -0
- package/dist/legacy/socket.io/cookie-session.d.mts +9 -0
- package/dist/legacy/socket.io/cookie-session.mjs +20 -0
- package/dist/legacy/socket.io/discord.cjs +61 -0
- package/dist/legacy/socket.io/discord.d.mts +10 -0
- package/dist/legacy/socket.io/discord.mjs +52 -0
- package/dist/legacy/socket.io/index.cjs +13 -0
- package/dist/legacy/socket.io/index.d.mts +9 -0
- package/dist/legacy/socket.io/index.mjs +9 -0
- package/dist/v1/basics/array.cjs +31 -0
- package/dist/v1/basics/array.d.mts +10 -0
- package/dist/v1/basics/array.mjs +22 -0
- package/dist/v1/basics/clock.cjs +187 -0
- package/dist/v1/basics/clock.d.mts +37 -0
- package/dist/v1/basics/clock.mjs +152 -0
- package/dist/v1/basics/objFilter.cjs +55 -0
- package/dist/v1/basics/objFilter.d.mts +27 -0
- package/dist/v1/basics/objFilter.mjs +52 -0
- package/dist/v1/basics/simpleMath.cjs +88 -0
- package/dist/v1/basics/simpleMath.d.mts +54 -0
- package/dist/v1/basics/simpleMath.mjs +75 -0
- package/dist/v1/basics/text.cjs +34 -0
- package/dist/v1/basics/text.d.mts +20 -0
- package/dist/v1/basics/text.mjs +25 -0
- package/dist/v1/index.cjs +26 -0
- package/dist/v1/index.d.mts +15 -0
- package/dist/v1/index.mjs +8 -0
- package/package.json +72 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import getJsonFetch from '../../../http/fetch/json.mjs';
|
|
2
|
+
import config from '../config.mjs';
|
|
3
|
+
import errorValidator from '../get/errorValidator.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* Refreshes the Discord OAuth2 access token using a valid refresh token.
|
|
6
|
+
*
|
|
7
|
+
* This function makes a `POST` request to the Discord API's `/oauth2/token` endpoint
|
|
8
|
+
* to obtain a new access token and refresh token. It uses the `refresh_token` grant type.
|
|
9
|
+
*
|
|
10
|
+
* @param {Object} dsData - The Discord OAuth2 data required for token refresh.
|
|
11
|
+
* @param {string} dsData.client_id - Your application's client ID.
|
|
12
|
+
* @param {string} dsData.client_secret - Your application's client secret.
|
|
13
|
+
* @param {string} dsData.code - The original authorization code (optional but may be required by some flows).
|
|
14
|
+
* @param {string} dsData.refresh_token - The current valid refresh token.
|
|
15
|
+
* @param {string} dsData.redirect_uri - The redirect URI used in the original OAuth2 flow.
|
|
16
|
+
* @param {string} dsData.scope - The scope(s) to request (e.g., 'identify guilds').
|
|
17
|
+
*
|
|
18
|
+
* @returns {Promise<Object>} Resolves with a new token object from the Discord API:
|
|
19
|
+
* {
|
|
20
|
+
* access_token: string,
|
|
21
|
+
* token_type: string,
|
|
22
|
+
* expires_in: number,
|
|
23
|
+
* refresh_token: string,
|
|
24
|
+
* scope: string
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* @throws {Object} If the request fails, rejects with an error object containing `code` and `message`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* refreshToken({
|
|
31
|
+
* client_id: '1234567890',
|
|
32
|
+
* client_secret: 'mySecret',
|
|
33
|
+
* code: 'optional-code',
|
|
34
|
+
* refresh_token: 'old-refresh-token',
|
|
35
|
+
* redirect_uri: 'https://myapp.com/callback',
|
|
36
|
+
* scope: 'identify guilds'
|
|
37
|
+
* })
|
|
38
|
+
* .then(newTokens => console.log('New Access Token:', newTokens.access_token))
|
|
39
|
+
* .catch(err => console.error('Failed to refresh token:', err));
|
|
40
|
+
*/
|
|
41
|
+
export default function refreshToken(dsData) {
|
|
42
|
+
return new Promise(function (resolve, reject) {
|
|
43
|
+
// API URL
|
|
44
|
+
const apiURL = config.url;
|
|
45
|
+
// Response
|
|
46
|
+
getJsonFetch(`${apiURL}oauth2/token`, {
|
|
47
|
+
method: 'POST',
|
|
48
|
+
body: new URLSearchParams({
|
|
49
|
+
client_id: dsData.client_id,
|
|
50
|
+
client_secret: dsData.client_secret,
|
|
51
|
+
grant_type: 'refresh_token',
|
|
52
|
+
code: dsData.code,
|
|
53
|
+
refresh_token: dsData.refresh_token,
|
|
54
|
+
redirect_uri: dsData.redirect_uri,
|
|
55
|
+
scope: dsData.scope,
|
|
56
|
+
}),
|
|
57
|
+
headers: {
|
|
58
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
.then((data) => {
|
|
62
|
+
// Error Validator
|
|
63
|
+
const result = errorValidator(data);
|
|
64
|
+
if (!result.error)
|
|
65
|
+
resolve(result.data);
|
|
66
|
+
else
|
|
67
|
+
reject(result.error);
|
|
68
|
+
})
|
|
69
|
+
.catch((err) => {
|
|
70
|
+
reject({ code: err.response.status, message: err.message });
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var json = require('../../../http/fetch/json.cjs');
|
|
4
|
+
var config = require('../config.cjs');
|
|
5
|
+
var errorValidator = require('../get/errorValidator.cjs');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Revokes an OAuth2 access token from the Discord API.
|
|
9
|
+
*
|
|
10
|
+
* This function makes a `POST` request to the `/oauth2/token/revoke` endpoint to invalidate
|
|
11
|
+
* a user's access token. This is useful for logout or unlink flows, and requires HTTP Basic Auth
|
|
12
|
+
* using the application's client ID and client secret encoded in base64.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} access_token - The Discord OAuth2 access token to be revoked.
|
|
15
|
+
* @param {Object} tinyAuth - An object with the client_id and client_secret used to generate the Basic Auth credentials.
|
|
16
|
+
* @param {string} tinyAuth.client_id - The Discord application client ID.
|
|
17
|
+
* @param {string} tinyAuth.client_secret - The Discord application client secret.
|
|
18
|
+
*
|
|
19
|
+
* @returns {Promise<Object>} Resolves when the token is successfully revoked.
|
|
20
|
+
* The response may not contain any body if successful, so `result.data` may be empty.
|
|
21
|
+
*
|
|
22
|
+
* @throws {Object} If the request fails, rejects with an error object containing `code` and `message`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* revokeToken('user-access-token', {
|
|
26
|
+
* client_id: '1234567890',
|
|
27
|
+
* client_secret: 'superSecret'
|
|
28
|
+
* })
|
|
29
|
+
* .then(() => console.log('Token revoked'))
|
|
30
|
+
* .catch(err => console.error('Failed to revoke token:', err));
|
|
31
|
+
*/
|
|
32
|
+
function revokeToken(access_token, tinyAuth) {
|
|
33
|
+
return new Promise(function (resolve, reject) {
|
|
34
|
+
// API URL
|
|
35
|
+
const apiURL = config.url;
|
|
36
|
+
const credentials = credentials(tinyAuth);
|
|
37
|
+
|
|
38
|
+
// Response
|
|
39
|
+
json(`${apiURL}oauth2/token/revoke`, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
body: new URLSearchParams({
|
|
42
|
+
token: access_token,
|
|
43
|
+
}),
|
|
44
|
+
headers: {
|
|
45
|
+
Authorization: `Basic ${credentials}`,
|
|
46
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
.then((data) => {
|
|
50
|
+
// Error Validator
|
|
51
|
+
const result = errorValidator(data);
|
|
52
|
+
if (!result.error) resolve(result.data);
|
|
53
|
+
else reject(result.error);
|
|
54
|
+
})
|
|
55
|
+
.catch((err) => {
|
|
56
|
+
reject({ code: err.response.status, message: err.message });
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = revokeToken;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Revokes an OAuth2 access token from the Discord API.
|
|
3
|
+
*
|
|
4
|
+
* This function makes a `POST` request to the `/oauth2/token/revoke` endpoint to invalidate
|
|
5
|
+
* a user's access token. This is useful for logout or unlink flows, and requires HTTP Basic Auth
|
|
6
|
+
* using the application's client ID and client secret encoded in base64.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} access_token - The Discord OAuth2 access token to be revoked.
|
|
9
|
+
* @param {Object} tinyAuth - An object with the client_id and client_secret used to generate the Basic Auth credentials.
|
|
10
|
+
* @param {string} tinyAuth.client_id - The Discord application client ID.
|
|
11
|
+
* @param {string} tinyAuth.client_secret - The Discord application client secret.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Promise<Object>} Resolves when the token is successfully revoked.
|
|
14
|
+
* The response may not contain any body if successful, so `result.data` may be empty.
|
|
15
|
+
*
|
|
16
|
+
* @throws {Object} If the request fails, rejects with an error object containing `code` and `message`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* revokeToken('user-access-token', {
|
|
20
|
+
* client_id: '1234567890',
|
|
21
|
+
* client_secret: 'superSecret'
|
|
22
|
+
* })
|
|
23
|
+
* .then(() => console.log('Token revoked'))
|
|
24
|
+
* .catch(err => console.error('Failed to revoke token:', err));
|
|
25
|
+
*/
|
|
26
|
+
export default function revokeToken(access_token: string, tinyAuth: {
|
|
27
|
+
client_id: string;
|
|
28
|
+
client_secret: string;
|
|
29
|
+
}): Promise<Object>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import getJsonFetch from '../../../http/fetch/json.mjs';
|
|
2
|
+
import config from '../config.mjs';
|
|
3
|
+
import credentials from '../get/credentials.mjs';
|
|
4
|
+
import errorValidator from '../get/errorValidator.mjs';
|
|
5
|
+
/**
|
|
6
|
+
* Revokes an OAuth2 access token from the Discord API.
|
|
7
|
+
*
|
|
8
|
+
* This function makes a `POST` request to the `/oauth2/token/revoke` endpoint to invalidate
|
|
9
|
+
* a user's access token. This is useful for logout or unlink flows, and requires HTTP Basic Auth
|
|
10
|
+
* using the application's client ID and client secret encoded in base64.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} access_token - The Discord OAuth2 access token to be revoked.
|
|
13
|
+
* @param {Object} tinyAuth - An object with the client_id and client_secret used to generate the Basic Auth credentials.
|
|
14
|
+
* @param {string} tinyAuth.client_id - The Discord application client ID.
|
|
15
|
+
* @param {string} tinyAuth.client_secret - The Discord application client secret.
|
|
16
|
+
*
|
|
17
|
+
* @returns {Promise<Object>} Resolves when the token is successfully revoked.
|
|
18
|
+
* The response may not contain any body if successful, so `result.data` may be empty.
|
|
19
|
+
*
|
|
20
|
+
* @throws {Object} If the request fails, rejects with an error object containing `code` and `message`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* revokeToken('user-access-token', {
|
|
24
|
+
* client_id: '1234567890',
|
|
25
|
+
* client_secret: 'superSecret'
|
|
26
|
+
* })
|
|
27
|
+
* .then(() => console.log('Token revoked'))
|
|
28
|
+
* .catch(err => console.error('Failed to revoke token:', err));
|
|
29
|
+
*/
|
|
30
|
+
export default function revokeToken(access_token, tinyAuth) {
|
|
31
|
+
return new Promise(function (resolve, reject) {
|
|
32
|
+
// API URL
|
|
33
|
+
const apiURL = config.url;
|
|
34
|
+
const credentials = credentials(tinyAuth);
|
|
35
|
+
// Response
|
|
36
|
+
getJsonFetch(`${apiURL}oauth2/token/revoke`, {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
body: new URLSearchParams({
|
|
39
|
+
token: access_token,
|
|
40
|
+
}),
|
|
41
|
+
headers: {
|
|
42
|
+
Authorization: `Basic ${credentials}`,
|
|
43
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
.then((data) => {
|
|
47
|
+
// Error Validator
|
|
48
|
+
const result = errorValidator(data);
|
|
49
|
+
if (!result.error)
|
|
50
|
+
resolve(result.data);
|
|
51
|
+
else
|
|
52
|
+
reject(result.error);
|
|
53
|
+
})
|
|
54
|
+
.catch((err) => {
|
|
55
|
+
reject({ code: err.response.status, message: err.message });
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration object containing Discord API and OAuth2 URLs.
|
|
5
|
+
*
|
|
6
|
+
* @typedef {Object} Config
|
|
7
|
+
* @property {string} url - The base URL for the Discord API.
|
|
8
|
+
* @property {string} oauth2 - The OAuth2 authorization URL for Discord.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The configuration object for the Discord API and OAuth2.
|
|
13
|
+
*
|
|
14
|
+
* @type {Config}
|
|
15
|
+
*/
|
|
16
|
+
const config = {
|
|
17
|
+
url: 'https://discord.com/api/',
|
|
18
|
+
oauth2: 'https://discord.com/oauth2/authorize',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports = config;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default config;
|
|
2
|
+
/**
|
|
3
|
+
* Configuration object containing Discord API and OAuth2 URLs.
|
|
4
|
+
*/
|
|
5
|
+
export type Config = {
|
|
6
|
+
/**
|
|
7
|
+
* - The base URL for the Discord API.
|
|
8
|
+
*/
|
|
9
|
+
url: string;
|
|
10
|
+
/**
|
|
11
|
+
* - The OAuth2 authorization URL for Discord.
|
|
12
|
+
*/
|
|
13
|
+
oauth2: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Configuration object containing Discord API and OAuth2 URLs.
|
|
17
|
+
*
|
|
18
|
+
* @typedef {Object} Config
|
|
19
|
+
* @property {string} url - The base URL for the Discord API.
|
|
20
|
+
* @property {string} oauth2 - The OAuth2 authorization URL for Discord.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* The configuration object for the Discord API and OAuth2.
|
|
24
|
+
*
|
|
25
|
+
* @type {Config}
|
|
26
|
+
*/
|
|
27
|
+
declare const config: Config;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration object containing Discord API and OAuth2 URLs.
|
|
3
|
+
*
|
|
4
|
+
* @typedef {Object} Config
|
|
5
|
+
* @property {string} url - The base URL for the Discord API.
|
|
6
|
+
* @property {string} oauth2 - The OAuth2 authorization URL for Discord.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The configuration object for the Discord API and OAuth2.
|
|
10
|
+
*
|
|
11
|
+
* @type {Config}
|
|
12
|
+
*/
|
|
13
|
+
const config = {
|
|
14
|
+
url: 'https://discord.com/api/',
|
|
15
|
+
oauth2: 'https://discord.com/oauth2/authorize',
|
|
16
|
+
};
|
|
17
|
+
export default config;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Handles Firebase authentication using a custom token, CSRF protection,
|
|
5
|
+
* and final redirection logic after login.
|
|
6
|
+
*
|
|
7
|
+
* This function signs in the user with a custom Firebase token, waits for
|
|
8
|
+
* the auth state to change, retrieves the ID token, sends it to the current
|
|
9
|
+
* server endpoint along with a CSRF token, and then redirects the user to
|
|
10
|
+
* the specified URL or executes a callback.
|
|
11
|
+
*
|
|
12
|
+
* @namespace login
|
|
13
|
+
*/
|
|
14
|
+
const login = {
|
|
15
|
+
/**
|
|
16
|
+
* Executes the login process with Firebase Auth and handles redirection.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} token - The Firebase custom token used for authentication.
|
|
19
|
+
* @param {string} redirect_url - The relative URL to redirect the user after login.
|
|
20
|
+
* If it starts with "/", it will be adjusted.
|
|
21
|
+
* @param {string} csrfToken - A CSRF protection token to be sent to the backend.
|
|
22
|
+
* @param {function} [callback] - Optional callback to be called after authentication.
|
|
23
|
+
* Signature: `(err, redirectFunction, user)`
|
|
24
|
+
* - If omitted, default redirection is used.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* login.run(firebaseToken, '/dashboard', csrfToken);
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* login.run(firebaseToken, '/dashboard', csrfToken, (err, redirect, user) => {
|
|
31
|
+
* if (err) {
|
|
32
|
+
* console.error('Login failed:', err);
|
|
33
|
+
* return;
|
|
34
|
+
* }
|
|
35
|
+
* console.log('Logged in as', user.displayName);
|
|
36
|
+
* redirect(); // manually trigger the redirect
|
|
37
|
+
* });
|
|
38
|
+
*/
|
|
39
|
+
run: function (token, redirect_url, csrfToken, callback) {
|
|
40
|
+
// Fix Redirect
|
|
41
|
+
if (typeof redirect_url === 'string') {
|
|
42
|
+
if (redirect_url.startsWith('/')) redirect_url = redirect_url.substring(1);
|
|
43
|
+
else if (redirect_url.startsWith(window.location.origin))
|
|
44
|
+
redirect_url = redirect_url.substring(window.location.origin.length + 1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Nope
|
|
48
|
+
else redirect_url = '';
|
|
49
|
+
|
|
50
|
+
// Prepare Redirect
|
|
51
|
+
const final_redirect = function (err, user) {
|
|
52
|
+
// The Redirect
|
|
53
|
+
const start_redirect = function () {
|
|
54
|
+
window.location.href = window.location.origin + '/' + redirect_url;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Default Redirect
|
|
58
|
+
if (typeof callback !== 'function') {
|
|
59
|
+
// Show Error
|
|
60
|
+
if (err) alert(err.message);
|
|
61
|
+
// Redirect Now
|
|
62
|
+
start_redirect();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Custom Redirect
|
|
66
|
+
else callback(err, start_redirect, user);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Firebase Auth
|
|
70
|
+
firebase.auth().onAuthStateChanged(function (user) {
|
|
71
|
+
if (user) {
|
|
72
|
+
user
|
|
73
|
+
.getIdToken()
|
|
74
|
+
.then(function (idToken) {
|
|
75
|
+
// Fetch
|
|
76
|
+
fetch(window.location.pathname, {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: {
|
|
79
|
+
Accept: 'application/json',
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({ token: idToken, csrfToken: csrfToken }),
|
|
83
|
+
})
|
|
84
|
+
.then((response) => {
|
|
85
|
+
response
|
|
86
|
+
.json()
|
|
87
|
+
.then((data) => {
|
|
88
|
+
// Show Error Message
|
|
89
|
+
if (!data.success) final_redirect(new Error(data.error));
|
|
90
|
+
// Complete
|
|
91
|
+
else final_redirect(null, user);
|
|
92
|
+
})
|
|
93
|
+
.catch(final_redirect);
|
|
94
|
+
})
|
|
95
|
+
.catch(final_redirect);
|
|
96
|
+
})
|
|
97
|
+
.catch(final_redirect);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Sign In
|
|
102
|
+
firebase
|
|
103
|
+
.auth()
|
|
104
|
+
.signInWithCustomToken(token)
|
|
105
|
+
// Fail
|
|
106
|
+
.catch(final_redirect);
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
module.exports = login;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles Firebase authentication using a custom token, CSRF protection,
|
|
3
|
+
* and final redirection logic after login.
|
|
4
|
+
*
|
|
5
|
+
* This function signs in the user with a custom Firebase token, waits for
|
|
6
|
+
* the auth state to change, retrieves the ID token, sends it to the current
|
|
7
|
+
* server endpoint along with a CSRF token, and then redirects the user to
|
|
8
|
+
* the specified URL or executes a callback.
|
|
9
|
+
*
|
|
10
|
+
* @namespace login
|
|
11
|
+
*/
|
|
12
|
+
const login = {
|
|
13
|
+
/**
|
|
14
|
+
* Executes the login process with Firebase Auth and handles redirection.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} token - The Firebase custom token used for authentication.
|
|
17
|
+
* @param {string} redirect_url - The relative URL to redirect the user after login.
|
|
18
|
+
* If it starts with "/", it will be adjusted.
|
|
19
|
+
* @param {string} csrfToken - A CSRF protection token to be sent to the backend.
|
|
20
|
+
* @param {function} [callback] - Optional callback to be called after authentication.
|
|
21
|
+
* Signature: `(err, redirectFunction, user)`
|
|
22
|
+
* - If omitted, default redirection is used.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* login.run(firebaseToken, '/dashboard', csrfToken);
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* login.run(firebaseToken, '/dashboard', csrfToken, (err, redirect, user) => {
|
|
29
|
+
* if (err) {
|
|
30
|
+
* console.error('Login failed:', err);
|
|
31
|
+
* return;
|
|
32
|
+
* }
|
|
33
|
+
* console.log('Logged in as', user.displayName);
|
|
34
|
+
* redirect(); // manually trigger the redirect
|
|
35
|
+
* });
|
|
36
|
+
*/
|
|
37
|
+
run: function (token, redirect_url, csrfToken, callback) {
|
|
38
|
+
// Fix Redirect
|
|
39
|
+
if (typeof redirect_url === 'string') {
|
|
40
|
+
if (redirect_url.startsWith('/'))
|
|
41
|
+
redirect_url = redirect_url.substring(1);
|
|
42
|
+
else if (redirect_url.startsWith(window.location.origin))
|
|
43
|
+
redirect_url = redirect_url.substring(window.location.origin.length + 1);
|
|
44
|
+
}
|
|
45
|
+
// Nope
|
|
46
|
+
else
|
|
47
|
+
redirect_url = '';
|
|
48
|
+
// Prepare Redirect
|
|
49
|
+
const final_redirect = function (err, user) {
|
|
50
|
+
// The Redirect
|
|
51
|
+
const start_redirect = function () {
|
|
52
|
+
window.location.href = window.location.origin + '/' + redirect_url;
|
|
53
|
+
};
|
|
54
|
+
// Default Redirect
|
|
55
|
+
if (typeof callback !== 'function') {
|
|
56
|
+
// Show Error
|
|
57
|
+
if (err)
|
|
58
|
+
alert(err.message);
|
|
59
|
+
// Redirect Now
|
|
60
|
+
start_redirect();
|
|
61
|
+
}
|
|
62
|
+
// Custom Redirect
|
|
63
|
+
else
|
|
64
|
+
callback(err, start_redirect, user);
|
|
65
|
+
};
|
|
66
|
+
// Firebase Auth
|
|
67
|
+
firebase.auth().onAuthStateChanged(function (user) {
|
|
68
|
+
if (user) {
|
|
69
|
+
user
|
|
70
|
+
.getIdToken()
|
|
71
|
+
.then(function (idToken) {
|
|
72
|
+
// Fetch
|
|
73
|
+
fetch(window.location.pathname, {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
headers: {
|
|
76
|
+
Accept: 'application/json',
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
},
|
|
79
|
+
body: JSON.stringify({ token: idToken, csrfToken: csrfToken }),
|
|
80
|
+
})
|
|
81
|
+
.then((response) => {
|
|
82
|
+
response
|
|
83
|
+
.json()
|
|
84
|
+
.then((data) => {
|
|
85
|
+
// Show Error Message
|
|
86
|
+
if (!data.success)
|
|
87
|
+
final_redirect(new Error(data.error));
|
|
88
|
+
// Complete
|
|
89
|
+
else
|
|
90
|
+
final_redirect(null, user);
|
|
91
|
+
})
|
|
92
|
+
.catch(final_redirect);
|
|
93
|
+
})
|
|
94
|
+
.catch(final_redirect);
|
|
95
|
+
})
|
|
96
|
+
.catch(final_redirect);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
// Sign In
|
|
100
|
+
firebase
|
|
101
|
+
.auth()
|
|
102
|
+
.signInWithCustomToken(token)
|
|
103
|
+
// Fail
|
|
104
|
+
.catch(final_redirect);
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
export default login;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Handles the logout process by signing out from Firebase Auth,
|
|
5
|
+
* verifying CSRF token, and redirecting the user.
|
|
6
|
+
*
|
|
7
|
+
* The `run` function signs out the user, sends a logout request to the server
|
|
8
|
+
* along with CSRF protection, and then redirects the user to the specified URL
|
|
9
|
+
* or executes a custom callback.
|
|
10
|
+
*
|
|
11
|
+
* @namespace logout
|
|
12
|
+
*/
|
|
13
|
+
const logout = {
|
|
14
|
+
/**
|
|
15
|
+
* Executes the logout process, verifying CSRF token, signing out
|
|
16
|
+
* the user, and handling redirection.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} token - The authentication token used to identify the user.
|
|
19
|
+
* @param {string} redirect_url - The relative URL to redirect the user after logout.
|
|
20
|
+
* If it starts with "/", it will be adjusted.
|
|
21
|
+
* @param {string} csrfToken - The CSRF token provided in the request.
|
|
22
|
+
* @param {string} original_csrfToken - The CSRF token that was initially generated.
|
|
23
|
+
* @param {function} [callback] - Optional callback to be called after logout.
|
|
24
|
+
* Signature: `(err, redirectFunction)`
|
|
25
|
+
* - If omitted, default redirection is used.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* logout.run(userToken, '/home', csrfToken, originalCsrfToken);
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* logout.run(userToken, '/home', csrfToken, originalCsrfToken, (err, redirect) => {
|
|
32
|
+
* if (err) {
|
|
33
|
+
* console.error('Logout failed:', err);
|
|
34
|
+
* return;
|
|
35
|
+
* }
|
|
36
|
+
* console.log('Logged out successfully');
|
|
37
|
+
* redirect(); // manually trigger the redirect
|
|
38
|
+
* });
|
|
39
|
+
*/
|
|
40
|
+
run: function (token, redirect_url, csrfToken, original_csrfToken, callback) {
|
|
41
|
+
// Compare CSRF Token
|
|
42
|
+
if (original_csrfToken.length < 1 || original_csrfToken === csrfToken) {
|
|
43
|
+
// Fix Redirect
|
|
44
|
+
if (typeof redirect_url === 'string') {
|
|
45
|
+
if (redirect_url.startsWith('/')) redirect_url = redirect_url.substring(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Nope
|
|
49
|
+
else redirect_url = '';
|
|
50
|
+
|
|
51
|
+
// Prepare Redirect
|
|
52
|
+
const final_redirect = function (err) {
|
|
53
|
+
// The Redirect
|
|
54
|
+
const start_redirect = function () {
|
|
55
|
+
window.location.href = window.location.origin + '/' + redirect_url;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Default Redirect
|
|
59
|
+
if (typeof callback !== 'function') {
|
|
60
|
+
// Show Error
|
|
61
|
+
if (err) alert(err.message);
|
|
62
|
+
// Redirect Now
|
|
63
|
+
start_redirect();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Custom Redirect
|
|
67
|
+
else callback(err, start_redirect);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// Sign Out
|
|
71
|
+
firebase
|
|
72
|
+
.auth()
|
|
73
|
+
.signOut()
|
|
74
|
+
// Success
|
|
75
|
+
.then(() => {
|
|
76
|
+
fetch(window.location.pathname, {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: {
|
|
79
|
+
Accept: 'application/json',
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({ token: token, csrfToken: csrfToken }),
|
|
83
|
+
})
|
|
84
|
+
.then((response) => {
|
|
85
|
+
response
|
|
86
|
+
.json()
|
|
87
|
+
.then(() => final_redirect())
|
|
88
|
+
.catch(final_redirect);
|
|
89
|
+
})
|
|
90
|
+
.catch(final_redirect);
|
|
91
|
+
})
|
|
92
|
+
// Fail
|
|
93
|
+
.catch(final_redirect);
|
|
94
|
+
}
|
|
95
|
+
// Invalid
|
|
96
|
+
else final_redirect(new Error('Invalid csrfToken!'));
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
module.exports = logout;
|