@adonisjs/ally
Version:
Social authentication provider for AdonisJS
179 lines (178 loc) • 5.55 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import type { HttpClient } from '@poppinss/oauth-client';
import type { GoogleToken, GoogleScopes, GoogleDriverConfig, ApiRequestContract, RedirectRequestContract } from '../types.ts';
import { Oauth2Driver } from '../abstract_drivers/oauth2.ts';
/**
* Google OAuth2 driver for authenticating users via Google.
* Supports fetching user profile information including name, email, and profile picture.
* Automatically prefixes scopes with the appropriate Google API URL.
*
* @example
* ```ts
* router.get('/google/redirect', ({ ally }) => {
* return ally.use('google').redirect((request) => {
* request.scopes(['userinfo.email', 'userinfo.profile', 'calendar.readonly'])
* })
* })
*
* router.get('/google/callback', async ({ ally }) => {
* const google = ally.use('google')
*
* if (google.accessDenied()) {
* return 'Access was denied'
* }
*
* if (google.stateMisMatch()) {
* return 'State mismatch error'
* }
*
* if (google.hasError()) {
* return google.getError()
* }
*
* const user = await google.user()
* return user
* })
* ```
*/
export declare class GoogleDriver extends Oauth2Driver<GoogleToken, GoogleScopes> {
config: GoogleDriverConfig;
/**
* Google token endpoint URL.
*/
protected accessTokenUrl: string;
/**
* Google authorization endpoint URL.
*/
protected authorizeUrl: string;
/**
* Google user profile endpoint URL.
*/
protected userInfoUrl: string;
/**
* The param name for the authorization code
*/
protected codeParamName: string;
/**
* The param name for the error
*/
protected errorParamName: string;
/**
* Cookie name for storing the "google_oauth_state"
*/
protected stateCookieName: string;
/**
* Parameter name to be used for sending and receiving the state
* from google
*/
protected stateParamName: string;
/**
* Parameter name for defining the scopes
*/
protected scopeParamName: string;
/**
* Scopes separator
*/
protected scopesSeparator: string;
/**
* @param ctx - The HTTP context
* @param config - Configuration for the Google driver
*/
constructor(ctx: HttpContext, config: GoogleDriverConfig);
/**
* Configures the redirect request with default scopes and Google-specific
* parameters like access_type, prompt, display, and hosted domain.
*
* @param request - The redirect request to configure
*/
protected configureRedirectRequest(request: RedirectRequestContract<GoogleScopes>): void;
/**
* Creates an authenticated HTTP request with the proper authorization
* header for Google API calls.
*
* @param url - The API endpoint URL
* @param token - The access token
*/
protected getAuthenticatedRequest(url: string, token: string): HttpClient;
/**
* Fetches the authenticated user's profile information from the Google API.
*
* @param token - The access token
* @param callback - Optional callback to customize the API request
*/
protected getUserInfo(token: string, callback?: (request: ApiRequestContract) => void): Promise<{
id: any;
nickName: any;
name: any;
email: any;
avatarUrl: any;
emailVerificationState: "verified" | "unverified";
original: any;
}>;
/**
* Check if the error from the callback indicates that the user
* denied authorization.
*/
accessDenied(): boolean;
/**
* Get the access token using the authorization code from the callback request.
* Returns a GoogleToken that includes the ID token.
*
* @param callback - Optional callback to customize the API request
*/
accessToken(callback?: (request: ApiRequestContract) => void): Promise<GoogleToken>;
/**
* Get the authenticated user's profile information using
* the authorization code from the callback request.
*
* @param callback - Optional callback to customize the API request
*
* @example
* ```ts
* const user = await ally.use('google').user()
* console.log(user.name, user.email)
* ```
*/
user(callback?: (request: ApiRequestContract) => void): Promise<{
token: GoogleToken;
id: any;
nickName: any;
name: any;
email: any;
avatarUrl: any;
emailVerificationState: "verified" | "unverified";
original: any;
}>;
/**
* Get the user's profile information using an existing
* access token.
*
* @param token - The Google access token
* @param callback - Optional callback to customize the API request
*
* @example
* ```ts
* const user = await ally.use('google').userFromToken(accessToken)
* ```
*/
userFromToken(token: string, callback?: (request: ApiRequestContract) => void): Promise<{
token: {
token: string;
type: "bearer";
};
id: any;
nickName: any;
name: any;
email: any;
avatarUrl: any;
emailVerificationState: "verified" | "unverified";
original: any;
}>;
/**
* Prefixes Google scopes with the appropriate API URL.
* Converts short scope names like 'userinfo.email' to full URLs.
*
* @param scopes - Array of scope names to prefix
*/
buildScopes(scopes: string[]): string[];
}