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,30 @@
|
|
|
1
|
+
import tinyDice from '../../../libs/dice.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a random Discord avatar URL or returns a specified avatar URL based on the provided value.
|
|
4
|
+
* If no value is provided, a random avatar from the Discord CDN is selected.
|
|
5
|
+
*
|
|
6
|
+
* @function
|
|
7
|
+
* @param {string|number|null} [value=null] - The avatar number or string. If provided, it will return the avatar associated with that number or string.
|
|
8
|
+
* If no value is provided or the value is invalid, a random avatar will be generated.
|
|
9
|
+
* @param {string} [url='https://cdn.discordapp.com/embed/avatars/'] - The base URL for the Discord avatars. By default, it uses the CDN URL.
|
|
10
|
+
*
|
|
11
|
+
* @returns {string} The URL of the avatar image.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Returns a random avatar URL
|
|
15
|
+
* const avatarURL = randomAvatar();
|
|
16
|
+
* console.log(avatarURL); // Example output: https://cdn.discordapp.com/embed/avatars/1234.png
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Returns a specific avatar URL based on the value
|
|
20
|
+
* const avatarURL = randomAvatar(2);
|
|
21
|
+
* console.log(avatarURL); // Example output: https://cdn.discordapp.com/embed/avatars/2.png
|
|
22
|
+
*/
|
|
23
|
+
export default function randomAvatar(value = null, url = 'https://cdn.discordapp.com/embed/avatars/') {
|
|
24
|
+
// Random
|
|
25
|
+
if (typeof value !== 'number' && typeof value !== 'string')
|
|
26
|
+
return url + tinyDice.vanilla(4) + '.png';
|
|
27
|
+
// Nope
|
|
28
|
+
else
|
|
29
|
+
return url + 'https://cdn.discordapp.com/embed/avatars/' + value + '.png';
|
|
30
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var login = require('./login.cjs');
|
|
4
|
+
var logout = require('./logout.cjs');
|
|
5
|
+
var redirect = require('./redirect.cjs');
|
|
6
|
+
var refreshToken = require('./refreshToken.cjs');
|
|
7
|
+
|
|
8
|
+
const getItems = {
|
|
9
|
+
login,
|
|
10
|
+
logout,
|
|
11
|
+
redirect,
|
|
12
|
+
refreshToken,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = getItems;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default getItems;
|
|
2
|
+
declare namespace getItems {
|
|
3
|
+
export { login };
|
|
4
|
+
export { logout };
|
|
5
|
+
export { redirect };
|
|
6
|
+
export { refreshToken };
|
|
7
|
+
}
|
|
8
|
+
import login from './login.mjs';
|
|
9
|
+
import logout from './logout.mjs';
|
|
10
|
+
import redirect from './redirect.mjs';
|
|
11
|
+
import refreshToken from './refreshToken.mjs';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import login from './login.mjs';
|
|
2
|
+
import logout from './logout.mjs';
|
|
3
|
+
import redirect from './redirect.mjs';
|
|
4
|
+
import refreshToken from './refreshToken.mjs';
|
|
5
|
+
const getItems = {
|
|
6
|
+
login,
|
|
7
|
+
logout,
|
|
8
|
+
redirect,
|
|
9
|
+
refreshToken,
|
|
10
|
+
};
|
|
11
|
+
export default getItems;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var objType = require('../../../get/objType.cjs');
|
|
5
|
+
var _default = require('../../../crypto/default.cjs');
|
|
6
|
+
var HTTP1_0 = require('../../../http/HTTP-1.0.cjs');
|
|
7
|
+
var getDomainURL = require('../../../http/getDomainURL.cjs');
|
|
8
|
+
var authURLGenerator = require('../get/authURLGenerator.cjs');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Handles the login process by checking the configuration, generating authentication URLs, and redirecting the user to the appropriate destination.
|
|
12
|
+
* Depending on the configuration type, it handles various authentication methods like login, login_command, and webhook.
|
|
13
|
+
*
|
|
14
|
+
* @function
|
|
15
|
+
* @param {Object} req - The request object, typically provided by the web framework (e.g., Express).
|
|
16
|
+
* @param {Object} res - The response object, typically provided by the web framework (e.g., Express).
|
|
17
|
+
* @param {Object} cfg - The configuration object that includes various settings for the login process.
|
|
18
|
+
* @param {boolean} existSession - A flag indicating whether the user already has an active session.
|
|
19
|
+
*
|
|
20
|
+
* @returns {void} This function does not return anything. It sends a redirect response to the user based on the configuration.
|
|
21
|
+
*
|
|
22
|
+
* @throws {Error} If any validation or configuration fails, an error response will be sent.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Example usage with Express:
|
|
26
|
+
* app.get('/login', (req, res) => {
|
|
27
|
+
* login(req, res, config, sessionExists);
|
|
28
|
+
* });
|
|
29
|
+
*/
|
|
30
|
+
function login(req, res, cfg, existSession) {
|
|
31
|
+
// Send Error
|
|
32
|
+
const sendError = function (data) {
|
|
33
|
+
if (typeof cfg.errorCallback !== 'function') return HTTP1_0.send(res, data.code);
|
|
34
|
+
else return cfg.errorCallback(data, req, res);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Detect Config
|
|
38
|
+
if (
|
|
39
|
+
objType(cfg, 'object') &&
|
|
40
|
+
typeof cfg.type === 'string' &&
|
|
41
|
+
(cfg.type === 'login' || cfg.type === 'login_command' || cfg.type === 'webhook')
|
|
42
|
+
) {
|
|
43
|
+
// Create Settings
|
|
44
|
+
const tinyCrypto = _.defaultsDeep({}, cfg.crypto, _default);
|
|
45
|
+
|
|
46
|
+
// Detect Config
|
|
47
|
+
if (objType(tinyCrypto, 'object')) {
|
|
48
|
+
// Create Settings
|
|
49
|
+
const tinyCfg = _.defaultsDeep({}, cfg.auth, {
|
|
50
|
+
redirect: 'http://localhost/redirect',
|
|
51
|
+
discordScope: [],
|
|
52
|
+
client_id: '',
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Validate Config
|
|
56
|
+
if (objType(tinyCfg, 'object')) {
|
|
57
|
+
// Default Values State
|
|
58
|
+
let tinyState = _.defaultsDeep({}, cfg.state, {
|
|
59
|
+
csrfToken: '',
|
|
60
|
+
redirect: '',
|
|
61
|
+
type: cfg.type,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Validate
|
|
65
|
+
if (typeof tinyState.type !== 'string') tinyState.type = cfg.type;
|
|
66
|
+
|
|
67
|
+
// Validate State
|
|
68
|
+
if (objType(tinyState, 'object')) {
|
|
69
|
+
// Create Settings
|
|
70
|
+
const tinyQuery = _.defaultsDeep({}, cfg.query, {
|
|
71
|
+
redirect: 'redirect',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Exist Cfg
|
|
75
|
+
if (objType(tinyQuery, 'object')) {
|
|
76
|
+
// Get Domain
|
|
77
|
+
const tinyDomain = getDomainURL(req, cfg.port);
|
|
78
|
+
|
|
79
|
+
// Redirect
|
|
80
|
+
let returnRedirect = tinyDomain + '/';
|
|
81
|
+
if (objType(req.query, 'object')) {
|
|
82
|
+
if (typeof req.query[tinyQuery.redirect] === 'string') {
|
|
83
|
+
req.query[tinyQuery.redirect] = req.query[tinyQuery.redirect].trim();
|
|
84
|
+
if (!req.query[tinyQuery.redirect].startsWith('http')) {
|
|
85
|
+
if (req.query[tinyQuery.redirect].startsWith('/'))
|
|
86
|
+
req.query[tinyQuery.redirect] = req.query[tinyQuery.redirect].substring(1);
|
|
87
|
+
|
|
88
|
+
// Return Redirect
|
|
89
|
+
tinyState.redirect = req.query[tinyQuery.redirect];
|
|
90
|
+
returnRedirect = req.query[tinyQuery.redirect];
|
|
91
|
+
|
|
92
|
+
// Fix Redirect
|
|
93
|
+
returnRedirect = tinyDomain + '/' + returnRedirect;
|
|
94
|
+
} else tinyState.redirect = '';
|
|
95
|
+
} else {
|
|
96
|
+
tinyState.redirect = '';
|
|
97
|
+
delete req.query[tinyQuery.redirect];
|
|
98
|
+
}
|
|
99
|
+
} else tinyState.redirect = '';
|
|
100
|
+
|
|
101
|
+
// Don't exist session
|
|
102
|
+
if (!existSession || cfg.type === 'login_command' || cfg.type === 'webhook') {
|
|
103
|
+
// Redirect Result
|
|
104
|
+
const redirect_discord = authURLGenerator(tinyCfg, tinyState, tinyCrypto, cfg.type);
|
|
105
|
+
return res.redirect(redirect_discord);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Yes
|
|
109
|
+
else return res.redirect(returnRedirect);
|
|
110
|
+
}
|
|
111
|
+
// Nope
|
|
112
|
+
else return sendError({ code: 400, message: 'Invalide Request!' });
|
|
113
|
+
}
|
|
114
|
+
// Error
|
|
115
|
+
else return sendError({ code: 400, message: 'Invalide State Config!' });
|
|
116
|
+
}
|
|
117
|
+
// Error
|
|
118
|
+
else return sendError({ code: 500, message: 'Invalide System Config!' });
|
|
119
|
+
}
|
|
120
|
+
// Nope
|
|
121
|
+
else return sendError({ code: 500, message: 'Invalid Crypto Values!' });
|
|
122
|
+
}
|
|
123
|
+
// Nope
|
|
124
|
+
else return sendError({ code: 500, message: 'Invalid Config Values!' });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = login;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles the login process by checking the configuration, generating authentication URLs, and redirecting the user to the appropriate destination.
|
|
3
|
+
* Depending on the configuration type, it handles various authentication methods like login, login_command, and webhook.
|
|
4
|
+
*
|
|
5
|
+
* @function
|
|
6
|
+
* @param {Object} req - The request object, typically provided by the web framework (e.g., Express).
|
|
7
|
+
* @param {Object} res - The response object, typically provided by the web framework (e.g., Express).
|
|
8
|
+
* @param {Object} cfg - The configuration object that includes various settings for the login process.
|
|
9
|
+
* @param {boolean} existSession - A flag indicating whether the user already has an active session.
|
|
10
|
+
*
|
|
11
|
+
* @returns {void} This function does not return anything. It sends a redirect response to the user based on the configuration.
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} If any validation or configuration fails, an error response will be sent.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Example usage with Express:
|
|
17
|
+
* app.get('/login', (req, res) => {
|
|
18
|
+
* login(req, res, config, sessionExists);
|
|
19
|
+
* });
|
|
20
|
+
*/
|
|
21
|
+
export default function login(req: Object, res: Object, cfg: Object, existSession: boolean): void;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import objType from '../../../get/objType.mjs';
|
|
3
|
+
import defaultCrypto from '../../../crypto/default.mjs';
|
|
4
|
+
import http_status from '../../../http/HTTP-1.0.mjs';
|
|
5
|
+
import getDomainURL from '../../../http/getDomainURL.mjs';
|
|
6
|
+
import authURLGenerator from '../get/authURLGenerator.mjs';
|
|
7
|
+
/**
|
|
8
|
+
* Handles the login process by checking the configuration, generating authentication URLs, and redirecting the user to the appropriate destination.
|
|
9
|
+
* Depending on the configuration type, it handles various authentication methods like login, login_command, and webhook.
|
|
10
|
+
*
|
|
11
|
+
* @function
|
|
12
|
+
* @param {Object} req - The request object, typically provided by the web framework (e.g., Express).
|
|
13
|
+
* @param {Object} res - The response object, typically provided by the web framework (e.g., Express).
|
|
14
|
+
* @param {Object} cfg - The configuration object that includes various settings for the login process.
|
|
15
|
+
* @param {boolean} existSession - A flag indicating whether the user already has an active session.
|
|
16
|
+
*
|
|
17
|
+
* @returns {void} This function does not return anything. It sends a redirect response to the user based on the configuration.
|
|
18
|
+
*
|
|
19
|
+
* @throws {Error} If any validation or configuration fails, an error response will be sent.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Example usage with Express:
|
|
23
|
+
* app.get('/login', (req, res) => {
|
|
24
|
+
* login(req, res, config, sessionExists);
|
|
25
|
+
* });
|
|
26
|
+
*/
|
|
27
|
+
export default function login(req, res, cfg, existSession) {
|
|
28
|
+
// Send Error
|
|
29
|
+
const sendError = function (data) {
|
|
30
|
+
if (typeof cfg.errorCallback !== 'function')
|
|
31
|
+
return http_status.send(res, data.code);
|
|
32
|
+
else
|
|
33
|
+
return cfg.errorCallback(data, req, res);
|
|
34
|
+
};
|
|
35
|
+
// Detect Config
|
|
36
|
+
if (objType(cfg, 'object') &&
|
|
37
|
+
typeof cfg.type === 'string' &&
|
|
38
|
+
(cfg.type === 'login' || cfg.type === 'login_command' || cfg.type === 'webhook')) {
|
|
39
|
+
// Create Settings
|
|
40
|
+
const tinyCrypto = _.defaultsDeep({}, cfg.crypto, defaultCrypto);
|
|
41
|
+
// Detect Config
|
|
42
|
+
if (objType(tinyCrypto, 'object')) {
|
|
43
|
+
// Create Settings
|
|
44
|
+
const tinyCfg = _.defaultsDeep({}, cfg.auth, {
|
|
45
|
+
redirect: 'http://localhost/redirect',
|
|
46
|
+
discordScope: [],
|
|
47
|
+
client_id: '',
|
|
48
|
+
});
|
|
49
|
+
// Validate Config
|
|
50
|
+
if (objType(tinyCfg, 'object')) {
|
|
51
|
+
// Default Values State
|
|
52
|
+
let tinyState = _.defaultsDeep({}, cfg.state, {
|
|
53
|
+
csrfToken: '',
|
|
54
|
+
redirect: '',
|
|
55
|
+
type: cfg.type,
|
|
56
|
+
});
|
|
57
|
+
// Validate
|
|
58
|
+
if (typeof tinyState.type !== 'string')
|
|
59
|
+
tinyState.type = cfg.type;
|
|
60
|
+
// Validate State
|
|
61
|
+
if (objType(tinyState, 'object')) {
|
|
62
|
+
// Create Settings
|
|
63
|
+
const tinyQuery = _.defaultsDeep({}, cfg.query, {
|
|
64
|
+
redirect: 'redirect',
|
|
65
|
+
});
|
|
66
|
+
// Exist Cfg
|
|
67
|
+
if (objType(tinyQuery, 'object')) {
|
|
68
|
+
// Get Domain
|
|
69
|
+
const tinyDomain = getDomainURL(req, cfg.port);
|
|
70
|
+
// Redirect
|
|
71
|
+
let returnRedirect = tinyDomain + '/';
|
|
72
|
+
if (objType(req.query, 'object')) {
|
|
73
|
+
if (typeof req.query[tinyQuery.redirect] === 'string') {
|
|
74
|
+
req.query[tinyQuery.redirect] = req.query[tinyQuery.redirect].trim();
|
|
75
|
+
if (!req.query[tinyQuery.redirect].startsWith('http')) {
|
|
76
|
+
if (req.query[tinyQuery.redirect].startsWith('/'))
|
|
77
|
+
req.query[tinyQuery.redirect] = req.query[tinyQuery.redirect].substring(1);
|
|
78
|
+
// Return Redirect
|
|
79
|
+
tinyState.redirect = req.query[tinyQuery.redirect];
|
|
80
|
+
returnRedirect = req.query[tinyQuery.redirect];
|
|
81
|
+
// Fix Redirect
|
|
82
|
+
returnRedirect = tinyDomain + '/' + returnRedirect;
|
|
83
|
+
}
|
|
84
|
+
else
|
|
85
|
+
tinyState.redirect = '';
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
tinyState.redirect = '';
|
|
89
|
+
delete req.query[tinyQuery.redirect];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else
|
|
93
|
+
tinyState.redirect = '';
|
|
94
|
+
// Don't exist session
|
|
95
|
+
if (!existSession || cfg.type === 'login_command' || cfg.type === 'webhook') {
|
|
96
|
+
// Redirect Result
|
|
97
|
+
const redirect_discord = authURLGenerator(tinyCfg, tinyState, tinyCrypto, cfg.type);
|
|
98
|
+
return res.redirect(redirect_discord);
|
|
99
|
+
}
|
|
100
|
+
// Yes
|
|
101
|
+
else
|
|
102
|
+
return res.redirect(returnRedirect);
|
|
103
|
+
}
|
|
104
|
+
// Nope
|
|
105
|
+
else
|
|
106
|
+
return sendError({ code: 400, message: 'Invalide Request!' });
|
|
107
|
+
}
|
|
108
|
+
// Error
|
|
109
|
+
else
|
|
110
|
+
return sendError({ code: 400, message: 'Invalide State Config!' });
|
|
111
|
+
}
|
|
112
|
+
// Error
|
|
113
|
+
else
|
|
114
|
+
return sendError({ code: 500, message: 'Invalide System Config!' });
|
|
115
|
+
}
|
|
116
|
+
// Nope
|
|
117
|
+
else
|
|
118
|
+
return sendError({ code: 500, message: 'Invalid Crypto Values!' });
|
|
119
|
+
}
|
|
120
|
+
// Nope
|
|
121
|
+
else
|
|
122
|
+
return sendError({ code: 500, message: 'Invalid Config Values!' });
|
|
123
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var objType = require('../../../get/objType.cjs');
|
|
5
|
+
var revokeToken = require('../api/revokeToken.cjs');
|
|
6
|
+
var getUser = require('../api/getUser.cjs');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Handles the logout process for the user by revoking the token and redirecting.
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} req - The HTTP request object.
|
|
12
|
+
* @param {string|number} access_token - The access token of the user to be revoked.
|
|
13
|
+
* @param {Object} cfg - Configuration object containing settings for the logout process.
|
|
14
|
+
* @param {boolean} existSession - A flag indicating whether a session exists.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Promise<Object>} Resolves with a result object or rejects with an error.
|
|
17
|
+
*
|
|
18
|
+
* @throws {Object} Rejects with an error if configuration or token values are invalid.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* logout(req, 'userAccessToken', cfg, true)
|
|
22
|
+
* .then(result => {
|
|
23
|
+
* // Handle successful logout
|
|
24
|
+
* })
|
|
25
|
+
* .catch(err => {
|
|
26
|
+
* // Handle error during logout
|
|
27
|
+
* });
|
|
28
|
+
*/
|
|
29
|
+
async function logout(req, access_token, cfg, existSession) {
|
|
30
|
+
return new Promise(function (resolve, reject) {
|
|
31
|
+
// Detect Config
|
|
32
|
+
if (objType(cfg, 'object')) {
|
|
33
|
+
// Create Settings
|
|
34
|
+
const tinyQuery = _.defaultsDeep({}, cfg.query, {
|
|
35
|
+
csrfToken: 'csrfToken',
|
|
36
|
+
redirect: 'redirect',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const tinyState = _.defaultsDeep({}, cfg.state, {
|
|
40
|
+
csrfToken: '',
|
|
41
|
+
redirect: '',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Exist Query Setting
|
|
45
|
+
if (objType(tinyQuery, 'object')) {
|
|
46
|
+
// Exist Query
|
|
47
|
+
if (
|
|
48
|
+
typeof tinyState.csrfToken !== 'string' ||
|
|
49
|
+
tinyState.csrfToken.length < 1 ||
|
|
50
|
+
(objType(req.query, 'object') &&
|
|
51
|
+
typeof cfg.csrfToken === 'string' &&
|
|
52
|
+
tinyState.csrfToken === cfg.csrfToken)
|
|
53
|
+
) {
|
|
54
|
+
// Result
|
|
55
|
+
const result = {
|
|
56
|
+
data: null,
|
|
57
|
+
existSession: existSession,
|
|
58
|
+
complete: false,
|
|
59
|
+
state: { csrfToken: cfg.csrfToken },
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Prepare Final Redirect
|
|
63
|
+
result.redirect = '/';
|
|
64
|
+
|
|
65
|
+
// Redirect
|
|
66
|
+
if (typeof tinyState.redirect === 'string' && tinyState.redirect.length > 0) {
|
|
67
|
+
if (tinyState.redirect.startsWith('/'))
|
|
68
|
+
result.redirect = tinyState.redirect.substring(1);
|
|
69
|
+
else result.redirect = tinyState.redirect;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Get Query
|
|
73
|
+
else if (typeof req.query[tinyQuery.redirect] === 'string') {
|
|
74
|
+
if (req.query[tinyQuery.redirect].startsWith('/'))
|
|
75
|
+
result.redirect = req.query[tinyQuery.redirect].substring(1);
|
|
76
|
+
else result.redirect = req.query[tinyQuery.redirect];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Exist Session
|
|
80
|
+
if (existSession) {
|
|
81
|
+
// Exist Token
|
|
82
|
+
if (
|
|
83
|
+
(typeof access_token === 'string' && access_token.length > 0) ||
|
|
84
|
+
(typeof access_token === 'number' && !isNaN(access_token))
|
|
85
|
+
) {
|
|
86
|
+
// Prepare Auth
|
|
87
|
+
const tinyAuth = _.defaultsDeep({}, cfg.auth, {
|
|
88
|
+
client_id: '',
|
|
89
|
+
client_secret: '',
|
|
90
|
+
redirect_uri: '',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Exist Client ID
|
|
94
|
+
if (
|
|
95
|
+
(typeof tinyAuth.client_id === 'string' && tinyAuth.client_id.length > 0) ||
|
|
96
|
+
(typeof tinyAuth.client_id === 'number' && !isNaN(tinyAuth.client_id))
|
|
97
|
+
) {
|
|
98
|
+
// Exist Client Secret
|
|
99
|
+
if (
|
|
100
|
+
(typeof tinyAuth.client_secret === 'string' &&
|
|
101
|
+
tinyAuth.client_secret.length > 0) ||
|
|
102
|
+
(typeof tinyAuth.client_secret === 'number' && !isNaN(tinyAuth.client_secret))
|
|
103
|
+
) {
|
|
104
|
+
// End Discord
|
|
105
|
+
const end_discord_session = function () {
|
|
106
|
+
// Get API HTTP and Revoke the Token
|
|
107
|
+
revokeToken(access_token, tinyAuth)
|
|
108
|
+
.then((data) => {
|
|
109
|
+
result.data = data;
|
|
110
|
+
resolve(result);
|
|
111
|
+
})
|
|
112
|
+
.catch((err) => {
|
|
113
|
+
result.err = err;
|
|
114
|
+
reject(result);
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// Don't need user info
|
|
119
|
+
if (typeof access_token !== 'string' || access_token.length < 1)
|
|
120
|
+
end_discord_session();
|
|
121
|
+
// Yes
|
|
122
|
+
else {
|
|
123
|
+
// Get User
|
|
124
|
+
getUser(access_token)
|
|
125
|
+
.then((data) => {
|
|
126
|
+
result.user = data;
|
|
127
|
+
end_discord_session();
|
|
128
|
+
})
|
|
129
|
+
.catch(reject);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Nope
|
|
133
|
+
else reject({ code: 401, message: 'Invalid Client Secret!' });
|
|
134
|
+
}
|
|
135
|
+
// Nope
|
|
136
|
+
else reject({ code: 401, message: 'Invalid Client ID!' });
|
|
137
|
+
}
|
|
138
|
+
// Nope
|
|
139
|
+
else reject({ code: 401, message: 'Invalid Token Data!' });
|
|
140
|
+
}
|
|
141
|
+
// Nope
|
|
142
|
+
else resolve(result);
|
|
143
|
+
}
|
|
144
|
+
// Nope
|
|
145
|
+
else reject({ code: 401, message: 'Invalid csrfToken!' });
|
|
146
|
+
}
|
|
147
|
+
// Nope
|
|
148
|
+
else reject({ code: 401, message: 'Invalid query setting!' });
|
|
149
|
+
}
|
|
150
|
+
// Nope
|
|
151
|
+
else reject({ code: 500, message: 'Invalid Config Values!' });
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
module.exports = logout;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handles the logout process for the user by revoking the token and redirecting.
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} req - The HTTP request object.
|
|
5
|
+
* @param {string|number} access_token - The access token of the user to be revoked.
|
|
6
|
+
* @param {Object} cfg - Configuration object containing settings for the logout process.
|
|
7
|
+
* @param {boolean} existSession - A flag indicating whether a session exists.
|
|
8
|
+
*
|
|
9
|
+
* @returns {Promise<Object>} Resolves with a result object or rejects with an error.
|
|
10
|
+
*
|
|
11
|
+
* @throws {Object} Rejects with an error if configuration or token values are invalid.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* logout(req, 'userAccessToken', cfg, true)
|
|
15
|
+
* .then(result => {
|
|
16
|
+
* // Handle successful logout
|
|
17
|
+
* })
|
|
18
|
+
* .catch(err => {
|
|
19
|
+
* // Handle error during logout
|
|
20
|
+
* });
|
|
21
|
+
*/
|
|
22
|
+
export default function logout(req: Object, access_token: string | number, cfg: Object, existSession: boolean): Promise<Object>;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import objType from '../../../get/objType.mjs';
|
|
3
|
+
import revokeToken from '../api/revokeToken.mjs';
|
|
4
|
+
import getUser from '../api/getUser.mjs';
|
|
5
|
+
/**
|
|
6
|
+
* Handles the logout process for the user by revoking the token and redirecting.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} req - The HTTP request object.
|
|
9
|
+
* @param {string|number} access_token - The access token of the user to be revoked.
|
|
10
|
+
* @param {Object} cfg - Configuration object containing settings for the logout process.
|
|
11
|
+
* @param {boolean} existSession - A flag indicating whether a session exists.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Promise<Object>} Resolves with a result object or rejects with an error.
|
|
14
|
+
*
|
|
15
|
+
* @throws {Object} Rejects with an error if configuration or token values are invalid.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* logout(req, 'userAccessToken', cfg, true)
|
|
19
|
+
* .then(result => {
|
|
20
|
+
* // Handle successful logout
|
|
21
|
+
* })
|
|
22
|
+
* .catch(err => {
|
|
23
|
+
* // Handle error during logout
|
|
24
|
+
* });
|
|
25
|
+
*/
|
|
26
|
+
export default async function logout(req, access_token, cfg, existSession) {
|
|
27
|
+
return new Promise(function (resolve, reject) {
|
|
28
|
+
// Detect Config
|
|
29
|
+
if (objType(cfg, 'object')) {
|
|
30
|
+
// Create Settings
|
|
31
|
+
const tinyQuery = _.defaultsDeep({}, cfg.query, {
|
|
32
|
+
csrfToken: 'csrfToken',
|
|
33
|
+
redirect: 'redirect',
|
|
34
|
+
});
|
|
35
|
+
const tinyState = _.defaultsDeep({}, cfg.state, {
|
|
36
|
+
csrfToken: '',
|
|
37
|
+
redirect: '',
|
|
38
|
+
});
|
|
39
|
+
// Exist Query Setting
|
|
40
|
+
if (objType(tinyQuery, 'object')) {
|
|
41
|
+
// Exist Query
|
|
42
|
+
if (typeof tinyState.csrfToken !== 'string' ||
|
|
43
|
+
tinyState.csrfToken.length < 1 ||
|
|
44
|
+
(objType(req.query, 'object') &&
|
|
45
|
+
typeof cfg.csrfToken === 'string' &&
|
|
46
|
+
tinyState.csrfToken === cfg.csrfToken)) {
|
|
47
|
+
// Result
|
|
48
|
+
const result = {
|
|
49
|
+
data: null,
|
|
50
|
+
existSession: existSession,
|
|
51
|
+
complete: false,
|
|
52
|
+
state: { csrfToken: cfg.csrfToken },
|
|
53
|
+
};
|
|
54
|
+
// Prepare Final Redirect
|
|
55
|
+
result.redirect = '/';
|
|
56
|
+
// Redirect
|
|
57
|
+
if (typeof tinyState.redirect === 'string' && tinyState.redirect.length > 0) {
|
|
58
|
+
if (tinyState.redirect.startsWith('/'))
|
|
59
|
+
result.redirect = tinyState.redirect.substring(1);
|
|
60
|
+
else
|
|
61
|
+
result.redirect = tinyState.redirect;
|
|
62
|
+
}
|
|
63
|
+
// Get Query
|
|
64
|
+
else if (typeof req.query[tinyQuery.redirect] === 'string') {
|
|
65
|
+
if (req.query[tinyQuery.redirect].startsWith('/'))
|
|
66
|
+
result.redirect = req.query[tinyQuery.redirect].substring(1);
|
|
67
|
+
else
|
|
68
|
+
result.redirect = req.query[tinyQuery.redirect];
|
|
69
|
+
}
|
|
70
|
+
// Exist Session
|
|
71
|
+
if (existSession) {
|
|
72
|
+
// Exist Token
|
|
73
|
+
if ((typeof access_token === 'string' && access_token.length > 0) ||
|
|
74
|
+
(typeof access_token === 'number' && !isNaN(access_token))) {
|
|
75
|
+
// Prepare Auth
|
|
76
|
+
const tinyAuth = _.defaultsDeep({}, cfg.auth, {
|
|
77
|
+
client_id: '',
|
|
78
|
+
client_secret: '',
|
|
79
|
+
redirect_uri: '',
|
|
80
|
+
});
|
|
81
|
+
// Exist Client ID
|
|
82
|
+
if ((typeof tinyAuth.client_id === 'string' && tinyAuth.client_id.length > 0) ||
|
|
83
|
+
(typeof tinyAuth.client_id === 'number' && !isNaN(tinyAuth.client_id))) {
|
|
84
|
+
// Exist Client Secret
|
|
85
|
+
if ((typeof tinyAuth.client_secret === 'string' &&
|
|
86
|
+
tinyAuth.client_secret.length > 0) ||
|
|
87
|
+
(typeof tinyAuth.client_secret === 'number' && !isNaN(tinyAuth.client_secret))) {
|
|
88
|
+
// End Discord
|
|
89
|
+
const end_discord_session = function () {
|
|
90
|
+
// Get API HTTP and Revoke the Token
|
|
91
|
+
revokeToken(access_token, tinyAuth)
|
|
92
|
+
.then((data) => {
|
|
93
|
+
result.data = data;
|
|
94
|
+
resolve(result);
|
|
95
|
+
})
|
|
96
|
+
.catch((err) => {
|
|
97
|
+
result.err = err;
|
|
98
|
+
reject(result);
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
// Don't need user info
|
|
102
|
+
if (typeof access_token !== 'string' || access_token.length < 1)
|
|
103
|
+
end_discord_session();
|
|
104
|
+
// Yes
|
|
105
|
+
else {
|
|
106
|
+
// Get User
|
|
107
|
+
getUser(access_token)
|
|
108
|
+
.then((data) => {
|
|
109
|
+
result.user = data;
|
|
110
|
+
end_discord_session();
|
|
111
|
+
})
|
|
112
|
+
.catch(reject);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Nope
|
|
116
|
+
else
|
|
117
|
+
reject({ code: 401, message: 'Invalid Client Secret!' });
|
|
118
|
+
}
|
|
119
|
+
// Nope
|
|
120
|
+
else
|
|
121
|
+
reject({ code: 401, message: 'Invalid Client ID!' });
|
|
122
|
+
}
|
|
123
|
+
// Nope
|
|
124
|
+
else
|
|
125
|
+
reject({ code: 401, message: 'Invalid Token Data!' });
|
|
126
|
+
}
|
|
127
|
+
// Nope
|
|
128
|
+
else
|
|
129
|
+
resolve(result);
|
|
130
|
+
}
|
|
131
|
+
// Nope
|
|
132
|
+
else
|
|
133
|
+
reject({ code: 401, message: 'Invalid csrfToken!' });
|
|
134
|
+
}
|
|
135
|
+
// Nope
|
|
136
|
+
else
|
|
137
|
+
reject({ code: 401, message: 'Invalid query setting!' });
|
|
138
|
+
}
|
|
139
|
+
// Nope
|
|
140
|
+
else
|
|
141
|
+
reject({ code: 500, message: 'Invalid Config Values!' });
|
|
142
|
+
});
|
|
143
|
+
}
|