
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
oauth2-handler-example
Advanced tools
This is an example project demonstrating how to use the `@blur-effect/oauth2-handler` package with Google Calendar API. The example showcases multi-account support, allowing users to connect and manage multiple Google accounts simultaneously.
This is an example project demonstrating how to use the @blur-effect/oauth2-handler
package with Google Calendar API. The example showcases multi-account support, allowing users to connect and manage multiple Google accounts simultaneously.
npm install
.env
file in the root directory with the following variables:# Server Configuration
PORT=3000
SESSION_SECRET=your-session-secret
# Google OAuth Configuration
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GOOGLE_CALLBACK_URL=http://localhost:3000/oauth2/callback/google
Set up a Google Cloud Project and OAuth 2.0 credentials:
.env
fileStart the server:
npm start
This example demonstrates how to implement multi-account support using the OAuth2 handler:
The multi-account functionality is implemented through:
Session Storage:
Account Management:
activeAccountIndex
in the sessionToken Management:
The UniversalTokenStore
class provides these key methods for multi-account support:
StoredUserData
objects containing user info and tokensconst userAccounts = await store.getUserAccounts(req.sessionID, 'google');
const deleted = await store.deleteByEmail(req.sessionID, 'google', email);
save(agentId, provider, tokens):
registerProvider(provider, fetcher):
store.registerProvider('google', new GoogleUserInfoFetcher());
The Express application implements these route handlers for multi-account management:
OAuth Callback Handler - /oauth2/callback/google
:
Switch Account - /switch-account/:index
:
activeAccountIndex
Logout Specific Account - /logout/:index
:
Logout All Accounts - /logout-all
:
The application uses Express session middleware to maintain account state:
app.use(async (req, res, next) => {
// Initialize the accounts array if it doesn't exist
if (!req.session.accounts) {
req.session.accounts = [];
}
// Set the active account if there's at least one and none is active
if (req.session.accounts.length > 0 && req.session.activeAccountIndex === undefined) {
req.session.activeAccountIndex = 0;
}
next();
});
The session stores:
accounts
: Array of connected user accountsactiveAccountIndex
: Index of the currently active accountThe application refreshes account data on page load:
// In home route handler
if (req.session.accounts && req.session.accounts.length > 0) {
const userAccounts = await store.getUserAccounts(req.sessionID, 'google');
// Update accounts data if available
if (userAccounts && userAccounts.length > 0) {
// Create an updated accounts array preserving our events data
const updatedAccounts = [];
// Process all stored accounts from the token store
for (const userData of userAccounts) {
// Find if we already have this account in the session
const existingIndex = req.session.accounts.findIndex(
acc => acc.email === userData.userInfo.email
);
// Update existing or add new account
// ...
}
}
}
src/index.js
- Main application file with multi-account implementationsrc/views/
- EJS templates with account management UIsrc/public/
- Static assets including CSS for multi-account interface.env
- Environment variables (not included in git)The application includes debugging information to help understand the multi-account flow:
/
- Home page with account list/auth/google
- Start Google OAuth flow/oauth2/callback/google
- OAuth callback handler/switch-account/:index
- Switch to another connected account/logout/:index
- Remove a specific account/logout-all
- Remove all connected accounts/calendar
- View calendar events for the active account/delete-token
- API endpoint to delete tokens by emailFAQs
This is an example project demonstrating how to use the `@blur-effect/oauth2-handler` package with Google Calendar API. The example showcases multi-account support, allowing users to connect and manage multiple Google accounts simultaneously.
The npm package oauth2-handler-example receives a total of 1 weekly downloads. As such, oauth2-handler-example popularity was classified as not popular.
We found that oauth2-handler-example demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.