dommilosz / minecraft-auth

29 stars 3 forks source link
authentication microsoft-authentication minecraft

Minecraft-auth package

npm bundle size GitHub package.json version GitHub top language npm

Minecraft-auth is a package to authenticate and get minecraft access tokens.

Authentication Types:

Mojang API:

Package contains MojangApi class which can be used to fetch other users skins, uuids, check server status and more.

2.0.0 migration

Version 2.0.0 changes how Microsoft Authentication works.

Error handling:

All authentication errors are thrown by using AuthenticationError or OwnershipError classes they all extend Error class. AuthenticationError also contains additionalInfo: string

Installation:

npm i --save minecraft-auth

Importing:

import * as minecraftAuth from "./src/index";
//or
const minecraftAuth = require("./src/index.ts");

Authentication Examples:

let account = new minecraftAuth.MicrosoftAccount(); MicrosoftAuth.setup({appID:"747bf062-ab9c-4690-842d-a77d18d4cf82"}); //https://github.com/dommilosz/minecraft-auth/wiki/How-to-setup-Microsoft-Auth let code = await MicrosoftAuth.listenForCode();

if(code !== undefined){ await account.authFlow(code); }


* Microsoft Authentication ([don't have specified lifetimes](https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#refresh-the-access-token)):
 ```javascript
const MicrosoftAuth = minecraftAuth.MicrosoftAuth;

let account = new minecraftAuth.MicrosoftAccount();
MicrosoftAuth.setup({appID:"YOUR APP ID", appSecret:"YOUR APP SECRET"}); //https://github.com/dommilosz/minecraft-auth/wiki/How-to-setup-Microsoft-Auth
let code = await MicrosoftAuth.listenForCode();

if(code !== undefined){
    await account.authFlow(code);
}

Usage example

//any type of authentication eg. from above examples

console.log(account.accessToken);
await account.getProfile();
console.log(account.username);            //Username of the account
console.log(account.uuid);                //UUID of the account (without dashes)
console.log(account.ownership);           //Does account even have minecraft
console.log(account.profile)              //User profile - skins, capes, uuid, username
console.log(account.profile.skins[0].url) //URL of the 1st skin.

accountsStorage:

AccountsStorage is a storage for your accounts.

Adding accounts:

You can add new account with AccountsStorage::addAccount(account)

Removing accounts:

You can remove account with AccountsStorage::removeAccount(account)

Getting accounts:

You can get accounts with: