From 842c185f4da29e98a3b2634814da67fc420fbb84 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Sat, 20 Aug 2022 05:28:54 -0300 Subject: [PATCH] chore(docs): improve auth example --- examples/auth/README.md | 20 ++++++++++------- examples/auth/index.js | 48 +++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/examples/auth/README.md b/examples/auth/README.md index 70138925..e633f886 100644 --- a/examples/auth/README.md +++ b/examples/auth/README.md @@ -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 diff --git a/examples/auth/index.js b/examples/auth/index.js index 84a5f74a..9953ab30 100644 --- a/examples/auth/index.js +++ b/examples/auth/index.js @@ -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(); +})(); \ No newline at end of file