remix-auth-afdian
Version:
爱发电 for Remix Auth
79 lines (78 loc) • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AfdianStrategy = exports.AfdianStrategyScopeSeperator = exports.AfdianStrategyDefaultScope = exports.AfdianStrategyDefaultName = void 0;
/* eslint-disable @typescript-eslint/ban-ts-comment */
const remix_auth_1 = require("remix-auth");
const remix_auth_oauth2_1 = require("remix-auth-oauth2");
exports.AfdianStrategyDefaultName = 'afdian';
exports.AfdianStrategyDefaultScope = 'basic';
exports.AfdianStrategyScopeSeperator = ' ';
class AfdianStrategy extends remix_auth_oauth2_1.OAuth2Strategy {
constructor({ clientID, clientSecret, callbackURL, userAgent, scope, authorizationURL = 'https://afdian.net/oauth2/authorize', tokenURL = 'https://afdian.net/api/oauth2/access_token' }, verify) {
super({
clientID,
clientSecret,
callbackURL,
authorizationURL,
tokenURL
}, verify);
this.name = exports.AfdianStrategyDefaultName;
this.scope = this.getScope(scope);
this.userAgent = userAgent !== null && userAgent !== void 0 ? userAgent : 'Remix Auth';
}
//Allow users the option to pass a scope string, or typed array
getScope(scope) {
if (!scope) {
return [exports.AfdianStrategyDefaultScope];
}
else if (typeof scope === 'string') {
return scope.split(exports.AfdianStrategyScopeSeperator);
}
return scope;
}
/**
* Format the data to be sent in the request body to the token endpoint.
*/
// @ts-ignore
async fetchAccessToken(code) {
let headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
const params = new URLSearchParams();
params.set('grant_type', 'authorization_code');
params.set('client_id', this.clientID);
params.set('client_secret', this.clientSecret);
params.set('code', code);
params.set('redirect_uri', this.callbackURL.includes('?') ? this.callbackURL.split('?')[0] : this.callbackURL);
let response = await fetch(this.tokenURL, {
method: 'POST',
headers,
body: params
});
let json = await response.json();
if (json.ec !== 200) {
console.error(json.data);
throw new remix_auth_1.AuthorizationError(json.data.error);
}
return {
// @ts-ignore
accessToken: {
provider: 'afdian',
id: json.data.user_id,
displayName: json.data.name,
// @ts-ignore
photos: [{ value: json.data.avatar }],
_json: json.data
}
};
}
// @ts-ignore
async getAccessToken(profile) {
return profile;
}
// @ts-ignore
async userProfile(profile) {
return profile;
}
}
exports.AfdianStrategy = AfdianStrategy;