Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better client.login() feedback #22

Merged
merged 2 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,24 @@ const client = new Instagram({ username, password })
})()
```

Save credentials to disk. The method `login` resolve an `Object` with credentials, this allows you to save in disk or any database:
Save cookies to disk by using a `though-cookie` store.

```js
// Native
const { existsSync } = require('fs')
const { join: joinPath } = require('path')

// Packages
const Instagram = require('instagram-web-api')
const loadJSON = require('load-json-file')
const writeJSON = require('write-json-file')

const credentialsFile = joinPath(__dirname, 'credentials.json')

;(async () => {
let client
const FileCookieStore = require('tough-cookie-filestore2')

if (existsSync(credentialsFile)) {
client = new Instagram(await loadJSON(credentialsFile))
} else {
const { username, password } = process.env
client = new Instagram({ username, password })
const { username, password } = process.env // Only required when no cookies are stored yet

const credentials = await client.login()
await writeJSON(credentialsFile, credentials)
}
const cookieStore = new FileCookieStore('./cookies.json')
const client = new Instagram({ username, password, cookieStore })

;(async () => {
// URL or path of photo
const photo = 'https://scontent-scl1-1.cdninstagram.com/t51.2885-15/e35/22430378_307692683052790_5667315385519570944_n.jpg'
const photo =
'https://scontent-scl1-1.cdninstagram.com/t51.2885-15/e35/22430378_307692683052790_5667315385519570944_n.jpg'

await client.login()

// Upload Photo
const { media } = await client.uploadPhoto(photo)
Expand Down Expand Up @@ -149,8 +138,10 @@ const client = new Instagram({ username: '', password: '' }, { language: 'es-CL'
### login(credentials)
```js
const { username, password, cookies } = await client.login({ username: '', password: '' })
const { authenticated, user } = await client.login({ username: '', password: '' })
```
> Login in the account, this method return an object with the credentials (username, password and cookies) for saving the session.

> Login in the account, this method returns `user` (`true` when username is valid) and `authenticated` (`true` when login was successful)
- `credentials`
- `username`: The username of account
- `password`: The password of account
Expand Down
14 changes: 7 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ class Instagram {
})

// Login
const cookies = await this.request
.post('/accounts/login/ajax/', {
resolveWithFullResponse: true,
form: { username, password }
})
.then(res => res.headers['set-cookie'].map(Cookie.parse))
const res = await this.request.post('/accounts/login/ajax/', {
resolveWithFullResponse: true,
form: { username, password }
})

const cookies = res.headers['set-cookie'].map(Cookie.parse)

// Get CSRFToken after successful login
const { value: csrftoken } = cookies
Expand All @@ -85,7 +85,7 @@ class Instagram {
cookies: cookies.map(cookie => cookie.toJSON())
}

return this.credentials
return res.body
}

logout() {
Expand Down
14 changes: 8 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import { media, users, locations, tags } from './helpers'
const { username, password } = process.env

const client = new Instagram({ username, password })
let credentials
let authentication
let commentId
let profile

test.before(async () => {
credentials = await client.login()
authentication = await client.login()
})

test('credentials', t => {
t.is(credentials.username, username)
t.is(credentials.password, password)
t.true(Array.isArray(credentials.cookies))
test('authentication', t => {
t.is(authentication.status, 'ok')
t.is(client.credentials.username, username)
t.is(client.credentials.password, password)
t.true(authentication.authenticated)
t.true(Array.isArray(client.credentials.cookies))
})

test('getHome', async t => {
Expand Down