passport-annict
Version:
Passport strategy for authenticating with Annict using the OAuth 2.0 API.
48 lines (47 loc) • 1.66 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const passport_oauth2_1 = __importStar(require("passport-oauth2"));
const Profile_1 = require("./Profile");
class AnnictStrategy extends passport_oauth2_1.default {
constructor(options, verify) {
super({
...options,
authorizationURL: 'https://api.annict.com/oauth/authorize',
tokenURL: 'https://api.annict.com/oauth/token',
}, verify);
this.name = 'annict';
}
userProfile(accessToken, done) {
const query = /* graphql */ `
query {
viewer {
annictId
name
username
email
avatarUrl
}
}
`;
this._oauth2._request('POST', 'https://api.annict.com/graphql', { 'Content-Type': 'application/json' }, JSON.stringify({ query }), accessToken, (err, body) => {
if (err) {
return done(new passport_oauth2_1.InternalOAuthError('Failed to fetch user profile', err));
}
try {
const result = JSON.parse(body.toString());
return done(null, Profile_1.Profile.create(result));
}
catch (err) {
return done(new Error('Failed to parse user profile'));
}
});
}
}
exports.AnnictStrategy = AnnictStrategy;