Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

oauth2orize is not working with hapijs #16

Open
@AmayraRajput

Description

@AmayraRajput

How do I implement oauth2orize in hapijs. Please suggest. I did this code but it's not working when I am hitting the API. I am new in hapijs and oauth2orize. Also hapi-oauth2orize is not working as I am using hapi 13.4.1 which is not compatible with hapi-oauth2orize 1.3.0, throwing error 'Invalid plugin options {}' .Please suggest me a way to implement oauth2orize in hapijs.

const Hapi = require('hapi');
const server = new Hapi.Server();
const oauth2orize = require('oauth2orize');
var oauth = oauth2orize.createServer();

server.connection({ 
    host: 'localhost', 
    port: 8000 
});

server.register([{
    register: require('hapi-mongodb'),
    options: dbOpts
}], function (err) {
    if (err) {
        console.error(err);
        throw err;
    }
    server.start();

    server.route([
                {
                  method: 'GET',
                  path: '/oauth/authorizegrant',
                  config: {
                    auth: false,
                    handler: function(request, reply) {
                        var clientId = request.query.client_id,
                            redirectUrl = request.query.redirect_uri,
                            resType = request.query.response_type,
                            state = request.query.state;
                        oauth.grant(oauth2orize.grant.code(function(clientId,redirectUrl,resType,state,callback) {
                          // Create a new authorization code
                              console.log('client', client);
                              var db = request.server.plugins['hapi-mongodb'].db;
                              var code = new Code({
                                value: uid(16),
                                clientId: client._id,
                                redirectUri: redirectUri,
                                userId: user._id
                              });

                          // Save the auth code and check for errors
                        db.collection('codes').insert(code, function(err) {
                            if (err) { console.log('err*********', err); return callback(err); }

                            callback(null, code.value);
                          });
                        }));

                    }
                  }
                },
            ]);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions