Skip to content

Instantly share code, notes, and snippets.

@abz89
Forked from marshallswain/authentication.js
Created September 24, 2021 08:59
Show Gist options
  • Select an option

  • Save abz89/63f6a0c304e97586aa3cfd3bf4ea7cc7 to your computer and use it in GitHub Desktop.

Select an option

Save abz89/63f6a0c304e97586aa3cfd3bf4ea7cc7 to your computer and use it in GitHub Desktop.

Revisions

  1. @marshallswain marshallswain revised this gist Apr 12, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions authentication.js
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,7 @@ module.exports = function () {
    name: 'github',
    Strategy: GithubStrategy,
    // Provide the handler to the GitHub auth setup.
    // The successRedirect should point to the handle-oauth-login.html hosted on the web server.
    handler: handler(config.github.successRedirect)
    }, config.github)));

  2. @marshallswain marshallswain revised this gist Apr 12, 2017. No changes.
  3. @marshallswain marshallswain created this gist Apr 12, 2017.
    40 changes: 40 additions & 0 deletions authentication.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    'use strict';

    const authentication = require('feathers-authentication');
    const jwt = require('feathers-authentication-jwt');
    const local = require('feathers-authentication-local');
    const oauth2 = require('feathers-authentication-oauth2');
    const GithubStrategy = require('passport-github');
    // Bring in the oauth-handler
    const makeHandler = require('./oauth-handler');

    module.exports = function () {
    const app = this;
    const config = app.get('authentication');

    // Create a handler by passing the `app` object.
    const handler = makeHandler(app);

    // Set up authentication with the secret
    app.configure(authentication(config));
    app.configure(jwt());
    app.configure(local());

    app.configure(oauth2(Object.assign({
    name: 'github',
    Strategy: GithubStrategy,
    // Provide the handler to the GitHub auth setup.
    handler: handler(config.github.successRedirect)
    }, config.github)));

    app.service('authentication').hooks({
    before: {
    create: [
    authentication.hooks.authenticate(config.strategies)
    ],
    remove: [
    authentication.hooks.authenticate('jwt')
    ]
    }
    });
    };
    28 changes: 28 additions & 0 deletions handle-oauth-login.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Handle OAuth Login</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
    <script>
    function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    if (decodeURIComponent(pair[0]) == variable) {
    return decodeURIComponent(pair[1]);
    }
    }
    console.log('Query variable %s not found', variable);
    }
    var token = getQueryVariable('token');
    if (token) {
    window.localStorage.setItem('feathers-jwt', token);
    }
    window.location = '/';
    </script>
    </body>
    </html>
    20 changes: 20 additions & 0 deletions oauth-handler.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    module.exports = function (app) {
    return function (url) {
    const config = app.get('authentication');
    const options = {
    jwt: config.jwt,
    secret: config.secret
    };

    return function (req, res, next) {
    if (req.feathers && req.feathers.payload) {
    app.passport.createJWT(req.feathers.payload, options).then(token => {
    res.redirect(`${url}?token=${token}`);
    })
    .catch(error => {
    next(error);
    });
    }
    };
    };
    };