chore(docs): improve auth example

This commit is contained in:
LuanRT
2022-08-20 05:28:54 -03:00
parent 790d528a2d
commit 842c185f4d
2 changed files with 37 additions and 31 deletions

View File

@@ -1,36 +1,40 @@
# Authentication via OAuth
## Usage
Before using any methods which require authentication, you have to authenticate the session:
```js
// 'auth-pending' is fired with the info needed to sign in via OAuth.
yt.session.on('auth-pending', data => {
yt.session.on('auth-pending', (data) => {
// data.verification_url contains the URL to visit to authenticate.
// data.user_code contains the code to enter on the website.
});
// 'auth' is fired once the authentication is complete
yt.session.on('auth', () => {
console.log('Sign in successful');
yt.session.on('auth', ({ credentials }) => {
// do something with the credentials, eg; save them in a database.
console.log('Sign in successful');
});
await session.signIn();
// 'update-credentials' is fired when the access token expires, if you do not save the updated credentials any subsequent request will fail
yt.session.on('update-credentials', ({ credentials }) => {
// do something with the updated credentials
});
await yt.session.signIn(/* credentials */);
```
### Cache Credentials
If you don't wish to sign in every time you start the session, you can cache the credentials:
If you don't wish to sign in every time you start the session, you can cache the credentials. Note that this SHOULD NOT be used in production, save your credentials in a database/file instead and pass them to `Session#signIn(creds?)` when signing in.
```js
// If you use this, the next call to signIn won't fire 'auth-pending' instead just 'auth'
await yt.session.oauth.cacheCredentials();
```
**Note:** When using cached credentials, you are still required to make a call to `Session#signIn`.
**Note:** When using cached credentials, you are still required to make a call to `Session#signIn()`.
### Sign Out

View File

@@ -1,35 +1,37 @@
const { Innertube, UniversalCache } = require('../../dist/index');
const { Innertube, UniversalCache } = require('youtubei.js');
(async () => {
const yt = await Innertube.create({
const yt = await Innertube.create({
// required if you wish to use OAuth#cacheCredentials
cache: new UniversalCache()
});
});
// 'auth-pending' is fired with the info needed to sign in via OAuth.
yt.session.on('auth-pending', data => {
// 'auth-pending' is fired with the info needed to sign in via OAuth.
yt.session.on('auth-pending', (data) => {
console.log(`Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`);
});
});
// 'auth' is fired once the authentication is complete
yt.session.on('auth', () => {
console.log('Sign in successful');
});
// 'auth' is fired once the authentication is complete
yt.session.on('auth', ({ credentials }) => {
console.log('Sign in successful:', credentials);
});
// Attempt to sign in
await yt.session.signIn();
// 'update-credentials' is fired when the access token expires, if you do not save the updated credentials any subsequent request will fail
yt.session.on('update-credentials', ({ credentials }) => {
console.log('Credentials updated:', credentials);
});
// ... do something after sign in
// Attempt to sign in
await yt.session.signIn();
// You may cache the session for later use
// If you use this, the next call to signIn won't fire 'auth-pending' instead just 'auth'
await yt.session.oauth.cacheCredentials();
// ... do something after sign in
// You may cache the session for later use
// If you use this, the next call to signIn won't fire 'auth-pending' instead just 'auth'.
await yt.session.oauth.cacheCredentials();
// Sign out of the session
// this will also remove the cached credentials
await yt.session.signOut();
})();
// Sign out of the session
// this will also remove the cached credentials
await yt.session.signOut();
})();