@adonisjs/ally
Version:
Social authentication provider for AdonisJS
137 lines (136 loc) • 3.79 kB
JavaScript
import {
Oauth2Driver
} from "../../chunk-GWAQFMNS.js";
import "../../chunk-N72DEJC2.js";
import "../../chunk-PZ5AY32C.js";
// src/drivers/facebook.ts
var FacebookDriver = class extends Oauth2Driver {
constructor(ctx, config) {
super(ctx, config);
this.config = config;
this.loadState();
}
accessTokenUrl = "https://graph.facebook.com/v10.0/oauth/access_token";
authorizeUrl = "https://www.facebook.com/v10.0/dialog/oauth";
userInfoUrl = "https://graph.facebook.com/v10.0/me";
/**
* The default set of fields to query for the user request
*/
userFields = [
"name",
"first_name",
"last_name",
"link",
"email",
"picture.width(400).height(400)",
"verified"
];
/**
* The param name for the authorization code
*/
codeParamName = "code";
/**
* The param name for the error
*/
errorParamName = "error";
/**
* Cookie name for storing the "facebok_oauth_state"
*/
stateCookieName = "facebok_oauth_state";
/**
* Parameter name to be used for sending and receiving the state
* from Facebok
*/
stateParamName = "state";
/**
* Parameter name for defining the scopes
*/
scopeParamName = "scope";
/**
* Scopes separator
*/
scopesSeparator = " ";
/**
* Configuring the redirect request with defaults
*/
configureRedirectRequest(request) {
request.scopes(this.config.scopes || ["email"]);
request.param("response_type", "code");
request.param("grant_type", "authorization_code");
if (this.config.display) {
request.param("display", this.config.display);
}
if (this.config.authType) {
request.param("auth_type", this.config.authType);
}
}
/**
* Returns the HTTP request with the authorization header set
*/
getAuthenticatedRequest(url, token) {
const request = this.httpClient(url);
request.header("Authorization", `Bearer ${token}`);
request.header("Accept", "application/json");
request.parseAs("json");
return request;
}
/**
* Fetches the user info from the Facebook API
* https://developers.facebook.com/docs/graph-api/reference/user/
*/
async getUserInfo(token, callback) {
const request = this.getAuthenticatedRequest(this.config.userInfoUrl || this.userInfoUrl, token);
request.param("fields", (this.config.userFields || this.userFields).join(","));
const body = await request.get();
if (typeof callback === "function") {
callback(request);
}
return {
id: body.id,
name: body.name,
nickName: body.name,
// https://developers.facebook.com/docs/graph-api/reference/user/picture/
avatarUrl: body.picture?.data?.url || null,
email: body.email || null,
// May not always be there (requires email scope)
// Important note: https://developers.facebook.com/docs/facebook-login/multiple-providers#postfb1
emailVerificationState: "verified" in body ? body.verified ? "verified" : "unverified" : "unsupported",
original: body
};
}
/**
* Find if the current error code is for access denied
*/
accessDenied() {
const error = this.getError();
if (!error) {
return false;
}
return error === "access_denied";
}
/**
* Returns details for the authorized user
*/
async user(callback) {
const token = await this.accessToken(callback);
const user = await this.getUserInfo(token.token, callback);
return {
...user,
token
};
}
/**
* Finds the user by the access token
*/
async userFromToken(token, callback) {
const user = await this.getUserInfo(token, callback);
return {
...user,
token: { token, type: "bearer" }
};
}
};
export {
FacebookDriver
};
//# sourceMappingURL=facebook.js.map