Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GeetanshuGulati opened this issue Aug 20, 2018 · 1 comment

Comments

@GeetanshuGulati
Copy link

GeetanshuGulati commented Aug 20, 2018

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 GeetanshuGulati changed the title Error after token authentication TypeError: Cannot read property 'callback' of undefined at node_modules/firebaseauth/dist/middlewares/guard.js:68:26. Aug 20, 2018
@GeetanshuGulati
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant