Append firebase JWT in Ember-simple-auth

176 views Asked by At

I am trying to do authorization in my Ember App(2.10). My workflow is

  1. user hit the button of Facebook login then
  2. i'm using torii to get the access token /my user database is on firebase/
  3. Then i send token to firebase.auth with facebook provider. It returns JWT token.

Problem is i got the JWT token and now i have to login to my emberapp. I am trying to customize torii authenticator here. How can i implement this in ember app. Below is my authenticator:

authenticate() {
return this._super(...arguments).then((torii) => {
    const serverTokenEndpoint = this.get('serverTokenEndpoint');
    return this.get('ajax').request(serverTokenEndpoint, {
        type: 'POST',
        data: {
        'type': torii.provider,
        'client_id': this.client,
        'token': torii.authorizationCode
        }
    }).then((token) => {
        var provider = new firebase.auth.FacebookAuthProvider();
        firebase.auth().signInWithPopup(provider).then(function(result) {
        // This gives Facebook Access Token.
        // JWT-token=result.user.Cd
        // JWT-token.iat at=result.user.ea.Sa
        // JWT-token-refresh = result.user.refreshToken
        console.log(result)
        //   token = result.user.Cd;
        // const expiresAt = this._absolutizeExpirationTime(result.user.ea.Sa);
        token = Ember.assign(token, { 'expires_at': result.user.ea.Sa });
        // this._scheduleAccessTokenRefresh(result.user.ea.Sa, expiresAt, result.user.refreshToken, torii);
        return Ember.assign(token, {'torii': torii});
        });
    });
    });  
}
2

There are 2 answers

1
marcoow On BEST ANSWER

Check out this guide in the ESA repo. It covers torii and Github auth but the general concepts are the same for your use case.

3
Zorig On

@marcoow I did try this and it authenticate but when token is expired i can not refresh token.Seems it is not the right approach, How can i refresh token using firebase

export default ToriiAuthenticator.extend({
    torii: Ember.inject.service(),
    ajax: Ember.inject.service(),
    refreshAccessTokens: true,
    rejectWithResponse: false,

    restore(data) {
        return new RSVP.Promise((resolve, reject) => {
            const now = (new Date()).getTime();
            const refreshAccessTokens = this.get('refreshAccessTokens');
            if (!isEmpty(data['expires_at']) && data['expires_at'] < now) {
                // if (refreshAccessTokens) {
                this._refreshAccessToken(data['expires_in'], data['refresh_token']).then(() => {
                    resolve();
                }).catch(function(error) {
                    reject();
                });

                // } else {
                // reject();
                // }
            } else {
                if (!this._validate(data)) {
                    reject();
                } else {
                    this._scheduleAccessTokenRefresh(data['expires_in'], data['expires_at'], data['refresh_token']);
                    resolve(data);
                }
            }
        });
    },
    authenticate() {
        return new Ember.RSVP.Promise((resolve, reject) => {
            var provider = new firebase.auth.FacebookAuthProvider();
            firebase.auth().signInWithPopup(provider).then((result) => {
                var expires_in = this._absolutizeExpirationTime(result.user.ea.Sa);
                var expiresAt = result.user.ea.Sa;
                result = Ember.assign(result, { 'expires_at': expiresAt, 'expires_in': expires_in, 'access_token': result.user.Cd, 'refresh_token': result.refresh_token });
                resolve(result)
            });
            // const useResponse = this.get('rejectWithResponse');
            // const provider = new firebase.auth.FacebookAuthProvider();
            // firebase.auth().signInWithPopup(provider).then((result) => {
            //     let expires_in = result.user.ea.Sa;
            //     const expiresAt = this._absolutizeExpirationTime(expires_in);
            //     this._scheduleAccessTokenRefresh(expires_in, expiresAt, result.refresh_token);
            //     if (!isEmpty(expiresAt)) {
            //         result = Ember.assign(result, { 'expires_at': expiresAt, 'expires_in': expires_in, 'access_token': result.user.Cd, 'refresh_token': result.refresh_token });
            //     }
            //     // resolve(result);
            // }, (response) => {
            //     Ember.run(null, reject, useResponse ? response : response.responseJSON);
            // }).catch(function(error) {
            //     console.log(error);
            // });
        });
    },
    invalidate(data) {
        const serverTokenRevocationEndpoint = this.get('serverTokenRevocationEndpoint');
        return new RSVP.Promise((resolve) => {
            if (isEmpty(serverTokenRevocationEndpoint)) {
                resolve();
            } else {
                if (!Ember.isEmpty(data.access_token)) {
                    delete data.access_token;
                    firebase.auth().signOut();
                    resolve();
                }
            }
        });
    },
    _scheduleAccessTokenRefresh(expiresIn, expiresAt, refreshToken) {
        console.log('sched')
        const refreshAccessTokens = this.get('_refreshAccessTokens');
        if (refreshAccessTokens) {
            const now = (new Date()).getTime();
            if (isEmpty(expiresAt) && !isEmpty(expiresIn)) {
                expiresAt = new Date(now + expiresIn * 1000).getTime();
            }
            const offset = this.get('tokenRefreshOffset');
            if (!isEmpty(refreshToken) && !isEmpty(expiresAt) && expiresAt > now - offset) {
                run.cancel(this._refreshTokenTimeout);
                delete this._refreshTokenTimeout;
                if (!testing) {
                    this._refreshTokenTimeout = run.later(this, this._refreshAccessToken, expiresIn, refreshToken, expiresAt - now - offset);
                }
            }
        }
    },

    _refreshAccessToken(expiresIn, refreshToken) {
        console.log('refresh');
        const data = { 'grant_type': 'refresh_token', 'refresh_token': refreshToken };
        firebase.auth().currentUser.getToken(/ forceRefresh / true).then((response) => {
            return new RSVP.Promise((resolve, reject) => {
                // firebase.auth().currentUser.getToken(true).then((response) => {
                expiresIn = response.user.ea.Sa || expiresIn;
                refreshToken = response.refresh_token || refreshToken;
                const expiresAt = this._absolutizeExpirationTime(expiresIn);
                const data = assign(response, { 'expires_in': expiresIn, 'expires_at': expiresAt, 'refresh_token': refreshToken });
                this._scheduleAccessTokenRefresh(expiresIn, null, refreshToken);
                this.trigger('sessionDataUpdated', data);
                resolve(data);
            }, (response) => {
                warn(`Access token could not be refreshed - server responded with ${response.responseJSON}.`);
                reject();
            });
        });
    },

    _absolutizeExpirationTime(expiresIn) {
        if (!isEmpty(expiresIn)) {
            return new Date((new Date().getTime()) + expiresIn * 1000).getTime();
        }
    },

    _validate(data) {
        return !isEmpty(data['access_token']);
    }
});