0.1.21 • Published 9 years ago

node-github-oauth2 v0.1.21

Weekly downloads
42
License
ISC
Repository
github
Last release
9 years ago

Node Github oauth2

This repository contains node module for authenticating with github and interacting with github.

Demo:

/**
 * Created by amitthakkar on 14/07/16.
 */
var express = require('express'),
    app = express();

const NodeGithubOAuth2 = require('node-github-oauth2');
NodeGithubOAuth2.initialize({
    clientId: 'ZZZZZZZZZZZZZZZZZZZZ',
    clientSecret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    redirectURI: 'http://localhost:3000/callback',
    scope: 'repo,admin:org,delete_repo',
    userAgent: 'Github-Test-App',
    gitDirectory: '/Users/amitthakkar/My Stuff/Projects/OSP/'
});

// Initial page redirecting to Github
exports.authorized = (request, response) => {
    response.status(200).json({redirectURL: NodeGithubOAuth2.getRedirectURL(request.user._id.toString())});
};

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', NodeGithubOAuth2.getToken, function (request, response) {
    console.log(request.token)
    response.json(request.token);
}, function (error, request, res, next) {
    console.log('Access Token Error', error.message);
    res.json(error);
});


exports.authorizedCallback = [NodeGithubOAuth2.getToken, (request, response) => {
    let token = request.token;
    let _id = request.state;
    NodeGithubOAuth2.getUserDetails({
        token: token
    }, (error, user) => {
        if (!user.email) {
            NodeGithubOAuth2.getEmailIds({
                token: token
            }, (error, emailIds) => {
                let emailId = emailIds.filter((email) => {
                    return email.primary;
                })[0];
                // User Details
                console.log({
                    token: token,
                    username: user.login,
                    email: emailId.email
                });
            });
        } else {
            console.log({
                token: token,
                username: user.login,
                email: emailId.email
            });
        }
    });
}];

app.get('/', function (req, res) {
    res.send('Hello<br><a href="/auth">Log in with Github</a>');
});

app.get('/orgs', function (req, res) {
    NodeGithubOAuth2.getOrganization({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
    }, function (error, result) {
        res.json(result);
    });
});

app.get('/createProject', function (req, res) {
    NodeGithubOAuth2.createRepoAndCloneProject({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
        org: 'OrganizationName',
        name: 'ngt1',
        description: 'Node-Git-Test-2',
        private: true
    }, function (error, result) {
        res.json(result);
    });
});

app.get('/cloneProject', function (req, res) {
    NodeGithubOAuth2.cloneProject({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
        org: 'OrganizationName',
        name: 'ngt1',
        description: 'Node-Git-Test-2',
        private: true
    }, function (error, result) {
        res.json(result);
    });
});

app.get('/deleteProject', function (req, res) {
    NodeGithubOAuth2.deleteGithubRepo({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
        org: 'OrganizationName',
        name: 'ngt1'
    }, function (error, result) {
        res.json(result);
    });
});

app.get('/addCollaborators', function (req, res) {
    NodeGithubOAuth2.addCollaborators({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
        user: 'OrganizationName',
        repo: 'ngt1',
        collabuser: 'NamitaMalik',
        permission: 'admin'
    }, function (error, result) {
        if (error) {
            res.json(result);
        } else {
            res.json(result);
        }
    });
});

app.get('/removeCollaborator', function (req, res) {
    NodeGithubOAuth2.removeCollaborator({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
        user: 'OrganizationName',
        repo: 'ngt1',
        collabuser: 'NamitaMalik'
    }, function (error, result) {
        if (error) {
            res.json(result);
        } else {
            res.json(result);
        }
    });
});

app.get('/commitAndPush', function (req, res) => {
    NodeGithubOAuth2.commitAndPush({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
        email: 'vigildbest@gmail.com',
        name: 'projectName',
        username: 'AmitThakkar',
        org: 'NA',
        commitMessage: 'Commit by Wize Server'
    }, (error, result) => {
        NodeGithubOAuth2.createRelease({
            token: options.github.token,
            user: options.org,
            repo: options.name,
            tag_name: options.tag
        }, function (error, result) {
            res.json(result);
        });
    });
});

exports.updateProject = (req, res) => {
    NodeGithubOAuth2.updateProject({
        token: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY',
        name: 'projectName',
        username: 'AmitThakkar',
        org: 'NA'
    }, function (error, result) {
        res.json(result);
    });
};

app.listen(3000);

console.log('Express server started on port 3000');
0.1.21

9 years ago

0.1.20

9 years ago

0.1.19

10 years ago

0.1.18

10 years ago

0.1.17

10 years ago

0.1.16

10 years ago

0.1.15

10 years ago

0.1.14

10 years ago

0.1.13

10 years ago

0.1.12

10 years ago

0.1.11

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.17

10 years ago

0.0.16

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago