ThisIsRudigo / firebaseauth

Firebase authentication library - a node js wrapper around the Firebase REST API
42 stars 8 forks source link

TypeError: Cannot read property 'callback' of undefined at node_modules/firebaseauth/dist/middlewares/guard.js:68:26. #18

Open GeetanshuGulati opened 6 years ago

GeetanshuGulati commented 6 years ago

Hi, I am using firebaseauth version ^1.0.0. I am getting an issue and not able to resolve. I am using firebase auth to authenticate users to use my endpoints. while using I am getting an error UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'callback' of undefined at node_modules/firebaseauth/dist/middlewares/guard.js:68:26.

I am getting this error when token is valid

Kindly guide me

my code const middlewareCallback = function(req, res, next, error, data) { if (error === 'ERROR_NO_TOKEN') { // token not supplied res.status(401).json({error: "No token provided"}); } else if (error === 'ERROR_INVALID_TOKEN') { // token failed verification res.status(401).json({error: "Unauthorized access"}); } else if (error) { // some other error occurred (this should never happen!) res.status(500).json({error: "Unexpected error"}); } else if (data.error) { // there was no error with verifying the token, thus user id can be found in data.userId // there was however an error in getting user info from firebase using the id res.status(401).json({error: "An error occurred while trying to verify your credentials"}); } else { // data contains user id and token (v0.2.0 and later) and full user information (id, displayName, email etc) for v0.1.1 and earlier req.user = data; next(); } };

const options = { tokenField: "key", userIdOnly: false };

const firebaseTokenMiddleware = FirebaseAuth.initTokenMiddleware(serviceAccount, options); app.use(firebaseTokenMiddleware)

GeetanshuGulati commented 6 years ago

In private fetchUserInfo = (userId: string, token: string, req: any, res: any, next: Function) => { admin.auth().getUser(userId) .then(function(userRecord) { const userInfo: any = new UserProfile(userRecord.toJSON()); userInfo.token = token; if (this.callback) { this.callback(req, res, next, undefined, userInfo); } else { req.user = userInfo; next(); } }) .catch(function(error) { if (this.callback) { this.callback(req, res, next, ERROR_RETRIEVE_USER_INFO, { userId: userId, error: error }); } else { res.status(401).json({error: "An error occurred while trying to load your account info"}); } }); };

it is saying "this is undefined" next to userInfo.token = token;