@silvacreate/ally-pinterest-driver
v0.0.18
Published
AdonisJS Ally driver for Pinterest
Maintainers
Readme
ally-pinterest-driver
An AdonisJS Ally driver for Pinterest OAuth2 authentication.
This package provides a custom driver for AdonisJS Ally to authenticate users via Pinterest. It allows you to easily request access to a user's Pinterest account, fetch their profile, and manage access/refresh tokens.
Installation
Install the package using your preferred package manager:
npm install @silvacreate/ally-pinterest-driver
# or
yarn add @silvacreate/ally-pinterest-driver
# or
pnpm add @silvacreate/ally-pinterest-driverConfiguration
1. Environment Variables
First, add your Pinterest OAuth credentials to your .env file. You can obtain these by creating an app in the Pinterest Developer Portal.
PINTEREST_CLIENT_ID=your_client_id
PINTEREST_CLIENT_SECRET=your_client_secret
PINTEREST_CALLBACK_URL=http://localhost:3333/pinterest/callbackMake sure to validate these inside your start/env.ts file:
// start/env.ts
export default Env.create(new URL('../', import.meta.url), {
PINTEREST_CLIENT_ID: Env.schema.string(),
PINTEREST_CLIENT_SECRET: Env.schema.string(),
PINTEREST_CALLBACK_URL: Env.schema.string(),
})2. Configure Ally
Open your config/ally.ts file and register the Pinterest driver:
import env from '#start/env'
import { defineConfig } from '@adonisjs/ally'
import { PinterestDriverService } from '@silvacreate/ally-pinterest-driverr'
const allyConfig = defineConfig({
pinterest: PinterestDriverService({
clientId: env.get('PINTEREST_CLIENT_ID'),
clientSecret: env.get('PINTEREST_CLIENT_SECRET'),
callbackUrl: env.get('PINTEREST_CALLBACK_URL'),
}),
})
export default allyConfig
declare module '@adonisjs/ally/types' {
interface SocialProviders extends InferSocialProviders<typeof allyConfig> {}
}Usage
You will need two routes and corresponding controller methods: one to redirect the user to Pinterest, and one to handle the callback.
1. Redirecting to Pinterest
Use the redirect method to send the user to Pinterest's authorization page. You can define the specific scopes your application needs here.
import type { HttpContext } from '@adonisjs/core/http'
export default class PinterestAuthController {
async redirect({ ally }: HttpContext) {
return ally.use('pinterest').redirect((request) => {
request.scopes([
'boards:write',
'boards:read',
'pins:write',
'pins:read',
'user_accounts:read',
])
})
}
}2. Handling the Callback
Once the user authorizes your app, Pinterest will redirect them back to your callback URL. You can then verify the request and fetch the user's profile and tokens.
import type { HttpContext } from '@adonisjs/core/http'
export default class PinterestAuthController {
async callback({ ally, response }: HttpContext) {
const pinterest = ally.use('pinterest')
// 1. Handle user denying access
if (pinterest.accessDenied()) {
return 'Access denied. You cancelled the login process.'
}
// 2. Prevent CSRF attacks
if (pinterest.stateMisMatch()) {
return 'State mismatch. Request may have been tampered with.'
}
// 3. Handle unknown errors
if (pinterest.hasError()) {
return pinterest.getError()
}
try {
const pinterestUser = await pinterest.user()
// 4 Your logic
} catch (error) {
return response.badRequest('OAuth failed. Check server logs.')
}
}
}