any chance @julian or someone else who has done this would help push me the right direction.
I think I am doing everything right but there is no message whatsoever on nodebb's log to see if I am mistaken.
PS: I just noticed something that could be causing problems for me. What is the name required supposed to do. I noticed it is being used in strategies.push from my understanding of how this works: nodebb makes a call to my authorization end point and I check if the client is correct and grant it an access code, it then visits my token end point and exchanges this access code for a token. I take it to the user end point and hands it my users credentials to log in. I think I am wrong since it is not working.
// Register authorization code grant type
server.grant(oauth2orize.grant.code(function(client, redirectUri, user, ares, callback) {
// Create a new authorization code
var code = new Code({
value: uid(16),
clientId: client._id,
redirectUri: redirectUri,
userId: user._id
});
// Save the auth code and check for errors
code.save(function(err) {
if (err) { return callback(err); }
callback(null, code.value);
});
}));
// Exchange authorization codes for access tokens
server.exchange(oauth2orize.exchange.code(function(client, code, redirectUri, callback) {
console.log(client, code, redirectUri)
Code.findOne({ value: code }, function (err, authCode) {
if (err) { return callback(err); }
console.log(authCode.clientId,client._id);
if (authCode === undefined) { return callback(null, false); }
if (client._id.toString() !== authCode.clientId) { return callback(null, false); }
//if (redirectUri !== authCode.redirectUri) { return callback(null, false); }
// Delete auth code now that it has been used
authCode.remove(function (err) {
if(err) { return callback(err); }
// Create a new access token
var token = new Token({
value: uid(256),
clientId: authCode.clientId,
userId: authCode.userId
});
// Save the access token and check for errors
token.save(function (err) {
if (err) { return callback(err); }
callback(null, token);
});
});
});
}));
setCodes = function(client, user, redirectUri, Code){
console.log('starting...')
var code = new Code();
code.value = uid(16),
code.clientId = client.id,
code.userId = user._id
code.save(function(error, success){
if(!success){
console.log(Code.find({}), 'I ran hered', code, error);
}
});
//console.log(code);
//Code.find({}, function(error, success){if(success){console.log(success, "george")}});
}
module.exports.authorization = [
server.authorization(function(clientId, redirectUri, callback) {
Client.findOne({ id: clientId }, function (err, client) {
if (err) { console.log("I ran herea");return callback(err); }
return callback(null, client, redirectUri);
});
}, function (client, user, redirectUri, done) {
console.log(redirectUri);
setCodes(client, user, redirectUri, Code);
Code.find({
clientId: client.id,
userId: user._id
}, function (err, codes) {
console.log(codes);
if (err) { console.log("second"); return done(err); }
if (codes.length > 0) {
console.log("third")
return done(null, true);
} else {
console.log('I ran here fourth');
return done(null,false);
}
});
})
]
// Application client token exchange endpoint
module.exports.token = [
server.token(),
server.errorHandler()
]
module.exports.getNodebbUsers = function(req, res){
console.log(req.body.token);
Token.find({value : req.body.token}, function(error, user){
userId = user[0].userId;
if(!error){
User.find({_id : userId}, function(error, user){
console.log(userId);
if(!error){
res.json({
username : user[0].userName,
email : user[0].email,
id : user[0]._id
})
} else {
console.log(error);
}
})
}
})
}
btw this is my code above. I could do with some help seeing I have pushed myself to the limit.
Why does the callback uri have callback appended to its name and should this be an endpoint in my app?
I also intend posting my solutions once this starts working to save someone the wasted hours I spent reading, debugging and hacking my codes.