EML Docs

YggdrasilAuth

YggdrasilAuth authenticates players against a custom Yggdrasil-compatible server. This is useful if you run a private authentication service that implements the Yggdrasil protocol.

Warning

The official Mojang Yggdrasil endpoint has been deprecated. This class is intended for use with community-maintained or self-hosted Yggdrasil implementations. Ensure that your server validates Minecraft account ownership to comply with Minecraft’s Terms of Service.

import { YggdrasilAuth } from 'eml-lib'

authUser()

async function authUser() {
  const auth = new YggdrasilAuth('https://auth.myserver.com')

  try {
    const result = await auth.auth('GoldFrite', 'MyPassword123')

    // Handle multiple profiles if needed
    if ('profiles' in result) {
      const account = auth.selectProfile(result, { name: 'GoldFrite' })
      // store account and proceed to the home view
    } else {
      const account = result
      // store account and proceed to the home view
    }
  } catch (err) {
    console.error(err)
  }
}

Constructor

ParameterTypeDescriptionRequired?
urlstringThe base URL of your Yggdrasil server.Yes

auth() method

Authenticates a player with username and password.

ParameterTypeDescriptionRequired?
usernamestringThe player’s username or email.Yes
passwordstringThe player’s password.Yes

Returns: Promise<Account | MultipleProfiles> — Returns an Account directly if the user has a single profile. Returns a MultipleProfiles object if the account has several profiles; call selectProfile() to resolve it.

Throws: AUTH_ERROR — If authentication fails.

selectProfile() method

Selects a profile from a MultipleProfiles result.

ParameterTypeDescriptionRequired?
profilesMultipleProfilesThe object returned by auth() when multiple profiles exist.Yes
select{ id?: string, name?: string }The profile to select, by ID or name. If both are given, ID takes precedence.Yes

Returns: Account

Throws: AUTH_ERROR — If the specified profile does not exist.

validate() method

Checks whether an account’s access token is still valid.

ParameterTypeDescriptionRequired?
userAccountThe account to validate.Yes

Returns: Promise<boolean>true if valid, false if expired.

refresh() method

Renews an expired access token.

ParameterTypeDescriptionRequired?
userAccountThe account to refresh.Yes

Returns: Promise<Account>

Throws: AUTH_ERROR — If the refresh fails.

logout() method

Invalidates the current session token for the given account.

Note

This method uses the invalidate endpoint, which invalidates only the current session. It does not call signout, which would invalidate all active sessions for the account.

ParameterTypeDescriptionRequired?
userAccountThe account to log out.Yes

Returns: Promise<void>

Throws: AUTH_ERROR — If the logout fails.