@adonisjs/ally
Version:
Social authentication provider for AdonisJS
154 lines (153 loc) • 4.78 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import { type TwitterToken, type AllyUserContract, type ApiRequestContract, type TwitterDriverConfig } from '../types.ts';
import { Oauth1Driver } from '../abstract_drivers/oauth1.ts';
/**
* Twitter OAuth1 driver for authenticating users via Twitter.
* Supports fetching user profile information including username, name, and email.
* Uses OAuth 1.0a protocol.
*
* @example
* ```ts
* router.get('/twitter/redirect', ({ ally }) => {
* return ally.use('twitter').redirect()
* })
*
* router.get('/twitter/callback', async ({ ally }) => {
* const twitter = ally.use('twitter')
*
* if (twitter.accessDenied()) {
* return 'Access was denied'
* }
*
* if (twitter.stateMisMatch()) {
* return 'State mismatch error'
* }
*
* if (twitter.hasError()) {
* return twitter.getError()
* }
*
* const user = await twitter.user()
* return user
* })
* ```
*/
export declare class TwitterDriver extends Oauth1Driver<TwitterToken, string> {
protected ctx: HttpContext;
config: TwitterDriverConfig;
/**
* Twitter request-token endpoint URL.
*/
protected requestTokenUrl: string;
/**
* Twitter authorization endpoint URL.
*/
protected authorizeUrl: string;
/**
* Twitter access-token endpoint URL.
*/
protected accessTokenUrl: string;
/**
* Twitter profile endpoint URL.
*/
protected userInfoUrl: string;
/**
* The query string param name for the error.
*/
protected errorParamName: string;
/**
* The query string param name for the "oauth_verifier". Used
* for both the post redirect value access and during the
* time of generating the access token
*/
protected oauthTokenVerifierName: string;
/**
* Cookie name for storing the oauth_token. The cookie
* name for storing oauth_token_secret is derived
* from this property
*/
protected oauthTokenCookieName: string;
/**
* Param name for defined the "oauth_token" pre redirect
* and also used post redirect for reading the "oauth_token"
* value
*/
protected oauthTokenParamName: string;
/**
* Twitter doesn't support scopes
*/
protected scopeParamName: string;
/**
* Scope separator placeholder maintained for OAuth1 compatibility.
*/
protected scopesSeparator: string;
/**
* @param ctx - The HTTP context
* @param config - Configuration for the Twitter driver
*/
constructor(ctx: HttpContext, config: TwitterDriverConfig);
/**
* Fetches the authenticated user's profile information from the Twitter API.
*
* @param token - The OAuth token
* @param secret - The OAuth token secret
* @param callback - Optional callback to customize the API request
*
* @see https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials
*/
protected getUserInfo(token: string, secret: string, callback?: (request: ApiRequestContract) => void): Promise<{
id: any;
nickName: any;
name: any;
email: any;
emailVerificationState: "unsupported";
avatarUrl: any;
original: any;
}>;
/**
* Get the authenticated user's profile information using
* the OAuth verifier from the callback request.
*
* @param callback - Optional callback to customize the API request
*
* @example
* ```ts
* const user = await ally.use('twitter').user()
* console.log(user.name, user.email)
* ```
*/
user(callback?: (request: ApiRequestContract) => void): Promise<{
token: TwitterToken;
id: any;
nickName: any;
name: any;
email: any;
emailVerificationState: "unsupported";
avatarUrl: any;
original: any;
}>;
/**
* Get the user's profile information using an existing OAuth token
* and token secret.
*
* @param token - The OAuth token
* @param secret - The OAuth token secret
* @param callback - Optional callback to customize the API request
*
* @example
* ```ts
* const user = await ally.use('twitter').userFromTokenAndSecret(token, secret)
* ```
*/
userFromTokenAndSecret(token: string, secret: string, callback?: (request: ApiRequestContract) => void): Promise<AllyUserContract<{
token: string;
secret: string;
}>>;
/**
* Check if the error from the callback indicates that the user
* denied authorization.
*
* @returns `true` when the request contains the denial marker.
*/
accessDenied(): boolean;
}