Skip to main content
Version: 0.17

Logging in

To log in to an account use the Storage class:

import { Storage } from 'megajs'

const storage = new Storage({
email: 'user@example.com',
password: 'correct horse battery example'
})

storage.once('ready', () => {
// User is now logged in
})

storage.once('error', error => {
// Some error happened
})

It may look weird to pass a callback to a constructor, but that's an option too:

const storage = new Storage({
email: 'user@example.com',
password: 'correct horse battery example'
}, error => {
if (error) {
// Some error happened
} else {
// User is now logged in
}
})

While is recommended to set up an user-agent before logging-in, in V0 the steps for doing so are a bit complicated. You can either switch to V1 or read more about it in the advanced usage page.

caution

If you are migrating from tonistiigi's mega you can still use the following code:

const mega = require('megajs')

const storage = mega({
email: 'user@example.com',
password: 'correct horse battery example'
}, error => {
if (error) {
// Some error happened
} else {
// User is now logged in
}
})

But this pattern might not be supported in future versions because it is not compatible with ES modules and causes issues with tree shaking, which is important when this using this library in a browser environment.

In the next part of this tutorial we will use the storage object to upload a file.