feathersjs-ecosystem / authentication

[MOVED] Feathers local, token, and OAuth authentication over REST and Websockets using JSON Web Tokens (JWT) with PassportJS.
MIT License
317 stars 118 forks source link

Unable to redirect and set client JWT #590

Closed DelfsEngineering closed 5 years ago

DelfsEngineering commented 6 years ago

(Posted here from Slack on request from @daffl )

I am incorporating LoginRadius authentication (a similar service to Auth0 but I am forced to use V1 of their API ), So far I have the authentication with their service return a temp token which I exchange server side for an auth-token and grab the user profile from their API. I obtain a Feathers JWT and am stuck from here.

I think I need to pass the token to the client and redirect the client to my homepage but don't see how? As things stand, I have the feathers JWT and just need to know to redirect and authenticate?

For reference to what I've done in my manual/custom strategy

// custom_strategy service
'use strict';

// Calls LoginRadius and exchanges request token for Access Token
// returns jwt with user id

const hooks = require('./hooks');
const request = require('request-promise');
const app = this

// A request instance that talks to the LoginRadius API
const makeRequest = request.defaults({
    baseUrl: 'https://api.loginradius.com',
    json: true
});

const Service = {
    setup(app) {
        this.app = app;
    },

    create(data, params) {
        const lr = this.app.get('loginRadius') // lr.secret ,lr.key, lr.appname
        let qs = {
            token: data.token,
            secret: lr.secret
        }

        // exchange for login radius access_token
        let response = makeRequest({
                uri: '/api/v2/access_token',
                qs: qs,
                method: 'GET'
            })

            // Get login radius profile
            .then(result => {
                // console.log('access_token', result.access_token)
                let qs = {
                    access_token: result.access_token
                }

                return makeRequest({
                    uri: 'api/v2/userprofile',
                    qs: qs,
                    method: 'GET'
                })
            })

            // try to find by login radius ID first
            .then(profile => {
                this.lr = profile
                this.lr.email = profile.Email[0].Value
                // console.log('profile lr: ', this.lr)

                const query = {
                    loginRadiusId: profile.ID, // loginRadius id
                    $limit: 1
                };
                return this.app.service('/users').find({ query })
            })

            // If not found try email
            .then(result => {
                if (result.total) {
                    return result
                }
                else {
                    // try by email
                    const query = {
                        email: '==' + this.lr.email, // loginRadius id
                        $limit: 1
                    };
                    // console.log('query2', query)
                    return this.app.service('/users').find({ query })
                }
            })

            // Create or merge profile with db record
            .then(result => {
                // console.log('fms result lr by email', result)
                if (result.total) { // have one, patch
                    var id = result.data[0].id
                    // update
                    let newData = {
                        loginRadiusId: this.lr.ID,
                        loginRadiusProfile: JSON.stringify(this.lr)
                    }
                    return this.app.service('/users').patch(id, newData, {})
                }
                else {
                    // create
                    let newData = {
                        email: this.lr.email,
                        loginRadiusId: this.lr.ID,
                        loginRadiusProfile: JSON.stringify(this.lr)
                    }
                    return this.app.service('/users').create(newData, {})
                }
            })

            // get JTW and redirect????
            .then(result => {
                // console.log('result #107', result)
                let payload = { userId: result.id } // result is just the record

                const params = {
                    authenticated: true,
                    // [options.entity]: 'someEntity',
                    payload
                };
                const data = {
                    // [options.entity]: entity,
                    payload
                };
                return this.app.service('authentication').create(data, params)
                    .then(result => {
                        console.log('auth call result: ', result)

                        // redirect user here with new token ???

                        return result // for testing just return the feathers Auth toekn
                    })
                return
            })

            .catch(error => {
                console.log('lr Cattachall error: ', error)
                return error
                // TODO failure redirect

            });

        return response
    }
}

Origional Slack Link

daffl commented 5 years ago

This should be more easily possible now with Feathers v4 authentication.

Please see the migration guide for more information. Closing this issue in order to archive this repository. Related issues can be opened at the new code location in the Feathers main repository.