¿Cómo uso Google Oauth en featherjs con un token de acceso existente? Los documentos no dan un ejemplo sobre esto. El único ejemplo es a través del navegador como se muestra aquí .
Al pasar por el navegador, http://localhost:3030/oauth/google funciona bien. Puedo agregar con éxito al usuario a mi base de datos. Aquí está mi código:
const { LocalStrategy } = require("@feathersjs/authentication-local"); const { expressOauth } = require("@feathersjs/authentication-oauth"); const { OAuthStrategy } = require("@feathersjs/authentication-oauth"); var uniqid = require("uniqid"); const { AuthenticationService, JWTStrategy } = require('@feathersjs/authentication'); class GoogleStrategy extends OAuthStrategy { async getEntityData(profile) { const baseData = await super.getEntityData(profile); console.log({ profile }); return { ...baseData, profilePicture: profile.picture, email: profile.email, password: uniqid.time(), }; } } module.exports = (app) => { const authentication = new AuthenticationService(app); authentication.register("local", new LocalStrategy()); authentication.register('jwt', new JWTStrategy()); authentication.register("google", new GoogleStrategy()); app.use("/authentication", authentication); app.configure(expressOauth()); };Sin embargo, si trato de usar un token de acceso, así
POST http://localhost:3030/authentication data: { "strategy": "google", "accessToken": "ya29.A0ARrdaM_UJa6idfZr-4taqwkJ6qGBV1Dp9wbxF-wsult8dNPaVNCVg6Fndmrqv7BhRSwxa5gAKllPvbKtsjyxS39WdmWmqkmE42HOsVZaJWHVEttxbebel3zdpD5BSxWtRiG7NuZLNVedMUaK5AdgIRrJk1u" }No me sale el perfil de google, no se agrega ningún usuario a mi base de datos y me sale este error:
{ "name": "GeneralError", "message": "401 Unauthorized", "code": 500, "className": "general-error", "data": {}, "errors": {} } He agregado "google" a mi lista de authStrategies en default.json
Entonces mi pregunta es ¿qué debo hacer?
Así que encontré la solución. Pensé que podría compartir. Agregue esto a autenticación.js
//add this method async getProfile(authResult) { const accessToken = authResult.accessToken; const { data } = await axios .get( `https://openidconnect.googleapis.com/v1/userinfo?access_token=${accessToken}` ) .then((res) => { return res; }) .catch((error) => console.log("autherr", error)); return data; }