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 */