From 9c938416b6e4ddb95c29bd4544ecfeb04136719d Mon Sep 17 00:00:00 2001 From: Regalijan <72576136+Regalijan@users.noreply.github.com> Date: Fri, 2 Aug 2024 04:36:36 +0900 Subject: [PATCH] Switch userinfo endpoint (#807) * Update current user endpoint * Update typings for LoggedInUserData * Revert "Update typings for LoggedInUserData" This reverts commit 2e000b25f9f8e34d569159d97ffe311a9640e024. * Revert "Update current user endpoint" This reverts commit 00ae4b9c05808bd42bf24dbfcbd196391282bb7d. * Create getAuthenticatedUser function * Add AuthenticatedUserData type * Add file extension to getAuthenticatedUser require * Fix linting --------- Co-authored-by: Josh Muir --- lib/index.js | 1 + lib/util/getAuthenticatedUser.js | 41 ++++++++++++++++++++++++++++++++ typings/index.d.ts | 6 +++++ typings/jsDocs.ts | 9 +++++++ 4 files changed, 57 insertions(+) create mode 100644 lib/util/getAuthenticatedUser.js diff --git a/lib/index.js b/lib/index.js index 8d6b9a59..485410ca 100644 --- a/lib/index.js +++ b/lib/index.js @@ -172,6 +172,7 @@ noblox.onBlurbChange = require('./users/onBlurbChange.js') noblox.clearSession = require('./util/clearSession.js') noblox.generalRequest = require('./util/generalRequest.js') noblox.getAction = require('./util/getAction.js') +noblox.getAuthenticatedUser = require('./util/getAuthenticatedUser.js') noblox.getCurrentUser = require('./util/getCurrentUser.js') noblox.getGeneralToken = require('./util/getGeneralToken.js') noblox.getHash = require('./util/getHash.js') diff --git a/lib/util/getAuthenticatedUser.js b/lib/util/getAuthenticatedUser.js new file mode 100644 index 00000000..374ef1f4 --- /dev/null +++ b/lib/util/getAuthenticatedUser.js @@ -0,0 +1,41 @@ +// Includes +const http = require('./http.js').func + +// Args +exports.optional = ['jar'] + +// Docs +/** + * 🔐 Get the current authenticated user. + * @category Utility + * @alias getAuthenticatedUser + * @returns {AuthenticatedUserData} + * @example const noblox = require("noblox.js") + * // Login using your cookie. + * const user = await noblox.getAuthenticatedUser() +**/ + +// Define +exports.func = async function (args) { + const jar = args.jar + const httpOpt = { + url: '//users.roblox.com/v1/users/authenticated', + options: { + method: 'GET', + followRedirect: false, + jar, + json: true, + resolveWithFullResponse: true + } + } + + const res = await http(httpOpt) + + if (res.statusCode === 401) { + throw new Error('You are not logged in.') + } else if (res.statusCode !== 200) { + throw new Error(JSON.stringify(res.body)) + } + + return res.body +} diff --git a/typings/index.d.ts b/typings/index.d.ts index ac06f21d..26dc9e8a 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1004,6 +1004,12 @@ declare module "noblox.js" { IsPremium: boolean } + interface AuthenticatedUserData { + id: number; + name: string; + displayName: string; + } + interface UserLoginApiData { userId: number; } diff --git a/typings/jsDocs.ts b/typings/jsDocs.ts index c7d203e4..5660344a 100644 --- a/typings/jsDocs.ts +++ b/typings/jsDocs.ts @@ -1380,6 +1380,15 @@ type LoggedInUserData = { IsPremium: boolean } +/** + * @typedef +*/ +type AuthenticatedUserData = { + id: number; + name: string; + displayName: string; +} + /** * @typedef */